Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.