Jump to content

[SOLVED] Warning: mysql_num_rows(): not valid?


samus

Recommended Posts

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:

problem.jpg

 

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.

problem2.jpg

 

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 :P )

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

again.jpg

 

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? :P

 

God I'm hopeless.  Heh.

 

Link to comment
Share on other sites

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.

again.jpg

Link to comment
Share on other sites

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

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.