View Full Version : SQL Error?

03-20-2007, 09:06 PM
Here is my php:


$querey = $_GET['q'];

// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("restfind") or die(mysql_error());

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
$pagenum = 1;

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query('SELECT `*` FROM `places` WHERE `name`, `description` LIKE \'%' . $querey . '\'') or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
$pagenum = 1;
elseif ($pagenum > $last)
$pagenum = $last;

//This sets range that we will display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query('SELECT `*` FROM `places` WHERE `name`, `description` LIKE +' . $querey . '+') or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))

Print $info['Name'];
echo "<br>";

echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

Here is the SQL taken out of the php:

SELECT `*` FROM `places` WHERE `name`, `description` LIKE \'%' . $querey . '\'

I don't know whats wrong!!!

Please Help!

03-20-2007, 09:27 PM
Don't know php, but try this:

SELECT * FROM places WHERE name, description LIKE \'%' . $querey . '\'
if name or description are reserved words, then surround them with [], like:

SELECT * FROM places WHERE [name], [description] LIKE \'%' . $querey . '\'

03-20-2007, 09:27 PM
SELECT * FROM `places` WHERE `name`, `description` LIKE \'%' . $querey . '\'

Shouldn't be any quotes around the *

03-20-2007, 10:42 PM
I still get

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 ' `description` LIKE '%dream'' at line 1

03-20-2007, 10:50 PM
Not sure but I don't think you can have your where statement setup like that? You can call a field then add a comma and then call another field and it will search through both fields that way? I have never done that... If you can do that, thats news to me :)

03-20-2007, 10:55 PM
iLLin's right, you have to use AND or OR or IN(), not a comma.

03-21-2007, 12:53 AM
$sql="SELECT *
FROM places
WHERE `name` LIKE '%{$_GET['q']}'
OR `description` LIKE '%{$_GET['q']}'

for mysql you (optionally) use backticks to escape field `names` but iLLin spotted the main issue + if you use double quotes you don't need to go around escaping or concatenating everything.

03-21-2007, 03:08 PM
I'd suggest reading a basic MySQL tutorial, perhaps one considering debugging. Because this one should've been relatively easy to catch (since MySQL tells you where the problem is).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum