tags:

views:

290

answers:

3

Based on this code below I use for regular mysql, how could I convert it to use mysqli?

Is it as simple as changing **mysql _query($sql); to mysqli _query($sql); ?**

<?PHP

//in my header file that is included on every page I have this
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>");
mysql_select_db($DB['dbName']);
// end header connection part

// function from a functions file that I run a mysql query through in any page.
function executeQuery($sql) {
    $result = mysql_query($sql);
    if (mysql_error()) {
     $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
     if ($_SESSION['auto_id'] == 1) {
      $sql_formatted = highlight_string(stripslashes($sql), true);
      $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
     }
     die($error);
    }
    return $result;
}

// example query ran on anypage of the site using executeQuery funtion
$sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id'];
$result_member=executequery($sql);
if($line_member=mysql_fetch_array($result_member)){
    extract($line_member);
} else {
    header("location: index.php");
    exit;
}
?>
+1  A: 

I would tentatively recommend using PDO for your SQL access.

Then it is only a case of changing the driver and ensuring the SQL works on the new backend. In theory. Data migration is a different issue.

Abstract database access is great.

Aiden Bell
+3  A: 

The first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the mysql API, which is a procediral one.

To help with that, the The MySQLi Extension Function Summary is definitly something that will prove helpful.

For instance :

Note that, for some functions, you may need to check the parameters carefully : maybe there are some differences here and there -- but not that many, I'd say : both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)

For instance :

  • with mysql, you have to use the mysql_select_db once connected, to indicate on which DB you want to do your queries
  • mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect.
  • still, there is also a mysqli_select_db function that you can use, if you prefer.


Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)

Pascal MARTIN
Thanks, I wasn't sure if I needed to do something else, seemed to simple
jasondavis
As long as you want to keep procedural code, switching from mysql to mysqli should not be too hard ; but, if your codebase is not too big (ie, going from one API to the other wouldn't mean too much work), you might want to use an object-oriented API ; it'll require more work, though... And if going for a total rewrite to an OO-API, I'd go with PDO instead of mysqli...
Pascal MARTIN
I mean reading up on the 2 all day now, from what I understand isn't the main difference with PDO, that PDO allows you to use a differnt DB system. IF that is the only benefit then it would not be much help in my case as I will never need a different one for this application.
jasondavis
In this case, mysqli will probably be just fine :-) especially as you already have some code built on mysql.
Pascal MARTIN
+1  A: 

if you want to keep the procedural programming, just add an "i" after mysql in every mysql_* function. otherwise check the manual for more details http://www.php.net/manual/en/book.mysqli.php

I do recommend you using PDO too. I use codeigniter to abstract my database access

w35l3y