
Originally Posted by
ReGGaeBOSS
Hmm many people is telling me this, but my concern is, now i have spent ages building my little CMS, isnt it hard for me as a beginner in php and mysql to "convert" my code to mysli? Should i try to do it or check it out for my next project?
Converting it to the procedural MySQLi API is simple because both APIs are very similar. Take the following script written with the original MySQL extension (taken from a PHPMaster article):
PHP Code:
$db = mysql_connect('host', 'username', 'password');
mysql_select_db('database', $db);
$clean['name'] = mysql_real_escape_string($name);
$clean['email'] = mysql_real_escape_string($email);
mysql_query("INSERT INTO table_name VALUES (NULL, '{$clean['name']}', '{$clean['email']}')");
mysql_query("UPDATE table_name SET name = 'Thomas' WHERE email = '{$clean['email']}'");
$result = mysql_query("SELECT name FROM table_name WHERE email = '{$clean['email']}'");
if ($row = mysql_fetch_assoc($result)) {
echo $row['name'];
} else {
echo 'No results found.';
}
And then convert it to the MySQLi API:
PHP Code:
$db = mysqli_connect('host', 'username', 'password');
mysqli_select_db($db, 'database');
$clean['name'] = mysqli_real_escape_string($db, $name);
$clean['email'] = mysqli_real_escape_string($db, $email);
mysqli_query($db, "INSERT INTO table_name VALUES (NULL, '{$clean['name']}', '{$clean['email']}')");
mysqli_query($db, "UPDATE table_name SET name = 'Thomas' WHERE email = '{$clean['email']}'");
$result = mysqli_query($db, "SELECT name FROM table_name WHERE email = '{$clean['email']}'");
if ($row = mysqli_fetch_assoc($result)) {
echo $row['name'];
} else {
echo 'No results found.';
}
Aside from the changing of mysql_* to mysqli_*, the only real difference is that we are forced to pass the connection argument as the first parameter to the mysqli class functions (the first table), as opposed to the optional passing of it as the second argument in the original MySQL extension.
Bookmarks