Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.