imarockstar Posted March 17, 2010 Share Posted March 17, 2010 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 .. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/ Share on other sites More sharing options...
premiso Posted March 17, 2010 Share Posted March 17, 2010 $result = mysql_query ("SELECT * FROM shows WHERE zip = '75034' ") or trigger_error("MySQL Error: " . mysql_error()); The or section needs to be outside of the mysql_query function. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1027712 Share on other sites More sharing options...
imarockstar Posted March 17, 2010 Author Share Posted March 17, 2010 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 .. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1027734 Share on other sites More sharing options...
premiso Posted March 17, 2010 Share Posted March 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1027739 Share on other sites More sharing options...
imarockstar Posted March 17, 2010 Author Share Posted March 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1027743 Share on other sites More sharing options...
premiso Posted March 17, 2010 Share Posted March 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1027745 Share on other sites More sharing options...
imarockstar Posted March 18, 2010 Author Share Posted March 18, 2010 works great .. thank you .. Quote Link to comment https://forums.phpfreaks.com/topic/195589-getting-error-with-a-num_rows-issue-could-someone-look-over-this-code/#findComment-1028124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.