Jump to content

[SOLVED] This code is not functioning the way I expect it too.


bobleny

Recommended Posts

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?

<?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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.