简体   繁体   中英

can mysql_fetch_array get mysql_query as argument?

can I use mysql_fetch_array(mysql_query('...')) ?

or is that the only way:

$res = mysql_query($sql); $data = array(); while(($row = mysql_fetch_array($res)) !== false) { $data[] = $row; }

NOTE that I'm expecting only ONE row/result

You can, yes (if you expect only one row returned), but you probably shouldn't .

It is important to check the success or failure of mysql_query() before attempting to fetch a result from it. If mysql_query() fails, it will pass FALSE to mysql_fetch_array() , and that will result it in an error like

 mysql_fetch_array() expects parameter 1 to be resource, boolean given..

It is much safer to assign the output of mysql_query() to a variable which is a result resource, and test its success or failure.

In any case you cannot do mysql_fetch_array(mysql_query(...)) if you expect more than one result returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM