Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: SQL Error?

  1. #1
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts

    SQL Error?

    Here is my php:

    PHP Code:
     <?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
    {

    else 
    {
    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:

    Code:
    SELECT `*` FROM `places` WHERE `name`, `description` LIKE \'%' . $querey . '\'
    I don't know whats wrong!!!

    Please Help!

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Don't know php, but try this:
    Code:
    SELECT * FROM places WHERE name, description LIKE \'%' . $querey . '\'
    if name or description are reserved words, then surround them with [], like:
    Code:
    SELECT * FROM places WHERE [name], [description] LIKE \'%' . $querey . '\'
    I am the luckiest man in the world

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    SELECT * FROM `places` WHERE `name`, `description` LIKE \'%' . $querey . '\'

    Shouldn't be any quotes around the *

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    iLLin's right, you have to use AND or OR or IN(), not a comma.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    PHP Code:
    <?php
    $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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •