简体   繁体   中英

WAMP PHP Error: Can not connect to MySQL

I need help with problem that appear on my website while I tried to connect to mysql. so, this is the error:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\\wamp\\www\\sites\\mysite\\core\\connection.php on line 9

Call Stack

    Time    Memory  Function    Location
1   0.0007  163008  {main}( )   ..\index.php:0
2   0.0012  165376  include( 'C:\wamp\www\sites\mysite\core\connection.php')    ..\index.php:1
3   0.0015  166400  mysql_connect ( )   ..\connection.php:9
( ! ) Warning: mysql_connect(): in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
    Time    Memory  Function    Location
1   0.0007  163008  {main}( )   ..\index.php:0
2   0.0012  165376  include( 'C:\wamp\www\sites\mysite\core\connection.php')    ..\index.php:1
3   0.0015  166400  mysql_connect ( )   ..\connection.php:9
MySQL Error: Accטs refusי pour l'utilisateur: 'root'@'@localhost' (mot de passe: OUI)

the php code to connect that I wrote, is:

<?php
session_start();

$dbhost = "localhost"; 
$dbname = "users"; 
$dbuser = "root"; 
$dbpass = "root"; 
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

因为您使用的是不推荐使用的扩展名,所以请使用mysqliPDO (不推荐使用mysql扩展名)

mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

By default, your MySQL credentials for WAMP are:

username: root and no password.

$dbhost = "localhost"; $dbname = "users"; $dbuser = "root"; $dbpass = "";

And as other users mentioned, try to use mysqli or pdo as mysql is deprecated, but it's not your issue here.

Good luck!

If your using the default user of phpmyadmin and didn't set a new password than the password would be an empty string (no password), unless you set your password to root. Your code is correct, although you should look into mysqli when you feel like you can handle it.

mysql_connect — Open a connection to a MySQL Server. This extension was deprecated in PHP 5.5.0 , and it was removed in PHP 7.0.0 . Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_connect()
PDO::__construct()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM