Lambneck Posted May 26, 2009 Share Posted May 26, 2009 Can anyone see what the problem is in this short script? I get the "else" message: "That record does not exist!" However the info is in fact in the database, so there is something wrong (Im assuming in the query) that I cant find. <?php if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); $sql = "SELECT * FROM $table ORDER BY id DESC LIMIT 25"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if(mysql_num_rows($result) == 1){ $row = mysql_fetch_array($result); $postTime = strtotime("-2 hours"); $diffArray = timeDiff($postTime); echo '<div id="front_page_post_container">'; echo '<div id="title_container">'; echo '<h2>'; echo '<a href="blorum-topic.php?id='.$row['id'].'">'.stripslashes($row['title']).'</a>'; echo '</h2>'; echo '</div>'; echo '<div id="post_container">'; echo stripslashes(nl2br(substr($row['post'],0,450))); echo ' <a href="blorum-topic.php?id='.$row['id'].'">Read on...</a>'; echo '</div>'; echo '<div id="name_date_container">'; print "<pre>"; print_r($diffArray); print "</pre>"; echo '</div>'; echo '</div>'; }else{ echo "That record does not exist!"; } Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/ Share on other sites More sharing options...
kickstart Posted May 26, 2009 Share Posted May 26, 2009 Hi If there are more than 1 matching records (the LIMIT 25 suggests this is likely) then if(mysql_num_rows($result) == 1) will evaluate to false. All the best Keith Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842515 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 Thanks, but the records are ordered by an incrementing id number so shouldn't be the same. Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842520 Share on other sites More sharing options...
gevans Posted May 26, 2009 Share Posted May 26, 2009 Your query will return every row in that table, you're not doing any filtering. So if you have more than one row in the database, irrelevant of its content it will count higher than 1 Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842522 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 i want it to return all info from each of the most recent 25 rows. Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842531 Share on other sites More sharing options...
kickstart Posted May 26, 2009 Share Posted May 26, 2009 Hi In which case simply change it to if(mysql_num_rows($result) >= 1), but then you need to loop through them all. Something like this:- <?php if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); $sql = "SELECT * FROM $table ORDER BY id DESC LIMIT 25"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if(mysql_num_rows($result) == 1) { $postTime = strtotime("-2 hours"); $diffArray = timeDiff($postTime); echo '<div id="front_page_post_container">'; while($row = mysql_fetch_array($result)) { echo '<div id="title_container">'; echo '<h2>'; echo '<a href="blorum-topic.php?id='.$row['id'].'">'.stripslashes($row['title']).'</a>'; echo '</h2>'; echo '</div>'; echo '<div id="post_container">'; echo stripslashes(nl2br(substr($row['post'],0,450))); echo ' <a href="blorum-topic.php?id='.$row['id'].'">Read on...</a>'; echo '</div>'; echo '<div id="name_date_container">'; print "<pre>"; print_r($diffArray); print "</pre>"; echo '</div>'; } echo '</div>'; } else { echo "That record does not exist!"; } ?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842538 Share on other sites More sharing options...
Lambneck Posted May 26, 2009 Author Share Posted May 26, 2009 Ah, I'm still getting the same problem: "That record does not exist!" Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842551 Share on other sites More sharing options...
kickstart Posted May 26, 2009 Share Posted May 26, 2009 Hi Probably my fault. Having mentioned using >= I then forgot to put that change in the code snippet. All the best Keith Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842555 Share on other sites More sharing options...
Lambneck Posted May 27, 2009 Author Share Posted May 27, 2009 Ok, great! thanks for the help kickstart! Link to comment https://forums.phpfreaks.com/topic/159733-solved-snippet-invisible-problem/#findComment-842798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.