Hi,
I am receiving the following error when i run my code on localhost:
Error creating database: Can't create database 'library_db'; database exists
Warning: mysql_query() [function.mysql-query]: Access denied for user ''@'localhost' (using password: NO) in C:\Program Files\EasyPHP-5.3.6.0\www\databaseEx.php on line 45
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Program Files\EasyPHP-5.3.6.0\www\databaseEx.php on line 45
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.6.0\www\databaseEx.php on line 47
Fail
This is my actual PHP code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log In Example</title>
</head>
<body>
<?php
$con = mysql_connect("127.0.0.1", "root");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE library_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("library_db", $con);
$sql = "CREATE TABLE Staff
(
UserId int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(UserId),
Username varchar(65) NOT NULL,
Password varchar(65) NOT NULL
)";
// Execute query
mysql_query($sql,$con);
mysql_query("INSERT INTO Staff (UserId, Username, Password)
VALUES (0000, 'Ben', 'Password')");
mysql_close($con);
$result = mysql_query("SELECT Username FROM Staff
WHERE Username = 'Ben'");
$count = mysql_num_rows($result);
if($count==1){
echo "$result has logged in";
}
else {
echo "Fail";
}
?>
</body>
</html>
From what i can see, it looks as though i can connect to sql as the database is created but then it does not allow me to query the database?
I'd really appreciate any help as i'm trying to press on with a project and without the database i'm stuck at the moment.
Thanks.