PHP 4 to PHP 5 MySQLi

Hi Folks,

Can anyone help me convert this code from Dreamweaver to a PHP5 compliant state (using MySqli). My version of Dreamweaver won’t do it.

// query for details of each course
mysql_select_db($database_con_mydata, $con_mydata);
$query_rs_courses = “SELECT * FROM courses WHERE coursecode LIKE ‘KCC%’ ORDER BY id”;
$rs_courses = mysql_query($query_rs_courses, $con_mydata) or die(mysql_error());
$row_rs_courses = mysql_fetch_assoc($rs_courses);
$totalRows_rs_courses = mysql_num_rows($rs_courses);

I have tried but it’s all a bit beyond me.

If you’re not tied down to the mysqli_* extension, go for PDO as it allows for named parameters with prepared statements and it’ll work with multiple types of database server.

An example might help.

$login = "";
$dbpass = "";
$db = "";

try {
    $pdo = new PDO('mysql:host=localhost;dbname='.$db, $login, $dbpass);
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );   
} catch (PDOException $e) {
   // echo "Error!: " . $e->getMessage() . "<br/>";
    echo "Database not available at this time";

$searchterm = 'KCC%';

$sql = "SELECT * FROM courses WHERE coursecode LIKE :coursecode ORDER BY id";
$query = $pdo->prepare($sql);
$query->bindParam(':coursecode', $searchterm);
while($row = $query->fetch(PDO::FETCH_ASSOC)){

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.