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.......

Recommended Answers

All 3 Replies

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>";
      }
   }
?>

Thanks for the tutorial link, very helpful indeed.

broj1, thanks for that, it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.