View Full Version : Can someone tell me what this error means?
06-10-2006, 07:03 AM
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
$dbh=mysql_connect ("localhost", "un", "pw") or die('I cannot connect to the database because: ' . mysql_error());
$query="select * from $display";
$result=mysql_db_query("db", $query) or die('sorry could not connect to the database' . mysql_error());
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
I don't know if I have ever seen that error before, so any help would be appreciated.
06-10-2006, 10:43 AM
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 (http://www.codingforums.com/forumdisplay.php?f=7) 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 (http://php.net/mysql_db_query) on PHP.net's manual (http://php.net/manual/en/) (it's toward the bottom of the page in the warning).
06-10-2006, 05:37 PM
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.
06-10-2006, 11:39 PM
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
06-11-2006, 12:29 PM
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.
06-11-2006, 07:16 PM
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.
06-11-2006, 11:12 PM
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
06-12-2006, 07:50 PM
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.
06-12-2006, 08:03 PM
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 (http://us3.php.net/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:
$display = $_GET['display'];
Be sure to run some validation/sanitation on that value, though, before sending it to your database.
06-13-2006, 12:15 PM
To escape data for the MySQL query, you should use the mysql_real_escape_string (http://php.net/mysql_real_escape_string) function.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.