Jump to content

Problem connecting to a database


Recommended Posts

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

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?

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)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.