bobleny Posted May 31, 2007 Share Posted May 31, 2007 The idea of this segment of code is to display the links that are stored in the database. If there are no links in there, echo "No Links". For some reason, it echos "No Links" regardless of whether or not there is links in the database. It will display the links, then it will display "No Links". <?php $sql = "SELECT * FROM `links` WHERE `page` = '" . $page . "'"; $query = mysql_query($sql); if(!$query) { $_SESSION['error_message'] = mysql_error(); $_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__; mysql_close(); sendem(error, .1); die(); } while($fetch = mysql_fetch_assoc($query)) { echo "<div class='related_link'><a href='" . $fetch['href'] . "' class='related'>" . $fetch['name'] . "</a></div>"; } if (!$fetch) { echo "<div class='text_lable'>No Links</div>"; } ?> I'm not sure how to fix this. What do you think? Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/ Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 <?php $sql = "SELECT * FROM `links` WHERE `page` = '" . $page . "'"; $query = mysql_query($sql); if(!$query) { $_SESSION['error_message'] = mysql_error(); $_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__; mysql_close(); sendem(error, .1); die(); } $links = false; while($fetch = mysql_fetch_assoc($query)) { echo "<div class='related_link'><a href='" . $fetch['href'] . "' class='related'>" . $fetch['name'] . "</a></div>"; $links = true; } if (!$links) { echo "<div class='text_lable'>No Links</div>"; } ?> Should take care of the problem. Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265760 Share on other sites More sharing options...
pocobueno1388 Posted May 31, 2007 Share Posted May 31, 2007 Scratch what I just said...it won't work. Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265761 Share on other sites More sharing options...
bobleny Posted May 31, 2007 Author Share Posted May 31, 2007 pocobueno1388 Why not? If there are no rows in the table, then there is logically no links in the table... Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265778 Share on other sites More sharing options...
pocobueno1388 Posted May 31, 2007 Share Posted May 31, 2007 Because I was typing the code as if you had an IF statement...if you want I can add it in to work with your code, but I am sure Frost's code will work just fine, that is the reason I didn't bother re-doing it. Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265781 Share on other sites More sharing options...
bobleny Posted May 31, 2007 Author Share Posted May 31, 2007 pocobueno1388 Actually, I am using what you posted, and I do have it as an if statement. Works fine. I tried frost110 idea yesterday and it worked, but I didn't like it; It seems wrong..... Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265814 Share on other sites More sharing options...
pocobueno1388 Posted May 31, 2007 Share Posted May 31, 2007 Oh, okay then...maybe I just looked at it wrong the first time, hah. Glad it worked for you =] Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265819 Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 It's not wrong, just another way of doing things, this might work better for you (stealin poco's thunder) <?php $sql = "SELECT * FROM `links` WHERE `page` = '" . $page . "'"; $query = mysql_query($sql); if(!$query) { $_SESSION['error_message'] = mysql_error(); $_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__; mysql_close(); sendem(error, .1); die(); }elseif (mysql_num_rows($query) > 0) { while($fetch = mysql_fetch_assoc($query)) { echo "<div class='related_link'><a href='" . $fetch['href'] . "' class='related'>" . $fetch['name'] . "</a></div>"; } }else { echo "<div class='text_lable'>No Links</div>"; } ?> In the programming world there is more than one way to skin a cat. Link to comment https://forums.phpfreaks.com/topic/53766-solved-this-code-is-not-functioning-the-way-i-expect-it-too/#findComment-265861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.