Jump to content

getting error with a num_rows issue .. could someone look over this code ..


Recommended Posts

this is my code

 

<div class="">
		    <?php
		    // if the submit button has been clicked then we perform the search
		    
		    if (isset($_POST['submitsearch'])) {
		    
			    $zip = $_POST['zip'];
			    $day = $_POST['day'];
			    $month = $_POST['month'];
			    $year = $_POST['year'];
			    
			    
			    $result = mysql_query ("SELECT * FROM shows WHERE zip = '75034' " or die(mysql_error()));
			    $count = mysql_num_rows($result);
			    echo $count;
			    
			    if ( $count == '' ) { echo "Sorry no shows were found, please try again";  }
			    else { 

				    echo "Search Results";
				    
				    while($rows=mysql_fetch_array($result)){
				    
				    echo $rows['venue'];
				    echo "<br>";
		    
		    
				    } // search results end
			    } // if post is empty else
		    } // post if end
		    ?>
		    </div>
		    
		    <p>Search for shows, this will be used for bands who will be on the road and need to book a show on a certain date in a certain city.</p> <br>

			<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			Zipcode: <input type="text" name="zip" > <br>
			Date Needed: <?php include("modules/form_date.php"); ?> <br>

			<input type="submit" name="submitsearch" value="Find Show">
			</form>

 

 

this is my error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/tourzag/public_html/stfo_scripts.php on line 60

Sorry no shows were found, please try again

 

i cant figure it out ... why its throwing that error ..

updated code

 

<div class="">
		    <?php
		    // if the submit button has been clicked then we perform the search
		    
		    if (isset($_POST['submitsearch'])) {
		    
			    $zip = $_POST['zip'];
			    $day = $_POST['day'];
			    $month = $_POST['month'];
			    $year = $_POST['year'];
			    
			    
			    
			    
			    $sql="SELECT * FROM shows WHERE zip = ".$_POST['zip']."";
				$result=mysql_query($sql);
			    $count = mysql_num_rows($sql);
			    
			    echo $count;
			    
			    if ( $count == '' ) { echo "Sorry no shows were found, please try again";  }
			    else { 

				    echo "Search Results";
				    
				    while($rows=mysql_fetch_array($result)){
				    
				    echo $rows['venue'];
				    echo "<br>";
		    
		    
				    } // search results end
			    } // if post is empty else
		    } // post if end
		    ?>
		    </div>
		    
		    <p>Search for shows, this will be used for bands who will be on the road and need to book a show on a certain date in a certain city.</p> <br>

			<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			Zipcode: <input type="text" name="zip" > <br>
			Date Needed: <?php include("modules/form_date.php"); ?> <br>

			<input type="submit" name="submitsearch" value="Find Show">
			</form>




 

 

error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/tourzag/public_html/stfo_scripts.php on line 63

Sorry no shows were found, please try again

Search for shows, this will be used for bands who will be on the road and need to book a show on a certain date in a certain city.

 

im trying to count the rows, and if the rows come back empty then it displays an error ..  this is line 63

 

$count = mysql_num_rows($sql);

 

not sure why i am getting this ..

 

 

First up, why did you remove the error reporting portion? As it can be useful and if done right will not cause any dismay.

 

Second, you are using $sql and not $result for the mysql_num_rows.

 

$sql="SELECT * FROM shows WHERE zip = ".$_POST['zip'].""; 
               $result=mysql_query($sql) or trigger_error("MySQL Failed: " . mysql_error());
                $count = mysql_num_rows($result);

 

As a side note, that code is possibly prone to SQL Injection. Before going public with it I would suggest looking into mysql_real_escape_string.

I am just testing now .. and I will add all the sql inj. stuff later ...

 

i updated the code and now I am getting 2 errors then I the var i pass is empty (zip)

 

<div class="">
		    <?php
		    // if the submit button has been clicked then we perform the search
		    
		    if (isset($_POST['submitsearch'])) {
		    
			    $zip = $_POST['zip'];
			    $day = $_POST['day'];
			    $month = $_POST['month'];
			    $year = $_POST['year'];
			    
			    
			    $sql="SELECT * FROM shows WHERE zip = ".$_POST['zip'].""; 
                    $result=mysql_query($sql) or trigger_error("MySQL Failed: " . mysql_error());
                    $count = mysql_num_rows($result);
			    
			    if ( $count == '' ) { echo "Sorry no shows were found, please try again";  }
			    else { 

				    echo "Search Results";
				    
				    while($rows=mysql_fetch_array($result)){
				    
				    echo $rows['venue'];
				    echo "<br>";
		    
		    
				    } // search results end
			    } // if post is empty else
		    } // post if end
		    ?>
		    </div>

 

my error :

 

Notice: MySQL Failed: 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 '' at line 1 in /home/tourzag/public_html/stfo_scripts.php on line 60

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/tourzag/public_html/stfo_scripts.php on line 61

Sorry no shows were found, please try again

 

 

although when i put a zipcode into the form, the query worked fine and it returns the data.

 

There are many different ways to do this, one check if $_POST['zip'] is isset and not empty if it is either, do not run the query and just display an error.

 

Method 2, which is easier but not as intuitive as an error message would be is add single quotes around the zip value like below.

$sql="SELECT * FROM shows WHERE zip = '". $_POST['zip."'"; 

 

Either should work.

 

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.