2013-02-22 140 views
0

嘗試使用以下代碼連接到localhost sql db(此時不做任何與查詢有關的查詢只是希望查詢執行):拒絕用戶'@'localhost'的訪問(使用密碼:NO)

<?php 

$host  = "localhost"; 
$port  = 3306; 
$socket = ""; 
$user  = "root"; 
$password = "Password1"; 
$dbname = "CIIP_WIKI"; 

$con = new mysqli($host, $user, $password, $dbname, $port); 

if(!$con) 
{ 
echo ("db connection failed!"); 
die ('Could not connect to the database server' . mysqli_connect_error()); 
} 

else { 
    echo ("db connection established."); 
    echo ("<br/>"); 
    } 

$query = sprintf("SELECT first_name FROM actor WHERE actor_id='1'"); 

$result = mysql_query($query); 

$con->close(); 

?> 

我不斷收到以下...

Welcome 

db connection established. 

Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31 

Warning: mysql_query(): A link to the server could not be established in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31 

爲什麼這個不行?

+0

你混合了mysql和mysqli的。留一個API。 – 2013-02-22 00:53:25

+0

Mysql_ *和mysqli是兩個不同的API – 2013-02-22 01:00:20

+0

可能的重複[PHP MySQLi不識別登錄信息](http://stackoverflow.com/questions/11589210/php-mysqli-doesnt-recognize-login-info) – hakre 2013-07-01 07:25:14

回答

16

這是因爲您使用mysqli_創建連接,然後使用mysql_嘗試獲取結果。他們是不同的API。

<?php 
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); 

if (!$link) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
      . mysqli_connect_error()); 
} 

echo 'Success... ' . mysqli_get_host_info($link) . "\n"; 

mysqli_close($link); 
?> 

See the manual

相關問題