JohnSmithers Posted June 3, 2011 Share Posted June 3, 2011 I used the following code: <?php echo "hi there"; $usr = "xxxxxxxxxxx"; $pwd = "xxxxxxxxxx"; $db = "samdb"; $host = "xx.xxx.xxx.xxx"; $sqlCon = mysql_connect($host, $usr, $pwd); mysql_select_db("database_name"); if (!$sqlCon) { echo("ERROR: " . mysql_error() . "\n"); } // GET THE CURRENTLY PLAYING SONG INFO $curPlay = mysql_query("SELECT * FROM `historylist' ORDER BY date_played DESC LIMIT 1"); $curPlayRow = mysql_fetch_assoc($curPlay); if (!$curPlay) { echo( mysql_error()); } ?> and got the following message: hi there Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\samtest\samtest.php on line 13 No database selected I also tried this: <?php echo "hi there"; function db_connect() { $result = new mysqli('xx.xxx.xxx.xxx', 'xxxxx', 'xxxxx', 'samdb'); if (!$result) { throw new Exception('Could not connect to database server'); } else { return $result; } } echo "got this far"; // GET THE CURRENTLY PLAYING SONG INFO $curPlay = mysql_query("SELECT * FROM historylist ORDER BY date_played DESC LIMIT 1"); $curPlayRow = mysql_fetch_assoc($curPlay); if (!$curPlay) { echo( mysql_error()); } ?> and got this result: hi theregot this far Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\samtest\samtest2.php on line 20 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\samtest\samtest2.php on line 20 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\samtest\samtest2.php on line 21 Access denied for user 'ODBC'@'localhost' (using password: NO) can anyone help with this?7 MySQL sever version 5.5.8 clinet version 5.0.7 I'm using xampp but I can't find the version of this, but it was the latest from about two months ago. Thanks in advance MOD EDIT: code tags added Quote Link to comment Share on other sites More sharing options...
revraz Posted June 3, 2011 Share Posted June 3, 2011 Error says it all, Access Denied for the user id/pw you are using to perform that action on the DB. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 3, 2011 Share Posted June 3, 2011 For your first piece of code, the error "No database selected" indicates that your mysql_select_db() statement failed for some reason and your query both failed because there was no database selected and it also has a sql syntax error in it (there is a single quote on the end of the table name.) For your second piece of code, you are not calling your db_connect() function and don't have a valid connection at the time you are trying to execute a query and the mysql_query() statement is attempting to make a connection using default values (from the php.ini) for the user/password, which of course fails. Your second piece of code is also attempting to make a mysqli connection, but is using mysql statements. You cannot mix mysqli with mysql for one connection. You need to pick one set of code that you know the best and stick with it. If you are just randomly trying code that you have seen or found without understanding what it does or even if it goes with other code in the same program, it will take you a very long time to produce any code that works. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 3, 2011 Share Posted June 3, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment Share on other sites More sharing options...
JohnSmithers Posted June 5, 2011 Author Share Posted June 5, 2011 Thanks for all your assistance and advice for posting. I had been giving myself a crash course on this a few months ago. The first code is lifted from an external source - the apostrophes were not in at all previously and I had tried all connotations on this; none of them work. My second code was one which I had lifted from an example I had been working on during the "crash course"; I had forgotten the differences. I suspected I could not access the database and I think this is a "port forwarding" issue, but the fact that my result said "got this far" confused me as I assumed I was connected (?). I assume therefore that I am not connected at all. Is there any advice you can give me to test if the connection actually works first, before I test for accessing the tables? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2011 Share Posted June 5, 2011 Here is some typical code that you can use as a guide - <?php $usr = "xxxxxxxxxxx"; $pwd = "xxxxxxxxxx"; $db = "samdb"; $host = "xx.xxx.xxx.xxx"; $main_content = ''; // main content to be output on the page $errors = array(); // array to flag errors on the page and hold user error messages if(!mysql_connect($host, $usr, $pwd)){ // connect failed, handle the error here... $errors[] = "A fatal error occurred and this page is non-functional at this time!"; // user error trigger_error("Db connection failed: " . mysql_error()); // application error } else { // connect worked if(!mysql_select_db($db)){ // select db failed, handle the error here ... $errors[] = "A fatal error occurred and this page is non-functional at this time!"; // user error trigger_error("Select db failed: " . mysql_error()); // application error } } // if no errors at this point, proceed with the code that produces the database related content on the page... if(empty($errors)){ // GET THE CURRENTLY (last) PLAYING SONG INFO $query = "SELECT * FROM historylist ORDER BY date_played DESC LIMIT 1"; if(!$result = mysql_query($query)){ // query failed, handle the error here... $errors[] = "A fatal error occurred and this page is non-functional at this time!"; // user error trigger_error("Query failed: $query<br /> Due to: " . mysql_error()); // application error } else { // query worked if(!mysql_num_rows($result)){ // no matching rows $main_content .= "No currently playing song info was found!\n"; } else { // query matched at least one row, use the results from the query here... $row = mysql_fetch_assoc($result); $main_content .= "Currently playing title: {$row['title']}\n"; // build the content for the page... } } } // produce, format, and style error content from any user error messages if(!empty($errors)){ $error_content = ''; foreach($errors as $error){ $error_content .= "$error<br />\n"; } } // code on your page having nothing to do with the content from the database... ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Typical php produced page</title> </head> <body> <?php echo (!empty($error_content)) ? $error_content : ''; ?> <?php echo $main_content; ?> </body> </html> The trigger_error function used in the above code lets you generate an application error/warning/notice/deprecated message (default is E_USER_NOTICE) through php's built in error handling. You should always have the error_reporting setting set to E_ALL or even better a -1. For development, the display_errors setting should be set to ON. This combination of error_reporting/display_errors settings will cause all php detected errors to be reported and displayed in the browser. On a live server, display_errors should be set to OFF and the log_errors setting should be set to ON. This combination of error_reporting/display_errors/log_errors settings will cause all php detected errors to be logged. Quote Link to comment Share on other sites More sharing options...
JohnSmithers Posted June 8, 2011 Author Share Posted June 8, 2011 Tried to send you a PM. That sorted out a lot of ills and saved a bucket full of time. Perhaps you coluld recommend how I can thank you and this forum? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.