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
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP Syntax Error - Please Help

    Been away from coding for a while and i am running into an error and I do not see where im going wrong.

    Error is :

    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 ''test'' at line 1

    Code is(omitted database passwords etc.):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <h1>Hello </h1>
    
    <?php
    $mysqli_link = mysqli_connect("---", "---", "---");
    
    if (mysqli_connect_errno($mysqli_link))
      {
      	echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      else
      {
    	echo "Database User and Password Works";
      }
    
    echo "<br />";
    $tableone = "test";
    $result = mysqli_query($mysqli_link,"SELECT * FROM '$tableone'") or die(mysqli_error($mysqli_link));
    
    if (mysqli_connect_errno($result))
      {
      	echo "Failed to connect to SQL Database";
      }
      else
     {
    	echo "It connected to the SQL database";
      }
      
    while($row = mysqli_fetch_array($result))
    {
        echo $row['Name'];
    }
      
    
    ?>
    </body>
    </html>
    I know the database is working because when i take the quotes away in the select statement i get "no database selected" error.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    No, this is *NOT* a "PHP syntax error".

    It has nothing (directly) to do with PHP.

    As the error message says, it is a MySQL syntax error. Don't confuse the two!!

    When you get a MySQL error, it is time to stop using PHP and use a MySQL database tool (e.g., myphpadmin or similar) to DEBUG your SQL code.

    And you typically do that thus:
    Code:
    ...
    $sql = "SELECT * FROM '$tableone'";
    echo "DEBUG SQL: " . $sql . "<hr/>]n"; // remove this when code starts working
    
    $result = mysqli_query($mysqli_link, $sql ) or die(mysqli_error($mysqli_link));
    ...
    If you did that, then you would see (in this case, only) the output
    Code:
    SELECT * FROM 'test'
    And then you would copy/paste that SQL query into your database tool.

    With any luck at all, you will then get a better diagnostic error message.

    In this case, you would like get an error message like this:
    Code:
    ERROR 1064 (42000): 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 ''test'' at line 1
    You will soon learn that this message puts '...' around your actual error. So when you see
    Code:
    ''test''
    you remove the outer apostrophes and you know that the error is in the code
    Code:
    'test'
    I'll save you the trouble of browsing through the MYSQL docs: ANY time you put something in APOSTROPHES, that something becomes a *STRING* to MySQL.

    And you can *NOT* select anything from a *STRING*.

    You must select from a table NAME.

    In other words, if you had simply done
    Code:
    SELECT * FROM test
    you would not get that error!

    *PROBABLY* you don't see the difference between apostrophes ('...') and back ticks (`...`). A ` back tick is the character that shares the keyboard key with the ~ tilde. And yes, MySQL allows you to put back ticks--*NOT* apostrophes--around table and field names.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    GStatus (07-12-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    By the way: What in the world is the point in putting the name of a database table into a PHP variable in the first place?

    Surely, in any database coding you will do, you will know that name of the table ahead of time. So just put it directly into the SQL string without using a PHP variable.

    [There are rare exceptions, as when--say--you are building some kind of database tool. But that's in the somewhat distant future for you, almost surely.]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    hi mate thanks for your response.
    I initially did put no variable and i understand there is no point but the code is exactly the same in theory, i was just trying different ways to identify the problem.
    I have amended my code based on your suggestion and I am still getting a "Database not selected" error when there IS a table in my database called test and it has one field called Name so i am lost now.

    Code:
    <?php
    $result = mysqli_query($mysqli_link,"SELECT * FROM test") or die(mysqli_error($mysqli_link));
    
    if (mysqli_connect_errno($result))
      {
      	echo "Failed to connect to SQL Database";
      }
      else
     {
    	echo "It connected to the SQL database";
      }
      
    while($row = mysqli_fetch_array($result))
    {
        echo $row['Name'];
    }
    ?>
    I also tried an alternate way, still same error:

    Code:
    <?php
    $tests = "SELECT * FROM test";
    $result = mysqli_query($mysqli_link,$tests) or die(mysqli_error($mysqli_link));
    
    if (mysqli_connect_errno($result))
      {
      	echo "Failed to connect to SQL Database";
      }
      else
     {
    	echo "It connected to the SQL database";
      }
      
    while($row = mysqli_fetch_array($result))
    {
        echo $row['Name'];
    }
    ?>
    Attached some images also...
    Attached Thumbnails Attached Thumbnails PHP Syntax Error - Please Help-table.png   PHP Syntax Error - Please Help-table2.png  

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Location
    USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    check after execution, its show some database related error, try to check your records.

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You have to tell MySQL WHICH database you are looking in.

    Change this

    $mysqli_link = mysqli_connect("---", "---", "---");



    To this: $mysqli_link = mysqli_connect("example.com", "user", "password", "database");

    That fourth parameter should hold the name of the database your 'test' table is in. If it is not set properly, you will not be able to execute any queries on the DB.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    GStatus (07-12-2013)

  • #7
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thanks alot mate, i knew i was missing something i just couldnt put my finger on it.
    Legend you are!

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    You can also select the database *after* making the connection by just issuing a "use" command.

    Handy if you want to access more than one database using the same connection.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there 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
    •