Jump to content

[SOLVED] Echoing if there are no results from the DB to display


HoTDaWg

Recommended Posts

how do i echo a statement like "There are no results to display." with a code like this?:

<?php
//connection stuff here....

echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>';
$sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'";
$result = mysql_query($sql,$connection)or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>';
}

echo'</table>';
?>

 

thanks for any help possible

 

HoTDaWg

Link to comment
Share on other sites

You can add a variable that checks how many results were returned from the DB query. If there were no results, then echo a "No results" message. Otherwise, run the while() loop.

 

Something like this:

 

<?php
//connection stuff here....

echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>';
$sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'";
$result = mysql_query($sql,$connection)or die(mysql_error());
$num_results = mysql_num_rows($result);

if($num_results == 0){
echo "No results to display!";
} else{
while($row = mysql_fetch_assoc($result)){
	echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>';
}
]

echo'</table>';
?>

Link to comment
Share on other sites

weird, i am getting the following error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1

heres the updated script:

<?php
//connection stuff

echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>';
$sql = "SELECT id,action,date,read,ip FROM actions WHERE read='nope'";
$result = mysql_query($sql,$connection)or die(mysql_error());
$num_results = mysql_num_rows($result);

if($num_results == 0){
echo "No results to display!";
}else{
while($row = mysql_fetch_assoc($result)){
echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>';
}
}

echo'</table>';
?>

and my database:


-- 
-- Table structure for table `actions`
-- 

CREATE TABLE `actions` (
  `id` int(11) NOT NULL auto_increment,
  `action` varchar(255) default NULL,
  `date` varchar(255) default NULL,
  `read` varchar(255) NOT NULL default '',
  `ip` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `actions`
-- 

 

??? whats goin on...

any help would be greatly appreciated thanks

 

HoTDaWg

Link to comment
Share on other sites

You should use

 

if($result == null){

echo "No results to display!";

}else{

// other query here

 

I have found that using a num_rows on top of a while loop throws the num_rows off for some reason that I am unfamiliar with

 

Link to comment
Share on other sites

thanks for your response:)

 

ouch...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1

 

thanks for your help all i have to go to sleep now but i will bring this topic back up tomorrow if u can, post a reply today and i will check it out asap

 

nightey night and thanks again

HoTDaWg

Link to comment
Share on other sites

geez this is killing me,

 

check it out i think its the same error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read,ip FROM actions WHERE read='nope'' at line 1

 

*sigh* KILLING ME:@ herere is the script again:

<?php
$connection = mysql_connect('localhost','xxxx','XXXXXX');
mysql_select_db('xXXXXXXXX');

echo'<table width="375" height="0" cellpadding="0" cellspacing="0" border="1"><tr><td>Id:</td><td>Action:</td><td>Date:</td><td>Ip:</td></tr><tr>';
$sql = "SELECT id,action,`date`,read,ip FROM actions WHERE read='nope'";
$result = mysql_query($sql,$connection)or die(mysql_error());
$num_results = mysql_num_rows($result);

if($num_results == null){
echo "No results to display!";
}else{
while($row = mysql_fetch_assoc($result)){
echo'<td>'.$row['id'].'</td><td>'.$row['action'].'</td><td>'.$row['date'].'</td><td>'.$row['ip'].'</td></tr><tr>';
}
}

echo'</table>';
?>

thanks again

 

HoTDaWg

Link to comment
Share on other sites

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.