Mysql_fetch_array () alternative

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

+3
source share
3 answers

Alternatives mysql_fetch_array()

You do not need to use a while loop. Using mysql_num_rows(), you can check the number of rows returned. This works on a result set that returns from the call mysql_query().

$res = mysql_query('select 0');
if (mysql_num_rows($res)) {
    $data = mysql_fetch_array($res);
}
+4
source

If you have only one (or zero) line to pull.

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

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


If you just want to find out how many lines you have

$count = mysql_num_rows($result);
+3

, .

function get_one_row($query) {
 $result = mysql_query($query);
 $row = mysql_fetch_array($result);
 return($row);
}  

mysql_fetch_array?

+2

Source: https://habr.com/ru/post/1724070/


All Articles