+ Reply to Thread
Results 1 to 4 of 4

Thread: how to ignore empty results from MySQL query in PHP?

  1. #1
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    how to ignore empty results from MySQL query in PHP?

    I'm planning to try a few approaches to pulling out information from a MySQL database using PHP that will break things down into groups according to year - it's for an art gallery archive.

    Each group of results will have the year displayed top left (only once), then on the right the list of artists who exhibited in that year going down.

    So far I have a series of arrays like this:

    HTML Code:
    <div id="archive_text">
    	<?php
    	$sql = 'SELECT * FROM exhibitions WHERE archive="yes" AND year="2009" ORDER BY year DESC, month DESC';
    	$result = mysql_query($sql) or die(mysql_error());
    	?>
    <div id="archive_year"><?php echo $row['year']; ?></div>
    <div id="archive_event">
    	<?php
    	while($row = mysql_fetch_assoc($result)) {
    	?>
    <a href="exhibitions/index.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a><br />
    	<?php } ?>
        </div>
        </div>
    Probably not the most elegant way of doing this, but it's a starting point. My problem is that it isn't showing the year even though there are results.

    Any ideas why?

    I can't hard code the year into each array because I want any year with no results to be ignored - so it doesn't mess up the vertical spacing

    thanks
    Steve

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: how to ignore empty results from MySQL query in PHP?

    Use the mysql_num_rows() to check that there are some results.
    E.g.
    PHP Code:
    if(mysql_num_rows($result)>0){
    echo 
    "<div id='archive_year'>" $row['year'] . "</div>";

    Or something along those lines.

    EDIT: Also, your trying to call the variable $row['year'] before it has been defined by while($row = mysql_fetch_assoc($result)) {
    Last edited by lemon-tree; 05-13-2009 at 04:32 PM.

  3. #3
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    Re: how to ignore empty results from MySQL query in PHP?

    thanks for that, I'd just tried a slightly fudged

    HTML Code:
    <div id="archive_text">
    <?php
    	$year = '2010';
    	$sql = 'SELECT * FROM exhibitions WHERE archive="yes" AND year="2010" ORDER BY year DESC, month DESC';
    	$result = mysql_query($sql) or die(mysql_error());
    
    if(mysql_num_rows($result)>0){ 
    echo '<div id="archive_year">' . $year . '</div>'; 
    } 
    ?>
    <div id="archive_event">
    	<?php
    	while($row = mysql_fetch_assoc($result)) {
    	?>
    <a href="exhibitions/index.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a><br />
    	<?php } ?>
        </div>
    	</div>
    but obviously if I implement the year after it's been defined then I can get rid of the $year quick fix

    nice one!
    Last edited by stevet70; 05-13-2009 at 04:59 PM.

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: how to ignore empty results from MySQL query in PHP?

    Here is a updated version for you
    HTML Code:
    <div id="archive_text">
    <?php
    	$year = '2010';
    	$sql = "SELECT * FROM exhibitions WHERE archive='yes' AND year='$year' ORDER BY month DESC, artist DESC';
    	$result = mysql_query($sql) or die(mysql_error());
    
    if(mysql_num_rows($result) > 0){ 
    echo '<div id="archive_year">' . $year . '</div>';
    ?>
    	<div id="archive_event">
    	<?php while($row = mysql_fetch_assoc($result)) { ?>
    	<a href="exhibitions/index.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a><br />
    	<?php } ?>
        </div>
    </div>
    <?php } ?>
    I've changed the SQL part, as you don't need to go order the year, since there is a WHERE clause specifying the year.
    I've also moved some of your code around in the end so that you don't get a lone </div> if there is no result.
    You should think in separating your code and your layout. It looks way better that way.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  2. Places to learn php
    By JaWasabi in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  3. Also got the PHP MySQL error
    By adfad666 in forum Free Hosting
    Replies: 1
    Last Post: 08-16-2008, 07:52 PM
  4. PHP MySQL Question..
    By anuj_web in forum Programming Help
    Replies: 7
    Last Post: 04-26-2008, 05:43 AM
  5. Php And Mysql Forum Rephrased!
    By maddude in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 04-05-2005, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers