samus Posted May 22, 2007 Share Posted May 22, 2007 Why, hello. I'm new - first post - here. And I require some help, if you don't mind (: I have searched, and searched, and searched some more for help within the forums for this problem I have. I'm positive others have had the same (yet they seem to have theirs solved) problem, but I can not seem to figure out mine. Run down: This is my database called "search". Yesh, I would like to make a search function (: http://img.photobucket.com/albums/0903/ganonsdaughter/search.jpg Now, here is my connection between my database and the website. <?php $connection=mysql_connect("localhost","****","****"); if (!$connection) { echo "Could not connect to MySQL server!"; exit; } $db=mysql_select_db("as_contacts",$connection); if(!$db) { echo "Could not change into database"; exit; } ?> And here, folks, is my search.php with a form. <?php include ('connect_inc.php'); if ('submit') { $search=$_POST['search']; } ?> And the form itself. <td width="623" ><div id="text"><center> <form action="search1.php" method="POST"> <input type="text" name="search" size="30" /> <input type="submit" value="search" /> </form> </center></div></td> Now, onto the search1.php to process the results. <?php include ('connect_inc.php'); ?> And <?php $search = $_REQUEST['search'] $query ="SELECT * FROM search WHERE item, calories, discription LIKE '%$search%'"; $result =mysql_query($query); $num =mysql_num_rows($result); if($num==0) { echo "<br><font color='white'>Sorry, could not find what you were looking for. Please try again.</font>"; } else { $i=0; while ($i<$num) { $item=mysql_result($result,$i,"item"); $calories=mysql_result($result,$i,"calories"); $discription=mysql_result($result,$i,"discription"); echo '$item'; echo '$calories'; echo '$discription'; $i++; } } ?> Now, the problem I am having is this: AH HAH I hear you cry. I should add the @ at the start of it. Like this. $num =@mysql_num_rows($result); Unfortuantely, that too does not work. ): And it gives me this. Nothing. Zip. Zero results. So, what I would like is to have a search function that gives me all the data of the row(s) from a "Like" SQL query. ~ If this is the wrong sub section, please move me Any help would be great! (And I do understand it could easily be a spelling mistake or even worse ) Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/ Share on other sites More sharing options...
MadTechie Posted May 22, 2007 Share Posted May 22, 2007 try $search = addslashes($search); //Added after Frosts comment $query ="SELECT * FROM search WHERE item LIKE '%$search%' OR calories LIKE '%$search%' OR discription LIKE '%$search%'"; EDIT: AH HAH I hear you cry. I should add the @ at the start of it. Like this. $num =@mysql_num_rows($result); Oh more like remove the @ is only hide the error nothing more Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259082 Share on other sites More sharing options...
per1os Posted May 22, 2007 Share Posted May 22, 2007 Never use the @ sign. Huge sign of a newbie, your code should work without having to surpress errors. EVen if it is an "easy" fix, it didn't fix the problem did it? Now onto the error, the reason you are throwing an error is that your query has sometype of an error in it. Whether it be $search contains a ' which if it does you have a prone to be sql injection problem. It could also be that discription is spelled wrong and in sense is not a column. I would add a debug statement for now as seen below: <?php $search = $_REQUEST['search'] $query ="SELECT * FROM search WHERE item, calories, discription LIKE '%$search%'"; $result =mysql_query($query) OR DIE("Your SQL: " . $query . "<br/> Produced this Error: " . mysql_error()); $num =mysql_num_rows($result); if($num==0) { echo "<br><font color='white'>Sorry, could not find what you were looking for. Please try again.</font>"; } else { $i=0; while ($i<$num) { $item=mysql_result($result,$i,"item"); $calories=mysql_result($result,$i,"calories"); $discription=mysql_result($result,$i,"discription"); echo '$item'; echo '$calories'; echo '$discription'; $i++; } } ?> Run that and report back the error. Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259083 Share on other sites More sharing options...
samus Posted May 22, 2007 Author Share Posted May 22, 2007 I did what you asked frost110 "Parse error: parse error in /home/asmith/public_html/search1.php on line 51" Is what I got. Line 51 is the original query itself. I then put this, since I didn't know if you meant that what you added was meant to go AFTER frost110 query, or just because you added it after he made a post. So I did it both ways (what a dofus I am). $search = addslashes($search); //Added after Frosts comment $query ="SELECT * FROM search WHERE item LIKE '%$search%' OR calories LIKE '%$search%' OR discription LIKE '%$search%'"; $result =mysql_query($query) OR DIE("Your SQL: " . $query . "<br/> Produced this Error: " . mysql_error()); $num =mysql_num_rows($result); Got this from above. Then I tried it this way due to the misunderstanding of the posts ^-^; <?php $search = $_REQUEST['search'] $query ="SELECT * FROM search WHERE item LIKE '%$search%' OR calories LIKE '%$search%' OR discription LIKE '%$search%'"; $result =mysql_query($query) OR DIE("Your SQL: " . $query . "<br/> Produced this Error: " . mysql_error()); $search = addslashes($search); //Added after Frosts comment $num =mysql_num_rows($result); if($num==0) { And I still get the same error from earlier, still aimed towards the $query. In the first set of code (on this post), I took out the $search =$_REQUEST['search'] by mistake, whoops. Put it back in, and I got this. <?php $search = $_REQUEST['search'] $search = addslashes($search); //Added after Frosts comment $query ="SELECT * FROM search WHERE item LIKE '%$search%' OR calories LIKE '%$search%' OR discription LIKE '%$search%'"; $result =mysql_query($query) OR DIE("Your SQL: " . $query . "<br/> Produced this Error: " . mysql_error()); $num =mysql_num_rows($result); if($num==0) { "Parse error: parse error in /home/asmith/public_html/search1.php on line 51" Which is now the; "$search = addslashes($search); //Added after Frosts comment" Now, I know you're probably thinking "what an idiot", I'm trying my best, I really am Just such a long day. ANYWAY, I believe I know why I got that last error, due to the $search can't be set twice? o_O Maybe? God I'm hopeless. Heh. Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259098 Share on other sites More sharing options...
MadTechie Posted May 22, 2007 Share Posted May 22, 2007 you may want to add the ; on line $search = $_REQUEST['search'] so $search = $_REQUEST['search']; Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259101 Share on other sites More sharing options...
taith Posted May 22, 2007 Share Posted May 22, 2007 your missing a ; at the end of the first line there also you can simplify your code like this $search=addslashes($_REQUEST['search']); Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259102 Share on other sites More sharing options...
samus Posted May 22, 2007 Author Share Posted May 22, 2007 Haha. Told you I was a clutz. HOWEVER o: Did what you said (: Well, I hope this is what you said. <?php $search=addslashes($_REQUEST['search']); $query ="SELECT * FROM search WHERE item LIKE '%$search%' OR calories LIKE '%$search%' OR discription LIKE '%$search%'"; $result =mysql_query($query) OR DIE("Your SQL: " . $query . "<br/> Produced this Error: " . mysql_error()); $num =mysql_num_rows($result); if($num==0) { echo "<br><font color='white'>Sorry, could not find what you were looking for. Please try again.</font>"; } else { $i=0; while ($i<$num) { $item=mysql_result($result,$i,"item"); $calories=mysql_result($result,$i,"calories"); $discription=mysql_result($result,$i,"discription"); echo '$item'; echo '$calories'; echo '$discription'; $i++; } } ?> And I'm getting this. Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259111 Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 Variables within single quotes are not interpreted, instead of <?php echo '$item'; echo '$calories'; echo '$discription'; ?> you probably want <?php echo $item . '<br>' . $calories . '<br>' . $discription; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259116 Share on other sites More sharing options...
MadTechie Posted May 22, 2007 Share Posted May 22, 2007 change <?php echo '$item'; echo '$calories'; echo '$discription'; ?> to <?php echo "item:$item<br>calories:$calories<br>discription:$discription"; ?> Doh too slow; Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259120 Share on other sites More sharing options...
samus Posted May 22, 2007 Author Share Posted May 22, 2007 Oh man, thanks very much guys! Thank you so much! - since this is solved do I edit the thread title myself? Or does one of the moderators do it? Again, thanks Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259125 Share on other sites More sharing options...
taith Posted May 22, 2007 Share Posted May 22, 2007 bottom left Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259127 Share on other sites More sharing options...
samus Posted May 22, 2007 Author Share Posted May 22, 2007 bottom left DOUBLE SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/52506-solved-warning-mysql_num_rows-not-valid/#findComment-259138 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.