0

I've been trying to use mysqli instead of mysql.
I have a file from another site which will fit nicely to this site im doing now, but i havent a clue what i need to do to convert it to mysqli.

This is the file

<?php
   include 'connect.php';
   //populate form dropdown box
   $op = '';
   $r = mysql_query("SELECT cat_id, category FROM categories ORDER BY cat_id");
   if (mysql_num_rows($r)){
      while ($d = mysql_fetch_assoc($r)){
         $op .= "\n\t<option value='{$d['cat_id']}'>{$d['category']}</option>";
      }
   }
?>

Any help would be appreciated.......

3
Contributors
3
Replies
22
Views
4 Years
Discussion Span
Last Post by GlenRogers
0

Unfortunately it is not enough just change mysql_* commands to mysqli_*. There are minor differences. For example mysql_query takes first parameter the query and optional second the link while mysqli_query takes the first parameter link (must be present) and second the query. I suggest you check the syntax of each command on php.net. And be aware that mysqli_* has and object oriented API as well as procedural. You probably want to start with procedural.

So in your case:

<?php
   include 'connect.php'; // assuming that link identifier is stored in $link
   //populate form dropdown box
   $op = '';
   $r = mysqli_query($link, "SELECT cat_id, category FROM categories ORDER BY cat_id");
   if (mysqli_num_rows($r)){
      while ($d = mysqli_fetch_assoc($r)){
         $op .= "\n\t<option value='{$d['cat_id']}'>{$d['category']}</option>";
      }
   }
?>

Edited by broj1

0

Thanks for the tutorial link, very helpful indeed.

broj1, thanks for that, it worked.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.