Jump to content

how to get an null echo reslut


sudduth66

Recommended Posts

You almost had it but instead it is:

 

if (mysql_num_rows($sql) > 0){
// Process hamsters
}else{
   echo 'There are no hamsters in the field! Feel free to let the cat out.';
}

 

mysql_num_rows() uses the result of the sql. You were just using $term which was the result of a post.

Link to comment
Share on other sites

You have place the code coolcam262 suggested in wrong place. The if (mysql_num_rows($sql) > 0){ line should go before the while loop

 

Change

     while ($row = mysql_fetch_array($sql)){
    echo 'box_num: '.$row['box_number'];
    echo '<br/> dept: '.$row['Department'];
    echo '<br/> company: '.$row['Company'];
    echo '<br/> status: '.$row['status'];
        echo '<br/> location: '.$row['location'];
    echo '<br/> description: '.$row['box_desc'];
         
         if (mysql_num_rows($sql) > 0){
}

else{
   echo 'No Boxes available please use your back button to select a new box.';
}
         echo '<br/><br/>';
    } 

 

To

if (mysql_num_rows($sql) > 0)
{   
    while ($row = mysql_fetch_array($sql))
    {
    echo 'box_num: '.$row['box_number'];
    echo '<br/> dept: '.$row['Department'];
    echo '<br/> company: '.$row['Company'];
    echo '<br/> status: '.$row['status'];
        echo '<br/> location: '.$row['location'];
    echo '<br/> description: '.$row['box_desc'];
    }
}
else
{
   echo 'No Boxes available please use your back button to select a new box.';
}

 

Link to comment
Share on other sites

Try this code:

<?php
  mysql_connect ("localhost", "3333","33333333")  or die (mysql_error());
      mysql_select_db ("Customers");
       
       $term = (isset($_POST['term'])?$_POST['term']:null);

$sql = mysql_query("select * from storage where box_number = '$term'");
    
    $row = mysql_fetch_array($sql);
if(count($row)){   
 while ($row){
	echo 'box_num: '.$row['box_number'];
	echo '<br/> dept: '.$row['Department'];
	echo '<br/> company: '.$row['Company'];
	echo '<br/> status: '.$row['status'];
	echo '<br/> location: '.$row['location'];
	echo '<br/> description: '.$row['box_desc'];
}
} else {
echo 'There are no hamsters in the field! Feel free to let the cat out.';
}
echo '<br/><br/>';
?>

 

Link to comment
Share on other sites

You have your logic backwards. You have to check if there were results BEFORE processing the results. Although, I have a hard time believing that you are getting no results and no errors.

 

<?php

//Connect to DB
mysql_connect ("localhost", "John","Harley08")  or die (mysql_error());
mysql_select_db ("Customers");

//Parse user input and run query
$term = mysql_real_escape_string(trim($_POST['term']));
$query = "SELECT * FROM storage WHERE box_number = '$term'";
$result = mysql_query($query);

//Check results of query
if (!$result)
{
    //There was an error running the query
    echo "There was an error running the query<br><br>\n";
    echo "Query:<br>{$query}<br><br>\n";
    echo "Error:<br>" . mysql_erro();
}
else if(mysql_num_rows($sql) < 1)
{
    //No records returned
    echo 'No Boxes available please use your back button to select a new box.';
}
else
{
    //There were records returned, display them
    while ($row = mysql_fetch_array($sql))
    {
       echo "box_num: {$row['box_number']}<br>\n";
       echo "dept: {$row['Department']}<br>\n";
       echo "company: {$row['Company']}<br>\n";
       echo "status: {$row['status']}<br>\n";
        echo "location: {$row['location']}<br>\n";
       echo "description: {$row['box_desc']}<br><br>\n";
    }
}

?>

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.