#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    France
    Posts
    205
    Rep Power
    18

    Replacing MYSQL with MYSQLi


    Hi,

    It's been a while since I did any PHP and I'm trying to catchup with how the langauge has developed. On the subject of MySql connections I see quite a lot has changed:

    I used to use this

    PHP Code:

    \\make the connection
    $db 
    mysql_connect($hostname$username$password);
    mysql_select_db($databasename$db);

    $result=mysql("$databasename","SELECT * FROM tablename");
    if (
    $row mysql_fetch_array($result)) 
        {
            do  
            { 
            
    $table_id = ($row["table_id"]);
            
    $table_row1 = ($row["table_row1"]);
            echo 
    "table_id $table_row1 <br>;
            } 
            while (
    $row = mysql_fetch_array($result)); 
        } 
    else 
        { 
        echo "
    no entries";
        } 
    Now it seems I use this

    PHP Code:
    \\make the connection

        $connection 
    = new mysqli($server$dbusername$dbpassword$db_name)
                    or die(
    mysql_error());
                    
          if (
    $connection->connect_error) {
        die(
    "ERROR: Unable to connect: " $connection->connect_error);
      } 

    $result $connection->query("SELECT * FROM tablename");
    $rows mysqli_num_rows($result); 
    if (
    $rows
    {     
        while (
    $row mysqli_fetch_array($result)) 
        {         
        
    $table_id = ($row["table_ide"]);
        
    $table_row1 = ($row["table_row1"]);
        echo 
    "table_id $table_row1 <br>;
        } 

    else 
        { 
        echo "
    no entries";
        }
      
    $result->close();
      
    $connection->close(); 
    I was wondering why the While/Do loop no longer seems to work in the same way, or how would I write is now using mysqli?

    I this new code the best way to replace the old?

    Thanks in advance for any advice or tips on this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    France
    Posts
    205
    Rep Power
    18
    I think I answered my own question. Is this the closest?

    PHP Code:

    \\make the connection
      
      $result 
    $connection->query("SELECT * FROM tablename");
      if (
    $row mysqli_fetch_array($result)) 
        {
            do  
            { 
        
    $table_id = ($row["table_ide"]);
        
    $table_row1 = ($row["table_row1"]);
        echo 
    "table_id $table_row1 <br>";    
            } 
            while (
    $row mysqli_fetch_array($result)); 
        } 
    Thanks for any comments
  4. #3
  5. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,915
    Rep Power
    9646
    The vast majority of the mysqli API is the same as the mysql API's. Translating between the two is mostly a matter of adding some 'i's and moving the $connection argument to the right position.

    What you're looking at is the "new" object-oriented style. Well, a hybrid of the two. If it helps you make the transition then you don't have to use it - you can continue with the procedural mysqli_* functions (as each object-oriented call has a normal function version too).

    So
    PHP Code:
    // make the connection 
    $db mysql_connect($hostname$username$password);
    mysql_select_db($databasename$db);

    $result=mysql_query("SELECT * FROM tablename"$db);
    if (
    $row mysql_fetch_array($result))
        {
            do
            {
            
    $table_id = ($row["table_id"]);
            
    $table_row1 = ($row["table_row1"]);
            echo 
    "$table_id $table_row1 <br>";
            }
            while (
    $row mysql_fetch_array($result));
        }
    else
        {
        echo 
    "no entries";
        } 
    becomes
    PHP Code:
    // make the connection 
    $db mysqli_connect($hostname$username$password);
    mysqli_select_db($db$databasename);

    $result=mysqli_query($db"SELECT * FROM tablename");
    if (
    $row mysqli_fetch_array($result))
        {
            do
            {
            
    $table_id = ($row["table_id"]);
            
    $table_row1 = ($row["table_row1"]);
            echo 
    "$table_id $table_row1 <br>";
            }
            while (
    $row mysqli_fetch_array($result));
        }
    else
        {
        echo 
    "no entries";
        } 
    Besides the obvious function name changes, the $db argument moves to the first position.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    France
    Posts
    205
    Rep Power
    18
    That is actually really useful in explaining the difference and how I can adjust from what I used to do to what I need to get used to doing now. Thanks very much for taking the time to explain.

IMN logo majestic logo threadwatch logo seochat tools logo