NiTx Posted August 28, 2012 Share Posted August 28, 2012 Hi, I'm trying to execute a while loop which by itself works fine. But I have no idea how to echo an error message if the while loop fails when there is nothing in the database. This is what I've tried so far: if($sel_brand) { $device_set = get_devices_by_brand($sel_brand['id']); if($device_set) { echo "error"; } else { while($devices = mysql_fetch_array($device_set)) { echo "<tr><td class=\"video_description\">" . "<a href=\"edit_device.php?device=" . urlencode($devices["id"]) . "\">" . "{$devices['dev_model']}" . " " . "{$devices['dev_version']}" . "</a></td></tr>"; } //end while loop }//end if($device_set) }//end if($sel_brand) I can see why my code isn't working, but I also know I cant execute my error code in the while loop. I'm really stuck here guys, if you could help that would be great :-) Link to comment https://forums.phpfreaks.com/topic/267720-execute-error-message-if-while-loop-fails/ Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 Are you sure this is what you want to do? if($device_set) { echo "error"; } Link to comment https://forums.phpfreaks.com/topic/267720-execute-error-message-if-while-loop-fails/#findComment-1373418 Share on other sites More sharing options...
NiTx Posted August 28, 2012 Author Share Posted August 28, 2012 Sorry my mistake. It's meant to be if(!$device_set) { echo "error"; } This part of the code was a shot in the dark. I didn't think it would work because I should really be testing $devices, but like I said, I can't execute the code inside my while loop. There's probably a really simple solution (I've tried to google it) but I cant figure it out. Essentially I just want to echo an error if there are no devices added in my database for any particular brand. Link to comment https://forums.phpfreaks.com/topic/267720-execute-error-message-if-while-loop-fails/#findComment-1373423 Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 if($device_set) { echo "error"; } elseif( mysql_num_rows($device_set) < 1 ) { echo "no devices"; } else { ... Link to comment https://forums.phpfreaks.com/topic/267720-execute-error-message-if-while-loop-fails/#findComment-1373429 Share on other sites More sharing options...
NiTx Posted August 28, 2012 Author Share Posted August 28, 2012 Ahhhh such a simple solution. Thank you so much xyph. I'll keep this in mind for future coding :-) Link to comment https://forums.phpfreaks.com/topic/267720-execute-error-message-if-while-loop-fails/#findComment-1373436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.