开发者

How do I connect to mysql from php?

I'm working through examples from a book on php/mysql development. I'm working on a linux/apache environment.

I've set up a database and a user. I attempt to connect with this line of code:

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

I get this error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)

I can only guess what is happening here: I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?

How do I pass in the credentials I've defined for my user ?

edit: By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.

they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.

Both my scripts can be seen here: http://pastebin.com/MUneLEib

#

Solved:

Thanks guys.

A couple of you pointed out that I had coded carelessy.

Also, I was particularly pl开发者_如何转开发eased by Neo's answer: he told me why the username of the owner of the apache process was being used.

:)


I just looked at your code! The variable with the username is $database_username but you are using $db_username.. Change your code to:

$db_server = mysql_connect($db_hostname, $database_username, $db_password);

or you could change the line with username with: $db_username='[your mysql user]';//or the username you created

When you don't pass anything it will be the user mysql assumes but it will not get the password so if you hadn't defined $db_password it would say: (using password:NO)

you set $database_username with you user but you are passing $db_username which is not set so the user is the linux username as default when nothing is passed with the password for the mysql user! Since there is no mysql user with that password or privileges or even with that name you are not given access!

That user is www-data which is as you guessed an apache user assigned to client-side requests!


In your login.php you use the variable $database_username, but in your connection function you use $db_username. Try matching them up.


The username goes into $db_username, and the password goes into $db_password.


All these other answers are so presumptuous, as if you don't know that $db_username means database username and same for password.

The error says that you've specified an username and password. You just specified the wrong ones. You need to use the username and password of MySQL, NOT the system username/password combination, so no, this will not be www-data. This may be root and some password, but again, these credentials are specified within MySQL, and are not (necessarily) the same as the system users and passwords.

Your MySQL installation should have a root user with a default password (which you should promptly change). There are several options: you can add an user via the MySQL command line or use an interface like cPanel or Webmin if your provider has something like this; I've used both of these and they both have easy interfaces to add new MySQL users and assign them privileges.

Also just a tip: I typically create one user per database and give the user full privileges on the database, and then use that user with the application linked to the database.

And then of course, once you create a MySQL user account and give it privileges on your web app's database, fill in that username and password into your script.


<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>


Make sure you actually assign your database login username and password to those variables before you try to connect. For example, before the line with mysql_connect():

$db_username = 'myuser';
$db_password = 'mypass123';


I'd recommend you go through a tutorial regarding MySQL and PHP before trying to go any further - just so you understand how it all works.

Try this one: http://www.tizag.com/mysqlTutorial/mysqlconnection.php

Also, documentation is your friend: http://php.net/manual/en/function.mysql-connect.php


The error given could mean that the privilege has not been granted, or perhaps the password is incorrect. Check that user is created and granted access, or just issue this to be sure it set to allow access on the mysql server.

$ mysql -u root -p
password:

(blah blah blah from server)

GRANT ALL PRIVILEGES ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';

If all privileges is too much, consider giving only the basic permissions needed:

GRANT SELECT,UPDATE,DELETE ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';

By the way, the server response to this statement on success is Query OK, 0 rows affected.


Your code is correct, it is saying your password is incorrect so your mysql database is rejecting the mysql connection. If you are using xampp use:

localhost

as hostname &

root

as username & in xampp there is no password so it would be

$password = ""; then $dblink = new mysqli($hostname, $dbuser, $password, $dbname);

so you can set your vars like this

<?php

    $hostname = "localhost";
    $dbuser = "root";
    $password = ""; // means there is no password to the database
    $dbname = "test"

?>

Conclusion :

if you get this as your message -

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)

It means the password you entered is wrong or there is no password.


Stop using mysql_connect()!

You should not use this or other related functions related to this extension.

It's depreciated and removed in PHP 7.0.0 and beyond. An alternative is using PDO or MySQLi. Below is an example script for connecting to MySQL using PDO:

<?php

  /* Connect to a MySQL database using driver invocation */

  /* DSN options:
     http://php.net/manual/en/ref.pdo-mysql.connection.php

     Error handling options:
     http://php.net/manual/en/pdo.error-handling.php

     Learn how to secure against SQL injection attacks:
     http://php.net/manual/en/pdo.prepared-statements.php
  */

  $user = 'myusername';
  $pass = 'mypassword';
  $host = 'localhost';
  $mydb = 'mydatabase';

  $dsn = "mysql:dbname=$mydb;host=$host"; 

  try {
      $dbh = new PDO($dsn, $user, $pass);
  } catch (PDOException $e) {
      echo 'Connection failed: ' . $e->getMessage();
  }

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜