Jump to content

[SOLVED] Problem if there's not enough entries in the Database - simple if you know how?


spikypunker

Recommended Posts

Hi guys, am slowly finding my way into the php world. I've managed to build an events sidebar, that displays 4 entries from the events database and then links to a page displaying details. The only problem is, currently the code goes haywire if there's not 4 entries in the database? Is there a simple fix?

 

<?php
                        	mysql_connect ("#","#");
						@mysql_select_db("#") or die ("unable to connect");
						$query = "SELECT * FROM events ORDER BY date DESC ";
						$result = mysql_query($query); 
						$num = mysql_num_rows($result);
						mysql_close();

						$i = 0;
						while ($i < 4)

						{ 

						$date = mysql_result($result,$i,"date");
						$title = mysql_result($result,$i,"title");
						$id = mysql_result($result,$i,"id");

						if(strlen($title) > 20){
    						$title = substr($title,0,20).'...';}

						print date("d.m.Y",strtotime("$date"));
						echo " <a href='eventitem.php?id=$id'>$title</a><br>";

						$i++;

						}
						?>

 

Cheers dudes! Chris

Hi Chris,

 

Use the MySQL LIMIT function rather than use a PHP while loop.

 

Amended code below:

 

<?php
                        	mysql_connect ("#","#");
						@mysql_select_db("#") or die ("unable to connect");
						$query = "SELECT * FROM events ORDER BY date DESC LIMIT4";
						$result = mysql_query($query); 
						mysql_close();

						while ($a = mysql_fetch_array($query))

						{ 

						$date = $a['date'];
						$title = $a['title'];
						$id = $a['id'];

						if(strlen($title) > 20){
    						$title = substr($title,0,20).'...';}

						print date("d.m.Y",strtotime("$date"));
						echo " <a href='eventitem.php?id=$id'>$title</a><br>";


						}
						?>

 

Have a look here for more information on MySQL LIMIT.

 

Hope this helps.

This right?

 

<?php
                           mysql_connect ("#","#");
                     @mysql_select_db("#") or die ("unable to connect");
                     $query = "SELECT * FROM events ORDER BY date DESC LIMIT4";
                     $result = mysql_query($query); 
                     mysql_close();

                     while ($a = mysql_fetch_array($query) && $ < 4)

                     { 
               
                     $date = $a['date'];
                     $title = $a['title'];
                     $id = $a['id'];
                     
                     if(strlen($title) > 20){
                      $title = substr($title,0,20).'...';}
                     
                     print date("d.m.Y",strtotime("$date"));
                     echo " <a href='eventitem.php?id=$id'>$title</a><br>";

                     
                     }
                     ?>

Hi

 

Need a space between the limit and the 4.

 

And no point in trying to check the count in the while loop

 

<?php
mysql_connect ("#","#");
@mysql_select_db("#") or die ("unable to connect");
$query = "SELECT * FROM events ORDER BY date DESC LIMIT 4";
$result = mysql_query($query); 
mysql_close();

while ($a = mysql_fetch_array($query))
{ 
$date = $a['date'];
$title = $a['title'];
$id = $a['id'];
if(strlen($title) > 20)
{
	$title = substr($title,0,20).'...';
}
print date("d.m.Y",strtotime("$date"));
echo " <a href='eventitem.php?id=$id'>$title</a><br>";
}
?>

 

All the best

 

Keith

I'm getting an error :(

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage21/th/eb/oo/thebookhive.co.uk/public_html/aboutus.php on line 72

Hi spikypunker,

 

try:

 

<?php
mysql_connect ("#","#");
@mysql_select_db("#") or die ("unable to connect");
$query = mysql_query("SELECT * FROM events ORDER BY date DESC LIMIT 4");

while ($a = mysql_fetch_array($query))
{ 

$date = $a['date'];
$title = $a['title'];
$id = $a['id'];

if(strlen($title) > 20){
$title = substr($title,0,20).'...';}

print date("d.m.Y",strtotime("$date"));
echo " <a href='eventitem.php?id=$id'>$title</a><br>";

}
?>

 

I've removed both mysql_close() and the $result= line and amended the MySQL Query.

 

Hope this helps.

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.