Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if results on while($row = mysql_fetch_array in PHP

Tags:

php

mysql

Im trying to figure out how to handle this is no results are returned, how would I code that?

while($row = mysql_fetch_array($Result))

So like if there a results: print them out

else: show a link

like image 491
jrutter Avatar asked Dec 20 '09 23:12

jrutter


People also ask

What is mysql_fetch_array () function?

mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.

What is the purpose of using mysql_fetch_array () & Mysqli_num_rows ()?

The mysqli_fetch_array() function is used to fetch rows from the database and store them as an array. The array can be fetched as an associative array, as a numeric array or both. Associative arrays are the arrays where the indexes are the names of the individual columns of the table.

What is the difference between mysql_fetch_array () and Ysql_fetch_object ()?

The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

How can fetch data without while loop in PHP?

You can just call mysql_fetch_assoc() (or any similar function) one. Like this: $sql = "SELECT * FROM table"; $row = mysql_fetch_assoc( mysql_query($sql) ); echo $row['title']; This is the easiest and most readable way to do this with the MySQL functions.


4 Answers

http://ca3.php.net/manual/en/function.mysql-num-rows.php

if(mysql_num_rows($result) > 0) {
   while($row = mysql_fetch_array($result)) { ... }
} else {
  // show link
}
like image 84
mpen Avatar answered Oct 05 '22 09:10

mpen


You can use mysql_num_rows() to tell you how many results are found. Using that with a simple if-statement, and you can determine what action to take.

if (mysql_num_rows($result) > 0) {
  // do while loop
} else {
  // show link
}
like image 29
Sampson Avatar answered Oct 05 '22 10:10

Sampson


Others suggest using mysql_num_rows() but you should be aware that that function works only if you use a buffered query. If you query using mysql_unbuffered_query(), the number of rows in the result is not available.

I would use a simple flag variable:

$found_row = false;

while ($row = mysql_fetch_array($result)) {
  $found_row = true;
  . . .
}

if ($found_row == false) {
  // show link
}

It may seem redundant to set $found_row to true repeatedly, but assigning a literal value to a variable ought to be an insignificant expense in any language. Certainly it is small compared to fetching and processing an SQL query result.

like image 22
Bill Karwin Avatar answered Oct 05 '22 09:10

Bill Karwin


Use even shorter syntax without insignificant mysql_num_rows to save processor time:

if($result) {
   // return db results
} else {
   // no result
}
like image 24
Binyamin Avatar answered Oct 05 '22 09:10

Binyamin