I am trying out following code:
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include 'config.php';
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT name, gender, age FROM student';
mysql_select_db('college');
echo $sql;
//echo $conn[] ;
$retval = mysql_query($sql, $conn);
if (!$retval) {
die('Could not get data: ' . mysql_error());
}
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Name :{$row['name']} <br> " .
"Gender : {$row['gender']} <br> " .
"Age : {$row['age']} <br> " .
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
</body>
</html>
config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'college');
/* Attempt to connect to MySQL database */
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
#$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
I am getting following error:
Could not get data: Access denied for user ''@'localhost' to database 'college'
As per this thread, I have run:
GRANT ALL ON *.* to root@'%' IDENTIFIED BY 'root';
in phpAdmin and it worked. But still getting same error.
If I remember this correct, I was able to work with college
database using root
user earlier, but now I am getting this exception. Whats going wrong here?