woodsonoversoul Posted May 31, 2007 Share Posted May 31, 2007 Alright what about this, I´m getting an unable to connect message with the code: // set database server access variables: $host = "localhost"; $user = "dan"; $pass = "------"; $db = "testdb"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); when I´ve just created the database. I´m using my root name and password. I didn´t set up any user privliges so I assumed access would be granted under root, is this incorrect or is there a problem with my code? Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/ Share on other sites More sharing options...
pocobueno1388 Posted May 31, 2007 Share Posted May 31, 2007 You need to select your database using mysql_select_db () Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265931 Share on other sites More sharing options...
jonahpup Posted June 1, 2007 Share Posted June 1, 2007 you need to use mysql_select_db($db) or die ("Could not select database because ".mysql_error()); or similar to tell the server which database to look at. Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265938 Share on other sites More sharing options...
woodsonoversoul Posted June 1, 2007 Author Share Posted June 1, 2007 The entire code is: </head> <body> <?php // set database server access variables: $host = "localhost"; $user = "dan"; $pass = "root"; $db = "testdb"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM symbols"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=10 border=1>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?> </body> </html> So I'm selecting the database right after I try to establish a connection, is there a problem there? Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265940 Share on other sites More sharing options...
trq Posted June 1, 2007 Share Posted June 1, 2007 What does.... $connection = mysql_connect($host, $user, $pass) or die (mysql_error()); give you? Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265942 Share on other sites More sharing options...
woodsonoversoul Posted June 1, 2007 Author Share Posted June 1, 2007 using <?php $connection = mysql_connect($host, $user, $pass) or die (mysql_error()); ?> I get Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/connectest.php on line 3 Access denied for user 'www-data'@'localhost' (using password: NO) Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265966 Share on other sites More sharing options...
woodsonoversoul Posted June 1, 2007 Author Share Posted June 1, 2007 anymore advice? All help is greatly appreciated Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-265994 Share on other sites More sharing options...
Fergusfer Posted June 1, 2007 Share Posted June 1, 2007 I get Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/connectest.php on line 3 Access denied for user 'www-data'@'localhost' (using password: NO) This warning reveals that the connection is not being created using the credentials you intend. For example, "www-data" is the Apache process. This is the user that would be used to connect if no user were specified. $user is not initialized correctly. Since your server is reporting the problem at line 3 and there is no connect statement at line 3 in the code you show us, I also conclude that the code you posted is not the code you are using. That makes it very hard to help you. Check the initialization of $user. Check the other variables $host, $pass, and $db. If $user is not initialized, the others may not be either. Link to comment https://forums.phpfreaks.com/topic/53804-problem-connecting-to-a-database/#findComment-266001 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.