问题
It seems as though my PHP
is trying to log in to the MySQL
database with a username I am not supplying.
The error I am getting is:
Warning: mysql_query(): Access denied for user 'radiocaf'@'localhost' (using password: NO) in /home/radiocaf/public_html/layout.php on line 16
Warning: mysql_query(): A link to the server could not be established in /home/radiocaf/public_html/layout.php on line 16
I am definitely supplying a password, and am not using "radiocaf" as the username in my connect file, so after 3 hours of staring, I still can't work out where I am going wrong.
Here is my code:
psl-config.php:
define("HOST", "localhost");
define("USER", "carl");
define("PASSWORD", "xxxxxxxxx");
define("DATABASE", "wlist");
db_connect.php
include_once 'psl-config.php'; // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
and then finally, the main page which is where I am receiving the error (I have cut out the HTML between the PHP:
ini_set('display_errors',1);
error_reporting(E_ALL);
//Include Connection PHP and connect
include_once('includes/db_connect.php');
//Check Connection
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
};
if (!$query = mysql_query("SELECT * FROM (
(SELECT * FROM users)
UNION ALL
(SELECT * FROM members)
) results
ORDER BY Name DESC")){
die("Error: " . mysqli_error($mysqli));
}
if (!$result = $mysqli->query($query)){
printf("Error: %s\n", $mysqli->error);
}
<HTML>
echo "<table border='0' cellpadding='0' cellspacing='0'>";
$x=0;
while($row = mysql_fetch_assoc($result)):
if ($x<10){
echo "<tr><td width='400' height='30' background='../images/green1.jpg'>".$row["Name"]."</td></tr>";
}
$x++;
if ($x == 10){
echo "<tr><td width='400' height'30' background='../images/green1.jpg'>More...</td></tr>";
break;
}
endwhile;
echo "</table>";
<HTML>
$mysqli->close();
The surrounding HTML
is just the layout of the page, essentially just a photoshop layout, sliced and exported to web.
I am fairly new to PHP
and so I hope this question is as explained as possible.
Edit:
Thanks so much guys, I apologise that this question seemed poor to some of you that you flagged it. Unfortunately I wouldn't have seen the "typos" as I really didn't know that I was attempting to use both mysql
and mysqli
and that they couldn't "communicate" with each other. Another issue I found was Dreamweaver uploaded the code from layout.php as db_connect.php. This doesn't explain (to me at least) how any connection was being made to bring up the access denied error for 'radiocaf'@'localhost' though.
Here's the old code lines I changed (in layout.php):
if (!$query = mysql_query("SELECT * FROM (
while($row = mysql_fetch_assoc($result)):
changed to:
if (!$query = "SELECT * FROM (
while($row = mysqli_fetch_assoc($result)):
And that's all it took, but I am entirely grateful! Thanks again everyone!
回答1:
You are conflicting MySQL
and MySQLi
. MySQL and MySQLi are two different methods
Warning is:
Warning: mysql_query(): .....
But you're connecting database with mysqli
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);.
Warning in php.net
:
MySQL 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.
回答2:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//Include Connection PHP and connect
//include_once('includes/db_connect.php');
$con=mysqli_connect(HOST, USER, PASSWORD, DATABASE);
//Check Connection
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
};
if (!$query = mysqli_query($con,"SELECT * FROM (
(SELECT * FROM users)
UNION ALL
(SELECT * FROM members)
) results
ORDER BY Name DESC")){
die("Error: " . mysqli_error($mysqli));
}
if (!$result = $mysqli->query($query)){
printf("Error: %s\n", $mysqli->error);
}
?>
<HTML>
<body>
<?php
echo "<table border='0' cellpadding='0' cellspacing='0'>";
$x=0;
while($row = mysqli_fetch_assoc($result)):
if ($x<10){
echo "<tr><td width='400' height='30' background='../images/green1.jpg'>".$row["Name"]."</td></tr>";
}
$x++;
if ($x == 10){
echo "<tr><td width='400' height'30' background='../images/green1.jpg'>More...</td></tr>";
break;
}
endwhile;
echo "</table>";
$mysqli->close();
?>
</body>
<HTML>
来源:https://stackoverflow.com/questions/33097971/warning-mysql-query-access-denied-for-user-adminlocalhost-using-passwo