Determine if MySQL query is empty without mysql_fetch_array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Determine if MySQL query is empty without mysql_fetch_array?

Post by JAB Creations »

I'm not sure why even this does not work...

Code: Select all

<?php
function index_empty() {echo '<div><p>The section exists, however no pages exist here yet.</p></div>';}
 
while($row1 = mysql_fetch_array($cms->index))
 {
  echo '<div><pre>';
  print_r($row1);
  echo '</pre></div>';
 
  if (empty($row1)) {index_empty();}
  else if ($row1 == '') {index_empty();}
 }
?>
...I can either list the array data fine if there is array data...or nothing. The issue is I want to know if there is nothing so I can tell the user, 'This section exists, there just aren't any pages in it yet!'

The second problem is using mysql_fetch_array more then once leaves the second use of it unable to access the first row of returned data.

...so after conquering some hard code I feel kinda lame being stuck on this. :roll: I've tried empty and count on the query before and after mysql_fetch_array; not sure what else to try at the moment. I'd prefer to determine if the array has data before the while loop...so if the array has twenty rows I'm not checking the array for data...twenty times. Thoughts please?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Eran »

A common approach is to aggregate all found rows into an array and then work with that.

Code: Select all

$rows = array();
while($row = mysql_fetch_array($cms->index)) {
    $rows[] = $row;
}
 
if(!empty($rows) {
 // show rows
} else {
  echo '<div><p>The section exists, however no pages exist here yet.</p></div>';
}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by JAB Creations »

Oh that's awesome! I don't know what else to say other then thanks! :)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Bill H »

Well, I'm not all that sure why putting it into a separate array is all that useful, really. It could be under some circumstances, but if you are just stepping through it once it seems like a lot of memory and work for little gain.

If you just wnat to know if anything was returned you can use mysql_num_rows($cms->index) which will be zero if no results were found. If larger than zero the resultset will still be indexed at the first recordset and ready to step through.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Eran »

He wants to do something with the array if it's not empty, it's not there just to check if rows were returned or not. This is common usage and quite useful since array access and functions is much robust than the while loop
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by johnworf »

hi,

i've got a similar problem with my code...

i want to add in different colors to the rows and the thing works fine when there are records but doesn't when there are none - as i get a table headings...i'm struggling with the code - it keeps breaking down when i modify it - not sure how to integrate the do while loop exactly..

Code: Select all

 
<?php
// technocurve arc 3 php bv block1/3 start
$color1 = "#99cc33";
$color2 = "#ccff99";
$color = $color1;
// technocurve arc 3 php bv block1/3 end
?><?php require_once('../Connections/myconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_myconn, $myconn);
$query_Recordset1 = "SELECT Restaurant, Website, `Description`, URL FROM Alabama WHERE URL = 'http://www.foo-bar.com/Alabama/alabaster.php' ORDER BY Restaurant ASC";
$Recordset1 = mysql_query($query_Recordset1, $myconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Alabaster Restaurants</title>
<meta name="keywords" content="Alabaster Restaurants" />
<meta name="description" content="Find a restaurant inAlabaster;the US state of Alabama Why not add your restaurant to the list?" />
<link href="../style/usa.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- start header -->
<div id="header">
    <div id="logo">
      <h1><a href="#">foo-bar.com</a></h1>
  </div>
<div id="menu">
        <ul>
            <li><a href="http://www.foo-bar.com/index.html">Home</a></li>
            <li><a href="http://www.foo-bar.com/restaurant-articles/index.php">Restaurant Articles</a></li>
            <li><a href="http://www.foo-bar.com/recipes/index.php">Recipes</a></li>
        </ul>
    </div>
</div>
<div id="headerbg" align="center"><span style="white-space: nowrap" margin-top: "20px"><img src="../state-images/alabama.jpg" alt="state of alabama"><img src="../recipes/images/main-meals.jpg" width="450" height="209" /></span></div>
<div id="page">
    <!-- start content -->
    <div id="content">
        <!-- start latest-post -->
        <div id="latest-post" class="post">
            <h1 class="title">Alabaster Restaurants</h1>
<div class="entry">
 <table width="580" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><h2>Restaurant</h2></td>
            <td><h2>Description</h2></td>
            <td><h2>Report</h2></td>
          </tr>
          <?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foo-bar.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
        </table>
        <p>&nbsp;</p>
        <p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>
  </div>
      </div>
    <!-- end latest-post -->
        <!-- start recent-posts -->
        <div id="recent-posts">
            <div class="post">
                <h2 class="title">Restaurant Articles</h2>
                <p>Buying A Restaurant</p>
              <div class="entry">
            <p><a href="../restaurant-articles/buying-a-restaurant.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Recipe</h2>
                <p>Cajun Chicken Wings</p>
              <div class="entry">
            <p><a href="http://www.foo-bar.com/recipes/cajun-chicken-wings.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Business Plan</h2>
                <p>Seafood Restaurant</p>
              <div class="entry">
            <p><a href="http://www.foo-bar2.com/business-plans/Seafood-Restaurant-Business-Plan.html" class="more">Read more</a></p>
              </div>
            </div>
            </div>
        <!-- end recent-posts -->
    </div>
    <!-- end content -->
    <!-- start sidebar -->
    <div id="sidebar">
        <ul>
            <li>
                <ul>
                    <li><a href="http://www.foo-bar.com/Alabama/index.php">Alabama</a></li>
                    <li><a href="http://www.foo-bar.com/Alaska/index.php">Alaska</a></li>
                    <li><a href="http://www.foo-bar.com/Arizona/index.php">Arizona</a></li>
                    <li><a href="http://www.foo-bar.com/Arkansas/index.php">Arkansas</a></li>
                    <li><a href="http://www.foo-bar.com/California/index.php">California</a></li>
                    <li><a href="http://www.foo-bar.com/Colorado/index.php">Colorado</a></li>
                    <li><a href="http://www.foo-bar.com/Connecticut/index.php">Connecticut</a></li>
                    <li><a href="http://www.foo-bar.com/Delaware/index.php">Delaware</a></li>
                    <li><a href="http://www.foo-bar.com/Florida/index.php">Florida</a></li>
                    <li><a href="http://www.foo-bar.com/Georgia/index.php">Georgia</a></li>
                    <li><a href="http://www.foo-bar.com/Hawaii/index.php">Hawaii</a></li>
                    <li><a href="http://www.foo-bar.com/Idaho/index.php">Idaho</a></li>
                    <li><a href="http://www.foo-bar.com/Illinois/index.php">Illinois</a></li>
                </ul>
            </li>
            <li>
          <ul>
                    
                    <li><a href="http://www.foo-bar.com/Indiana/index.php">Indiana</a></li>
                    <li><a href="http://www.foo-bar.com/Iowa/index.php">Iowa</a></li>
                    <li><a href="http://www.foo-bar.com/Kansas/index.php">Kansas</a></li>
                    <li><a href="http://www.foo-bar.com/Kentucky/index.php">Kentucky</a></li>
                    <li><a href="http://www.foo-bar.com/Louisiana/index.php">Louisiana</a></li>
                    <li><a href="http://www.foo-bar.com/Maine/index.php">Maine</a></li>
                    <li><a href="http://www.foo-bar.com/Maryland/index.php">Maryland</a></li>
                    <li><a href="http://www.foo-bar.com/Massachusetts/index.php">Massachusetts</a></li>
                    <li><a href="http://www.foo-bar.com/Michigan/index.php">Michigan</a></li>
                    <li><a href="http://www.foo-bar.com/Minnesota/index.php">Minnesota</a></li>
                    <li><a href="http://www.foo-bar.com/Mississippi/index.php">Mississippi</a></li>
                    <li><a href="http://www.foo-bar.com/Missouri/index.php">Missouri</a></li>
                    <li><a href="http://www.foo-bar.com/Montana/index.php">Montana</a></li>
              </ul>
            </li>
      <li>
                
                <ul>
                    
                    <li><a href="http://www.foo-bar.com/Nebraska/index.php">Nebraska</a></li>
                    <li><a href="http://www.foo-bar.com/Nevada/index.php">Nevada</a></li>
                    <li><a href="http://www.foo-bar.com/New-Hampshire/index.php">New Hampshire</a></li>
                    <li><a href="http://www.foo-bar.com/New-Jersey/index.php">New Jersey</a></li>
                    <li><a href="http://www.foo-bar.com/New-Mexico/index.php">New Mexico</a></li>
                    <li><a href="http://www.foo-bar.com/New-York/index.php">New York</a></li>
                    <li><a href="http://www.foo-bar.com/North-Carolina/index.php">North Carolina</a></li>
                    <li><a href="http://www.foo-bar.com/North-Dakota/index.php">North Dakota</a></li>
                    <li><a href="http://www.foo-bar.com/Ohio/index.php">Ohio</a></li>
                    <li><a href="http://www.foo-bar.com/Oklahoma/index.php">Oklahoma</a></li>
                    <li><a href="http://www.foo-bar.com/Oregon/index.php">Oregon</a></li>
                    <li><a href="http://www.foo-bar.com/Pennsylvania/index.php">Pennsylvania</a></li>
                </ul>
          </li>
            <li>
              <ul>
                    
                    <li><a href="http://www.foo-bar.com/Rhode-Island/index.php">Rhode Island</a></li>
                    <li><a href="http://www.foo-bar.com/South-Carolina/index.php">South Carolina</a></li>
                    <li><a href="http://www.foo-bar.com/South-Dakota/index.php">South Dakota</a></li>
                    <li><a href="http://www.foo-bar.com/Tennessee/index.php">Tennessee</a></li>
                    <li><a href="http://www.foo-bar.com/Texas/index.php">Texas</a></li>
                    <li><a href="http://www.foo-bar.com/Utah/index.php">Utah</a></li>
                    <li><a href="http://www.foo-bar.com/Vermont/index.php">Vermont</a></li>
                    <li><a href="http://www.foo-bar.com/Virginia/index.php">Virginia</a></li>
                    <li><a href="http://www.foo-bar.com/Washington/index.php">Washington</a></li>
                    <li><a href="http://www.foo-bar.com/West-Virginia/index.php">West Virginia</a></li>
                    <li><a href="http://www.foo-bar.com/Wisconsin/index.php">Wisconsin</a></li>
                    <li><a href="http://www.foo-bar.com/Wyoming/index.php">Wyoming</a></li>
              </ul>
          </li>
        </ul>
          </li>
        </ul>
      <div style="clear: both;">&nbsp;</div>
    </div>
  <!-- end sidebar -->
</div>
<!-- end page -->
<div id="footer">
    <p id="legal">&copy;2008 All Restaurants USA. All Rights Reserved.</p>
  <p id="links"><a href="http://www.foo-bar.com/index.php">Home</a>&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp; <a href="http://www.foo-bar.com/terms.php">Terms</a> &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; <a href="http://www.foo-bar.com/faq.php">FAQ</a> &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="http://www.foo-bar.com/contact.php">Contact Us</a></p>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
 
johnworf
Forum Commoner
Posts: 28
Joined: Fri Nov 02, 2007 1:02 pm

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by johnworf »

Hi,

I'm not sure how i do the if else constraint properly...
i'm getting an error in line 104

Parse error: syntax error, unexpected T_ELSE in /home/allresta/public_html/Alabama/alabaster.php on line 106

Code: Select all

 
<?php
// technocurve arc 3 php bv block1/3 start
$color1 = "#99cc33";
$color2 = "#ccff99";
$color = $color1;
// technocurve arc 3 php bv block1/3 end
?><?php require_once('../Connections/myconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_myconn, $myconn);
$query_Recordset1 = "SELECT Restaurant, Website, `Description`, URL FROM Alabama WHERE URL = 'http://www.foo-bar.com/Alabama/alabaster.php' ORDER BY Restaurant ASC";
$Recordset1 = mysql_query($query_Recordset1, $myconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Alabaster Restaurants</title>
<meta name="keywords" content="Alabaster Restaurants" />
<meta name="description" content="Find a restaurant inAlabaster;the US state of Alabama Why not add your restaurant to the list?" />
<link href="../style/usa.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- start header -->
<div id="header">
    <div id="logo">
      <h1><a href="#">foo-bar.com</a></h1>
  </div>
<div id="menu">
        <ul>
            <li><a href="http://www.foo-bar.com/index.html">Home</a></li>
            <li><a href="http://www.foo-bar.com/restaurant-articles/index.php">Restaurant Articles</a></li>
            <li><a href="http://www.foo-bar.com/recipes/index.php">Recipes</a></li>
        </ul>
    </div>
</div>
<div id="headerbg" align="center"><span style="white-space: nowrap" margin-top: "20px"><img src="../state-images/alabama.jpg" alt="state of alabama"><img src="../recipes/images/main-meals.jpg" width="450" height="209" /></span></div>
<div id="page">
    <!-- start content -->
    <div id="content">
        <!-- start latest-post -->
        <div id="latest-post" class="post">
            <h1 class="title">Alabaster Restaurants</h1>
<div class="entry">
<?php 
if ( $totalRows_Recordset1 > 0 ) {
echo '<table width="580" border="0" cellspacing="0" cellpadding="0"><tr><td><h2>Restaurant</h2></td><td><h2>Description</h2></td><td><h2>Report</h2></td></tr>';?>
 
          <?php do {  ?>
          <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>>
            <td><a href="http://<?php echo $row_Recordset1['Website']; ?>" target="_blank" rel="nofollow"><?php echo $row_Recordset1['Restaurant']; ?></a></td>
            <td><?php echo $row_Recordset1['Description']; ?></td>
            <td><a href="mailto:reportabuse@foo-bar.com?subject=<?php echo $row_Recordset1['URL']; ?>">Report Abuse</a></td>        
          </tr>
          <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
    $color = $color2;
} else {
    $color = $color1;
}
// technocurve arc 3 php bv block3/3 end
?>
 
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<?php echo '</table>'; ?>
<?php echo '<p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>'; ?>;
 
 
<?php } ?>
<?php else {   ?>
        <?php echo '<p>Do you want to <a href="../advertise-your-restaurant.php">advertise your restaurant</a> in the town of Alabaster?</p>'; ?>;
        <?php }?>
  </div>
      </div>
    <!-- end latest-post -->
        <!-- start recent-posts -->
        <div id="recent-posts">
            <div class="post">
                <h2 class="title">Restaurant Articles</h2>
                <p>Buying A Restaurant</p>
              <div class="entry">
            <p><a href="../restaurant-articles/buying-a-restaurant.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Recipe</h2>
                <p>Cajun Chicken Wings</p>
              <div class="entry">
            <p><a href="http://www.foo-bar.com/recipes/cajun-chicken-wings.php" class="more">Read more</a></p>
              </div>
            </div>
            <div class="post">
                <h2 class="title">Business Plan</h2>
                <p>Seafood Restaurant</p>
              <div class="entry">
            <p><a href="http://www.foo-bar2.com/business-plans/Seafood-Restaurant-Business-Plan.html" class="more">Read more</a></p>
              </div>
            </div>
            </div>
        <!-- end recent-posts -->
    </div>
    <!-- end content -->
    <!-- start sidebar -->
    <div id="sidebar">
        <ul>
            <li>
                <ul>
                    <li><a href="http://www.foo-bar.com/Alabama/index.php">Alabama</a></li>
                    <li><a href="http://www.foo-bar.com/Alaska/index.php">Alaska</a></li>
                    <li><a href="http://www.foo-bar.com/Arizona/index.php">Arizona</a></li>
                    <li><a href="http://www.foo-bar.com/Arkansas/index.php">Arkansas</a></li>
                    <li><a href="http://www.foo-bar.com/California/index.php">California</a></li>
                    <li><a href="http://www.foo-bar.com/Colorado/index.php">Colorado</a></li>
                    <li><a href="http://www.foo-bar.com/Connecticut/index.php">Connecticut</a></li>
                    <li><a href="http://www.foo-bar.com/Delaware/index.php">Delaware</a></li>
                    <li><a href="http://www.foo-bar.com/Florida/index.php">Florida</a></li>
                    <li><a href="http://www.foo-bar.com/Georgia/index.php">Georgia</a></li>
                    <li><a href="http://www.foo-bar.com/Hawaii/index.php">Hawaii</a></li>
                    <li><a href="http://www.foo-bar.com/Idaho/index.php">Idaho</a></li>
                    <li><a href="http://www.foo-bar.com/Illinois/index.php">Illinois</a></li>
                </ul>
            </li>
            <li>
          <ul>
                    
                    <li><a href="http://www.foo-bar.com/Indiana/index.php">Indiana</a></li>
                    <li><a href="http://www.foo-bar.com/Iowa/index.php">Iowa</a></li>
                    <li><a href="http://www.foo-bar.com/Kansas/index.php">Kansas</a></li>
                    <li><a href="http://www.foo-bar.com/Kentucky/index.php">Kentucky</a></li>
                    <li><a href="http://www.foo-bar.com/Louisiana/index.php">Louisiana</a></li>
                    <li><a href="http://www.foo-bar.com/Maine/index.php">Maine</a></li>
                    <li><a href="http://www.foo-bar.com/Maryland/index.php">Maryland</a></li>
                    <li><a href="http://www.foo-bar.com/Massachusetts/index.php">Massachusetts</a></li>
                    <li><a href="http://www.foo-bar.com/Michigan/index.php">Michigan</a></li>
                    <li><a href="http://www.foo-bar.com/Minnesota/index.php">Minnesota</a></li>
                    <li><a href="http://www.foo-bar.com/Mississippi/index.php">Mississippi</a></li>
                    <li><a href="http://www.foo-bar.com/Missouri/index.php">Missouri</a></li>
                    <li><a href="http://www.foo-bar.com/Montana/index.php">Montana</a></li>
              </ul>
            </li>
      <li>
                
                <ul>
                    
                    <li><a href="http://www.foo-bar.com/Nebraska/index.php">Nebraska</a></li>
                    <li><a href="http://www.foo-bar.com/Nevada/index.php">Nevada</a></li>
                    <li><a href="http://www.foo-bar.com/New-Hampshire/index.php">New Hampshire</a></li>
                    <li><a href="http://www.foo-bar.com/New-Jersey/index.php">New Jersey</a></li>
                    <li><a href="http://www.foo-bar.com/New-Mexico/index.php">New Mexico</a></li>
                    <li><a href="http://www.foo-bar.com/New-York/index.php">New York</a></li>
                    <li><a href="http://www.foo-bar.com/North-Carolina/index.php">North Carolina</a></li>
                    <li><a href="http://www.foo-bar.com/North-Dakota/index.php">North Dakota</a></li>
                    <li><a href="http://www.foo-bar.com/Ohio/index.php">Ohio</a></li>
                    <li><a href="http://www.foo-bar.com/Oklahoma/index.php">Oklahoma</a></li>
                    <li><a href="http://www.foo-bar.com/Oregon/index.php">Oregon</a></li>
                    <li><a href="http://www.foo-bar.com/Pennsylvania/index.php">Pennsylvania</a></li>
                </ul>
          </li>
            <li>
              <ul>
                    
                    <li><a href="http://www.foo-bar.com/Rhode-Island/index.php">Rhode Island</a></li>
                    <li><a href="http://www.foo-bar.com/South-Carolina/index.php">South Carolina</a></li>
                    <li><a href="http://www.foo-bar.com/South-Dakota/index.php">South Dakota</a></li>
                    <li><a href="http://www.foo-bar.com/Tennessee/index.php">Tennessee</a></li>
                    <li><a href="http://www.foo-bar.com/Texas/index.php">Texas</a></li>
                    <li><a href="http://www.foo-bar.com/Utah/index.php">Utah</a></li>
                    <li><a href="http://www.foo-bar.com/Vermont/index.php">Vermont</a></li>
                    <li><a href="http://www.foo-bar.com/Virginia/index.php">Virginia</a></li>
                    <li><a href="http://www.foo-bar.com/Washington/index.php">Washington</a></li>
                    <li><a href="http://www.foo-bar.com/West-Virginia/index.php">West Virginia</a></li>
                    <li><a href="http://www.foo-bar.com/Wisconsin/index.php">Wisconsin</a></li>
                    <li><a href="http://www.foo-bar.com/Wyoming/index.php">Wyoming</a></li>
              </ul>
          </li>
        </ul>
          </li>
        </ul>
      <div style="clear: both;">&nbsp;</div>
    </div>
  <!-- end sidebar -->
</div>
<!-- end page -->
<div id="footer">
    <p id="legal">&copy;2008 All Restaurants USA. All Rights Reserved.</p>
  <p id="links"><a href="http://www.foo-bar.com/index.php">Home</a>&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp; <a href="http://www.foo-bar.com/terms.php">Terms</a> &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; <a href="http://www.foo-bar.com/faq.php">FAQ</a> &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="http://www.foo-bar.com/contact.php">Contact Us</a></p>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by josh »

pytrin wrote:He wants to do something with the array if it's not empty, it's not there just to check if rows were returned or not. This is common usage and quite useful since array access and functions is much robust than the while loop
The array is only empty if 0 rows get returned. I use mysql_num_rows

@johnworf way to hijack the man's thread.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Bill H »

He wants to do something with the array if it's not empty, it's not there just to check if rows were returned or not. This is common usage and quite useful since array access and functions is much robust than the while loop
Well, not to be argumentative, but if there is no data in the database, then mysql_num_rows() returns zero and no additional action is required. One function call; finished.

With your methodology, one has to create an array, run through some code that attempts to fill that array from the resultset, and then test to see if that array is empty or not. That hardly seems to me to be a masterpiece of efficiency.

If, after finding that is not empty, he wants to put it into an array then there might be some advantage to that as I see that he is using print_r(). That is a masterpiece of ugliness, but certainly takes less work than writing script to provide an attractive output of the data. But the fact remains that it is hardly necessary to put the resultset into an array and then measure the array just to discover that there are no results.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Eran »

Well, not to be argumentative, but if there is no data in the database, then mysql_num_rows() returns zero and no additional action is required. One function call; finished.
If there is no data returned, the array would remain empty since no rows are iterated in the while loop - It's exactly the same as using mysql_num_rows. Not creating an empty array is such a minor optimization as opposed to being able to extract row handling logic out of the loop.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Bill H »

Okay, I bow to your superior wisdom. Half a dozen lines of code is more efficient that a single function call. I've only been programming in C, and C++ since 1981, so I could hardly be expected to know much of anything about PHP.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Eran »

You're not much for abstraction of logic out of loops, that's fine. Doesn't mean it can't work for others. Efficiency takes second place for maintainability in my book every time, not to mention your approach doesn't offer any tangible performance gains (unless you call declaring an empty array a major performance hit).
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by JAB Creations »

@Josh, I don't mind all that much. :)

@johnworf unexpected T_ELSE means you're likely missing an ending curly bracket }

I'd try to determine if the $i count was even or odd inside the loop and then apply the color/class.

@pytrin I have to agree about maintainability...while I'm working on my site if I have to add an extra step fine...it's not like the 29th version will be the last. :mrgreen:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by josh »

pytrin wrote:You're not much for abstraction of logic out of loops, that's fine.
This doesn't even make sense, your method is the one moving the logic into a loop.
pytrin wrote:Efficiency takes second place for maintainability in my book every time, not to mention your approach doesn't offer any tangible performance gains (unless you call declaring an empty array a major performance hit).
So if the query hit 5k rows you'd load them into memory just to find out its not empty? ( Not to mention _you_ didn't provide any tangible maintainability gains ;-) )
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Determine if MySQL query is empty without mysql_fetch_array?

Post by Eran »

This doesn't even make sense, your method is the one moving the logic into a loop.
No, I moved the logic outside of the loop... Jab was trying to determine inside the while loop whether it has rows or not. The point is that he wants to display the rows if they're there, not simply check if rows exists or not (otherwise a different query would be the right solution). How do you consider filling out an array as logic?
My main points for maintainability should be obvious to you josh, since you use ZF which employs the same tactics. Stuffing the rows into an array gives much better access to the data contained in the result set, including array functions, iteratable rowsets and so forth. It gives the opportunity of passing on the information to different methods / objects, as opposed to only having access to the current row inside a while loop.

Second, since he wants to display them - I assume there won't be 5k rows... if there are they would have been displayed just the same. The point is - mysql_row_count would stop a while loop from iterating over all the rows if there are no rows. In the same case, no rows would be pushed into the array... so what is the difference?
Post Reply