17

Can someone please tell me why this doesnt work, when I tried to switch my old sql to sqli:

$query = "SELECT * FROM `product_category`";
$result = mysql_query($query, $connect) or die("could not perform query: " . mysql_error());
$num_rows = mysql_num_rows($result);

for ($i=0; $i < $num_rows; $i++)
{
    $ID = mysql_result($result,$i,"ID");
    $name = mysql_result($result,$i,"name");
    $description = mysql_result($result,$i,"description");

to:

$query = ("SELECT * FROM `product_category`");
$result = mysqli_query($connect, $query) or die("could not perform query");
$num_rows = mysqli_num_rows($result);

for ($i=0; $i < $num_rows; $i++)
{
    $ID = mysqli_result($result, "ID");
    $name = mysqli_result($result,$i,"name");
    $description = mysqli_result($result,$i,"description");`

it keeps giving me an error of: "Fatal error: Call to undefined function mysqli_result()"

Cesarg2199
  • 569
  • 1
  • 6
  • 18
  • Where's your call to `mysqli_error()`? You need to pass the connection variable into it as a parameter. – andrewsi Jul 17 '13 at 18:24
  • 4
    There is no `mysqli_result()` function. You could supply your own, to emulate the behavior of the `mysql_result()` function, but you don't really want to do that. Better to go with the newer model, and just use a call to the `mysqli_fetch_assoc()` function. And there's no need to get the count of rows, and a for loop. Just use a while loop. The `mysqli_fetch_assoc()` function will return FALSE when there are no more rows to fetch, exiting you from the loop. See the answer from Marc B. – spencer7593 Jul 17 '13 at 18:31
  • So annoying when you're using an open source project and they're only selecting one value from one column and you have to fix it because it's broken everywhere..... Damn you Salazar! – Brain2000 Sep 09 '20 at 14:46

2 Answers2

23

Don't use this kind of code. It's highly inefficient. Use mysqli_fetch_assoc() instead:

while($row = mysqli_fetch_assoc($result)) {
   $id = $row['ID'];
   $name = $row['name']; 
   etc..
}

One SINGLE database operation, rather than the 3+ you're trying to do.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 6
    +1. To actually answer the first question the OP posed, "tell me why this doesn't work"... the reason the code in the question doesn't work is that it makes calls to a non-existent `mysqli_result()` function. That function does not exist. – spencer7593 Jul 17 '13 at 18:35
  • yep, because it was a moronic function in the mysql library to begin with. – Marc B Jul 17 '13 at 18:37
  • 3
    Hey now! :) Some of us moronics just happen to like a moronic function we can use, widdout us havin's to understand all dem highfalutin hijinx dat happens behind da curtains. I just want my moronic field value! (i kid, i kid) (I have seen an implementation of a `mysqli_result` function ). – spencer7593 Jul 17 '13 at 18:41
  • Yes, excellent circumscription by another mysqli function. This is the way to go. – mtjmohr Dec 08 '19 at 16:50
6
if (!function_exists('mysqli_result')) {
  function mysqli_result($res, $row, $field=0) {
    $res->data_seek($row);
    $datarow = $res->fetch_array();
    return $datarow[$field];
  }
}

You can create this function.

Code4R7
  • 2,600
  • 1
  • 19
  • 42
Roman Panevnyk
  • 313
  • 3
  • 7
  • This is a helpful answer when you need to get old code up and running again. You can use [`function_exists()`](https://www.php.net/manual/en/function.function-exists.php) to create the function only when it is needed. – Code4R7 Oct 27 '19 at 14:21
  • Very good way to deal with older code which needs to be brought up to more modern mysqli standards but lack the rescription of code the very good and only working way Marc B has shown here. – mtjmohr Dec 08 '19 at 16:51