开发者

alternative to mysql_fetch_array()

开发者 https://www.devze.com 2022-12-12 11:05 出处:网络
I want to execute a query and either it will return one row or none. I d开发者_如何学Goon\'t want to use mysql_fetch_array(). What else can I do?Alternatives to mysql_fetch_array()

I want to execute a query and either it will return one row or none. I d开发者_如何学Goon't want to use mysql_fetch_array(). What else can I do?


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);
}


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);


You could write a function to do it for you.

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

Or are you trying to eschew mysql_fetch_array entirely?

0

精彩评论

暂无评论...
验证码 换一张
取 消