Dynadot โ€” .com Registration $8.99

What the **** is up with my script?!?

Spaceship Spaceship
Watch

tm

Established Member
Impact
25
Hey,

Im trying to find a string in a database.

I KNOW the string is there, i can find it easily in phpmyadmin.

I'm DEFINATELY connecting to the database, otherwise the script would die().

I've checked that everything is spelled correctly.

so WHY is it returning 0 numrows?!? There's definately a good two or three in the database (WHERE string='$q').

I've turned on error reporting
error_reporting(E_ALL);
but nothings coming up.

Me and several other people are STUMPED, so I need help!

thanks,

tm
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
whats the whole query?
 
0
•••
I dont know, we would have to see the code!
 
0
•••
use echo $q to make sure that $q isn't blank or something like that. Just check the small things. Also, instead of using $q, try an actual word instead of a variable. Will show you if it's a problem in the variable. Sorry if this doesn't help, but I tried.
 
0
•••
If the row has more than just $q in it, that won't work.

Use:

PHP:
$query = "SELECT * FROM table";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
     echo 'No results.';
} else {
     while ($row = mysql_fetch_assoc($result) {
          if (eregi($q, $row['string'])) {
               echo 'I don\'t know what you want it to write.. so edit this.';
          }
     }
}
 
0
•••
What I always do in a situation like yours, I just echo the query and run the same query in phpMyAdmin. Then you usually find out in a couple of seconds what's wrong with the original query.

If you don't give us your code/query, there's not much else we can do.

best of luck!

-monty
 
0
•••
well if you are finding it in phpmyadmin then just clikc the create php code link on the top and copy-paste it. :D
 
0
•••
Here is the code:

$query = "SELECT * FROM `table` WHERE string = '$q'";
$result = mysql_query($query) or die("Query failed 2: " . mysql_error());
$num = mysql_numrows($result);


there is nothing else in that table part other than $q (not even spaces).

I do die($q) and it echoes the string.
 
0
•••
Your problem is quite simple:
PHP:
$num = mysql_numrows($result);

//Should be

$num = mysql_num_rows($result);
;)
 
0
•••
Still doesnt work.. :S
 
0
•••
So you have a field called 'string' in your table ?
Isn't it a reserved keyword ? Be careful when using reserved keyword in field names as this can lead to unpredictable results.
Did you try your query in phpmyadmin ?
 
0
•••
Yes string is in the table.

Whats a reserved keyword? :s

Query in phpmyadmin returns:
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0248 sec)
 
0
•••
then there is something wrong with the query. Can you echo the query and paste it here?
 
0
•••
Try the following.

PHP:
 $query = "SELECT * FROM `table` WHERE string = ".$q."";
$result = mysql_query($query) or die("Query failed 2: " . mysql_error());
$num = mysql_num_rows($result);

//Debugging if it doesnt work,
echo "The Query Is ".$query."";

Run the query in phpmyadmin, and see what happens.

Flubb
 
0
•••
If sdsinc is right, you need to change the name of it from string to something else.
 
0
•••
0
•••
It's not actually string.. sorry for confusion, its actually a different (non-reserved) word.

Flubber, i changed the query to that and it still doesnt work... not sure where you're supposed to run php in phpmyadmin though :S
 
0
•••
PHP:
 $query = "SELECT * FROM table";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
     echo 'No results.';
} else {
     $i = 0;
     while ($row = mysql_fetch_assoc($result) {
          if (eregi($q, $row['string'])) {
               $i++;
          }
     }
}

Then $i will be the total number.. you shouldn't have to do this except you won't tell us the full code, or even the name of the row..
 
0
•••
OK here's basically the code i have atm:

Code:
		$query = "SELECT * FROM `table` WHERE strng = '".$q."'";
		$result = mysql_query($query) or die("Query failed 2: " . mysql_error());
		$num = mysql_num_rows($result); 
		
		if($num == 0) { 
			echo "<B>$q</B><UL><LI>No result(s) found.</LI>";
			echo "</UL>";
		} else {
		
		
		echo "<B>$q</B>";
		echo "<UL>";
		
		$i = 0;
		while ($i < $num) {
				$desc = mysql_result($result,$i,"desc");
		
		echo "<li>$desc</li>";
		
		$i++;
		}
		
		echo "</UL>";
		
		}

It always echoes:

$q (eg A)
No Results Found

even though $q is DEFINATELY in the database
 
0
•••
I think I know what it is.

Replace
$query = "SELECT * FROM `table` WHERE strng = '".$q."'";
With
$query = "SELECT * FROM table WHERE string = '$q'";
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back