0

I am trying to run a simple SELECT query but I get the following error:

Access denied for user ''@'localhost' (using password: NO)

It seems like I'm not connected to MySQL with my username and password, but I do have a PDO object with my connection parameters :

$_connection = 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"));

How can I use this connection to execute my query? Here's the query that works in my 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;
    }
}

I could pass the PDO object in the function but then I don't know where to use it !

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Natty
  • 497
  • 1
  • 11
  • 23
  • 3
    check what is the value of ` $PARAM_user` – RAUSHAN KUMAR Jun 15 '17 at 12:10
  • From the error, nothing is in `$PARAM_user` AND nothing is in `$PARAM_password` – RiggsFolly Jun 15 '17 at 12:10
  • [don't use mysql extension](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) as it's [deprecated and not safe](http://php.net/manual/en/mysql.php) – giorgio Jun 15 '17 at 12:11
  • 2
    **you are connecting with PDO and attempting to query using the old `mysql_` database extension.** Woops two different animals **completely** – RiggsFolly Jun 15 '17 at 12:12
  • Less _Ukulele_ more attention to details – RiggsFolly Jun 15 '17 at 12:16
  • The `$PARAM_user` is set, but it does not change anything since I am not using the PDO object to execute my query, if you know how to that could help ! – Natty Jun 15 '17 at 12:16
  • Yea sure, here is how... [Its called the manual](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jun 15 '17 at 12:17
  • @RiggsFolly Yes I know ! I would like to use my PDO object but I don't know how :( Ukulele is love, ukulele is life, watch what you're saying ;) – Natty Jun 15 '17 at 12:17
  • :) I could respond with watch what you are coding – RiggsFolly Jun 15 '17 at 12:23
  • Its like Riggs already said... you're connecting with PDO, and then you're trying to use mysql. Thats like you go in your car, start it and then try to drive with the neightbars car. That won't work. You're mixing up two different things. Either you connect with mysql (deprecated, so not recommended) - or you learn how to query the database with PDO (Takes you 3 seconds to google for millions or results) :) – Twinfriends Jun 15 '17 at 12:35

0 Answers0