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 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    texas
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can someone tell me what this error means?

    I get this error:

    sorry could not connect to the databaseYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    Here is the code that I have
    Code:
    <?php
    
    $dbh=mysql_connect ("localhost", "un", "pw") or die('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("db"); 
    
    $query="select * from $display";
    
    $result=mysql_db_query("db", $query) or die('sorry could not connect to the database' . mysql_error());
    ?>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    </body>
    </html>
    I don't know if I have ever seen that error before, so any help would be appreciated.
    thanks
    bri

  • #2
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are a few problems with your code. The initial error you're having trouble with shows that you can't connect to your MySQL server. Your user name or password for the database must be wrong, or your MySQL server isn't configured properly.

    To get to the bottom of the problem if your MySQL server is misconfigured (or not even running), you might need to go to the MySQL forums here on codingforums, and post what version of MySQL you're using, the platform on which it is running, and any other relevant information you can think of. Don't forget to post about your problem in detail, so everyone will be able to help you adequately.

    Back to the PHP:

    The variable $display isn't defined within the scope of the code you posted. Unless it is defined above the code you posted, or in an included file, you woudn't be able to execute your query, even if you had connected.

    The mysql_db_query function is deprecated in favor of mysql_query. Since you use mysql_select_db, mysql_db_query is completely unnecessary. See this page on PHP.net's manual (it's toward the bottom of the page in the warning).
    Last edited by Curtis D; 06-10-2006 at 10:46 AM.
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like your script is connecting to your database ok; it's your actual query that's messing up (but it's confusing, because your query error message says you can't connect). Like Curtis D posted, that's probably because of a missing $display variable. You can echo out $query to see what you're getting.

  • #4
    New Coder
    Join Date
    Apr 2006
    Location
    Queensland Oz
    Posts
    55
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Remove the h from $dbh
    Nominate the database you want to connect to - mysql_select_db ("database_name",$db);
    Define the $display variable - table name
    Query should be - $result = mysql_query("$query", $db) or die(mysql_error());

    Just my thoughts
    Last edited by ozlad; 06-11-2006 at 12:09 AM.

  • #5
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, the error says it's on line 1, if line 1 refers to his block of code, then it's the line with mysql_connect.
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The 'line 1' is coming from MySQL -- it's referring to the first line of the query, not the first line of the PHP script.

    The die() function carries the message you're seeing, and 'sorry could not connect to the database' is in the mysql_query() call's die(). If mysql_connect() were raising the error, you'd see 'I cannot connect to the database because'. Also, a failed connection wouldn't raise a syntax error.

    And for those watching at home, all this is an object lesson in paying attention to the clarity of manually defined error messages.

  • #7
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lol, yes, thank you. You are completely right. My mind must have been wandering at the time. As you can see, my observation powers are at maximum LOL
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski

  • #8
    New Coder
    Join Date
    Dec 2003
    Location
    texas
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That makes perfect sense....I will drop the 'H' out of dbh and see what happens. I have $display set, it is coming from the URL...basically having the page reserved to cache...so it will load faster. Thanks for all of the help guys I really appreciate it....and I will let you know of the outcome.
    thanks
    bri

  • #9
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dropping the 'h' won't do you any good; the name of your connection variable doesn't matter (although if you define it as $dbh, and then call for $db, of course it'll fail).

    Have you echo'd your query to double-check the $display variable? Your script is outdated, probably written for earlier versions of PHP with register_globals turned on, so URL variables were automatically set up for you. This is no longer the case, so dollars to doughnuts your query's going to echo as 'select * from', meaning you need to pull your value out of the $_GET array:

    Code:
    $display = $_GET['display'];
    Be sure to run some validation/sanitation on that value, though, before sending it to your database.

  • #10
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To escape data for the MySQL query, you should use the mysql_real_escape_string function.
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski


  •  

    Posting Permissions

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