Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to mysql_fetch_array()

Tags:

php

mysql

I want to execute a query and either it will return one row or none. I don't want to use mysql_fetch_array(). What else can I do?

like image 471
RKh Avatar asked Jul 27 '26 16:07

RKh


2 Answers

Alternatives to mysql_fetch_array()

  1. mysql_fetch_object()
  2. mysql_fetch_assoc()
  3. mysql_fetch_row()

You don't have to use a while loop. Using mysql_num_rows() you can check the number of returned rows. This works on the resultset that is returned from a mysql_query() call.

$res = mysql_query('select 0');
if (mysql_num_rows($res)) {
    $data = mysql_fetch_array($res);
}
like image 165
Peter Lindqvist Avatar answered Jul 30 '26 06:07

Peter Lindqvist


If you've got only one (or zero) rows to pull.

$result = mysql_query(/* ... */);
$row = mysql_fetch_array($result);
mysql_free_result($result);

If there is a row, $row will have it. If not - $row will be false. No need for while().


If you just want to know how many rows you've got

$count = mysql_num_rows($result);
like image 41
Emil Ivanov Avatar answered Jul 30 '26 06:07

Emil Ivanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!