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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Mysql Syntax error PLEASE HELP - SHORT QUESTION

    Hi, i have been trying for 3 hours now to get one simple string of information from a mysql database using php. I have used the exact same code before by only changing the mysql command to other database information... i have tryed multiple codes without success.

    Mysql returns syntax error like this: 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 'Key FROM pptable WHERE Username=dexter' at line 1

    i have two files, includes/connection.php and process.php

    Here is the content of both:

    includes/connection.php:

    <?php
    // Below lines sets the information to connect to the database... change for use with your own information...
    $host = "127.0.0.1";
    $user = "root";
    $password = "";
    $database = "database";

    $connect = mysql_connect($host, $user, $password) or die (mysql_error());
    mysql_select_db($database) or die (mysql_error());
    ?>

    process.php:

    <?php
    include("includes/connection.php");
    $key ="";
    $user = "";
    $key = $_GET['key'];
    $user = $_GET['user'];

    if ($user == ""){
    echo "<h3>* Was not able to gather user...<hr></h3>";
    }
    if ($key == ""){
    echo "<h3>* Was not able to gather key...<h3><br><hr>";
    }

    $query = "SELECT Key FROM pptable WHERE Username=$user";
    $query = mysql_query($query) or die (mysql_error());
    echo $query;
    ?>

    How can i fix this syntax error? ps. The mysql database is correctly configured...

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Check http://dev.mysql.com/doc/refman/5.1/...ved-words.html

    KEY is a reserved word, so you can't as such use it as the name of a table/field inside a query. You may use backticks to avoid the error, but using those words as table/field names is a bad practice.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Also, you almost surely need quotes (apostrophes) around the supplied username.

    So:
    Code:
    $query = "SELECT `Key` FROM pptable WHERE Username='$user'";
    Be careful: Those are backticks (usually share the keyboard with the ~ key) around Key and apostrophes (share the keyboard with the " key) around $user.


  •  

    Tags for this Thread

    Posting Permissions

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