Jump to content

php and mysql no results message


light-angel

Recommended Posts

this is my code but i want to have a pice so if their is no results the it will put in a message like "No Results"

but i cant figger it out

 

 

<?php

$host="localhost";

$username="xxxxx";

$password="xxxxxx";

$db_name="xxxxxxx";

$tbl_name="Shows";

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name WHERE Month='April'and Year='2011' order by Day asc ";

$result=mysql_query($sql);

 

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

<td>

<table width="400" border="1" cellspacing="0" cellpadding="3">

 

<?php

while($rows=mysql_fetch_array($result)){

?>

 

<span class="elevenbold"><? echo $rows['Day']; ?> <? echo $rows['Month']; ?> <br><strong class="elevenboldblue"><? echo $rows['Song']; ?></strong><br><? echo $rows['Data']; ?></span><br><br>

 

 

<?php

}

?>

</table>

</td>

</tr>

</table>

<?php

mysql_close();

?>

Link to comment
Share on other sites

Try this:

 

<?php
$host="localhost"; 
$username="xxxxx"; 
$password="xxxxxx"; 
$db_name="xxxxxxx"; 
$tbl_name="Shows"; 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE Month='April'and Year='2011' order by Day asc ";
$result=mysql_query($sql);

echo '
<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <table width="400" border="1" cellspacing="0" cellpadding="3">';

if (mysql_num_rows($result) > 0)
    while($rows=mysql_fetch_array($result)) 
        echo '<tr><td><span class="elevenbold">'.$rows['Day'].' '.$rows['Month'].'<br /><strong class="elevenboldblue">'.$rows['Song'].'</strong><br />'.$rows['Data'].'</span><br /><br /></td></tr>';
else
    echo '<tr><td>No Results</td></tr>';

echo '
            </table>
        </td>
    </tr>
</table>';

mysql_close();
?>

 

You should really look at your html structure as well as it is not laid out well and you will run into problem when you are trying to do more complex pages.

Link to comment
Share on other sites

I personally find that mysql_num_rows can be quite slow sometimes, especially when you are fetching hundreds of rows at a time (which I do frequently).. 

 

So option, adds a few lines of code.

 

First off I add SQL_CALC_FOUND_ROWS into my query so:

<?php $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table"; ?>

 

Then to retrieve that I add a minimum of 3 lines of code.

 

$_query = "SELECT FOUND_ROWS() as total";
        	$_result = mysql_query($_query);
      		$_row = mysql_fetch_array($_result, MYSQL_ASSOC);

 

I tend to do my select statement outside of mysql_query just for my own ease of reading later.  With this code I would then access count by using: $_row['total']

 

With that you'd then use the same type of if statement as in anti-moronic has, only using $_row['total'] instead of mysql_num_rows

Link to comment
Share on other sites

I personally find that mysql_num_rows can be quite slow sometimes, especially when you are fetching hundreds of rows at a time (which I do frequently).. 

 

So option, adds a few lines of code.

 

First off I add SQL_CALC_FOUND_ROWS into my query so:

<?php $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table"; ?>

 

Then to retrieve that I add a minimum of 3 lines of code.

 

$_query = "SELECT FOUND_ROWS() as total";
        	$_result = mysql_query($_query);
      		$_row = mysql_fetch_array($_result, MYSQL_ASSOC);

 

I tend to do my select statement outside of mysql_query just for my own ease of reading later.  With this code I would then access count by using: $_row['total']

 

With that you'd then use the same type of if statement as in anti-moronic has, only using $_row['total'] instead of mysql_num_rows

 

Interesting, didn't know that! Thanks for info.

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.