Using the following code, the database can be connected to successfully. However, when trying to execute a query using the function 'executeNonQuery' I get the error { Access denied for user ''@'localhost' to database 'photobook' }. The user has full privileges, although strangely the query 'show grants' will not display that fact. However, when looking at user privileges in the GUI of mysql workbench it is. What can be the problem? I even used root, but the query is denied with the same error. The user can connect, but can't issue a query.
file: class.database.php
<?php
class Database {
function __construct(){
}
public function connect($host, $user, $pass){
$dbcnct = mysqli_connect($host, $user, $pass);
if(!$dbcnct){
die("Couldn't Connect: " . mysql_error());
}else{
echo "Connected Successfully";
}
}
public function disconnect(){
mysql_close($dbcnct);
if(!$dbcnct){
echo "Disconnected Successfully";
}
}
public function executeNonQuery($database, $sql){
mysql_select_db($database);
$retval = mysql_query($sql, $dbcnct);
if(!$retval){
die("Couldn't Update Data: " . mysql_error());
}
echo "Update Successful";
}
}
?>
<?php
include 'class.database.php';
$db = new Database;
$db->connect("localhost", "user", "pass");
$userdata=$_GET['data'];
$index=$_GET['index'];
echo $index . "<br/>";
foreach($userdata as $data){
$sql = "update photobook set photoName='" . $data . "' where photoPosition=" . $index;
$db->executeNonQuery("photoBook", $sql);
echo $data . "<br/>";
}
$db->disconnect();
?>
mysql_*functions they are deprecated. Use MySQLi or PDO instead. Also consider using prepared statements.mysqli_*andmysql_*mysqli_connectand thenmysql_*library