我正在运行一个简单的SELECT查询,但是我收到以下错误:
Access denied for user ''@'localhost' (using password: NO)
正如您所看到的,似乎我没有使用我的用户名和密码连接到mySQL,但我确实有一个带有连接参数的PDO对象:
$_connexion = new PDO('mysql:host='.$PARAM_host.';port='.$PARAM_port.';dbname='.$PARAM_dbname,
$PARAM_user, $PARAM_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
如何使用此连接来执行查询?这是查询(在我的DBMS中运行良好):
public static function getRoleByCuID($id){
$sql = "SELECT `RoleDetail_ID` ";
$sql .= "FROM common.role_detail ";
$sql .= "WHERE `RoleDetail_ID` IN (SELECT `RoleDetail_ID` ";
$sql .= "FROM common.role_application ";
$sql .= "WHERE `RoleApplication_ID` IN (SELECT `RoleApplication_ID` ";
$sql .= "FROM common.habilitation WHERE `CommonUser_ID` = '".$id."' AND `Application_ID` ='".APP_ID."'));";
//This is where the error is returned, in the 'or die' part
$result = mysql_query($sql) or die("Erreur de la requete 1 (serviceRole) ".$sql.mysql_error() );
if(mysql_num_rows($result) != 0){
return mysql_result($result, 0, 'RoleDetail_ID');
}
else {
return 0;
}
}
我可以在函数中传递PDO对象,但后来我不知道在哪里使用它!