Forum Moderators: coopster

Message Too Old, No Replies

PHP search problems

Why isn't it running the query?

         

alste123

3:42 pm on May 5, 2006 (gmt 0)

10+ Year Member



Hello,

I've been working with PHP for a while but it's the first time I get this error. Here is my code:

<?
$type = $_GET['type'];
if ($type) {
if ($type=="web") {
function redirection($page) {
echo "<script language='Javascript'>";
echo "<!--\n";
echo "location.href = '$page';\n";
echo "// -->";
echo "</script>\n";
}
redirection("google.com/ssss"); }
elseif ($type=="leb") $result = mysql_query("SELECT * FROM lebweb WHERE title LIKE '%$q%' ¦¦ desc LIKE '%$q%'", $db);
elseif ($type=="polls") $result = mysql_query("SELECT * FROM poll_list WHERE question LIKE '%$q%'", $db);
elseif ($type=="blogs") $result = mysql_query("SELECT * FROM blog_list WHERE title LIKE '%$q%' ¦¦ desc LIKE '%$q%' ¦¦ link LIKE '%$q%' ¦¦ author LIKE '%$q%'", $db);
elseif ($type=="classifieds") $result = mysql_query("SELECT * FROM classifieds_list WHERE itemname LIKE '%$q%' ¦¦ desc LIKE '%$q%'", $db);
elseif ($type=="fanlistings") $result = mysql_query("SELECT * FROM fan_list WHERE title LIKE '%$q%' ¦¦ name LIKE '%$q%'", $db);
elseif ($type=="published") $result = mysql_query("SELECT * FROM publication_list WHERE title LIKE '%$q%' ¦¦ content LIKE '%$q%' ¦¦ author LIKE '%$q%'", $db);
else die("Invalid search type.");
}
$aff = mysql_affected_rows();

echo $aff;

?>

So, when I search for a term that is present once in the database, it returns -1 affected rows. When I search for a term that is present 2 or more times in my database, it returns all rows with the correct number of rows.

However, when I run a search on the polls table, everything is fine.

Are my queries wrong? Thanks, guys.

All suggestions on improving code + security are welcome.

eelixduppy

5:18 pm on May 5, 2006 (gmt 0)



When mysql_affected_rows() returns -1, that means that the last query that was used didn't return any values. I think this may be due to you using "desc" as a column name. Try replacing all of the "desc"'s with tablename.desc to see if this fixes your problem.

Good luck!

eelix

alste123

5:25 pm on May 5, 2006 (gmt 0)

10+ Year Member



Hi,

Yes, indeed, it was the "desc" that was the source of the problems. The MySQL was probably reading the column name as DESC order.

Thanks for for help, eelix!

coopster

2:29 pm on May 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, alste123.

You may also want to use mysql_num_rows() [php.net]. Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() [php.net] to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.