This is a discussion on Check if result set is empty within the PHP Development forums, part of the Programming & Scripting category; hi everyone starting to get into PHP, and I have trouble checking if a result set is empty. I am ...
| |||||||
|
#1
| |||
| |||
| hi everyone starting to get into PHP, and I have trouble checking if a result set is empty. I am doing it like this: Code: $result = mysql_query($query);
if (mysql_fetch_array($result, MYSQL_ASSOC) == False) print "no records found";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) //carry on looping through while there are records
{
while loop will skip the first record. this is my first day with php, so be gentle |
|
#2
| ||||
| ||||
| you can try: Code: If (!$result) {
...code...
}
Else {
...code...
}
here's an example of some of my code: Code: //get results of content query
Public Function ResultsContent($GetAll) {
//open conn to db
$this->OpenDB();
//get results
$result = mysql_query($GetAll);
$num_rows = mysql_num_rows($result);
If($num_rows == 0) {
$this->arrImage[] = array
(
'Data'=>array
(
'ID'=>0,
)
);
}
Else {
while ($row = mysql_fetch_object($result)) {
$this->arrContent[] = array
(
'Data'=>array
(
'ID'=>$row->idkey,
'Content'=>$row->content,
'Tags'=>$row->tags,
'Timekey'=>$row->timekey,
'Title'=>$row->title,
'Type'=>$row->type,
'Date'=>$row->date
)
);
}
}
//close conn to db
$this->CloseDB();
}
hope that helps some ![]() Shem |
| The Following User Says Thank You to Shem For This Useful Post: | ||
noFriends (June 25th, 2008) | ||
|
#3
| ||||
| ||||
| Hey NF, I'm not well experienced with PHP, either, but I believe you could do something like this: Code: $returned_rows = mysql_num_rows ($Query); Code: if ($returned_rows == 0){
echo 'No records found';
}else{
echo 'There were ' . $returned_rows . 'records found.';
}
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
| The Following User Says Thank You to jmurrayhead For This Useful Post: | ||
noFriends (June 25th, 2008) | ||
|
#4
| |||
| |||
| thanks guys, the num_rows works 100% |
|
#5
| ||||
| ||||
| Did you try (!$result)? I think that should work too Shem |
|
#6
| |||
| |||
| the !result function is used to determine if your sql itself is flawed AFAIK. tried it earlier, didn't work |
|
#7
| ||||
| ||||
| That's right. There's a difference between a query that returns zero rows (which does NOT return a False result, just an empty result set) and a False result (which happens when there's a connection error or a syntax error). The PHP function mysql_num_rows($result) is what you want. It will return 0 or some positive integer. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |