Jump to content

How To Resolve “Notice: Array To String Conversion In”


copperspeed

Recommended Posts

I can't figure out why I am receiving the following error Notice: Array to String conversion in 'path' line 462. Below is the code for line 462.

 

public function fetchOneBrg(array $conditions){

 

$db = Core::getInstance();

$sql = "SELECT * FROM ".USERS_BRG." WHERE ";

$params = array();

$i=0;

foreach ($conditions as $column => $value) {

if (preg_match('/^[a-z-.-_]+$/', $column)) {

if($i !=0){

$sql .= " AND ";

}

$sql .= "$column = ?";

$params[] = $value;

$i++;

}

}

$res = $db->dbh->prepare($sql);

Line 462: $res->execute(array_values($params));

return $res->fetch(PDO::FETCH_ASSOC);

 

 

$ststres = $db->dbh->prepare("SELECT * FROM ".USERS_STATS." WHERE user_id = :uID ORDER BY id DESC LIMIT :zero, :forty");

$ststres->bindParam(':uID',$udata['id'],PDO::PARAM_INT);

$ststres->bindValue(':zero',0,PDO::PARAM_INT);

$ststres->bindValue(':forty',40,PDO::PARAM_INT);

$ststres->execute();

$ststres=$ststres->fetchAll(PDO::FETCH_ASSOC);

 

I suspect it is the code above being passed in as a string to during the call array(['id'=>$ststres[ststval].to_id] below in Smarty. Doing a var_dump returns strings in array. How to resolve this and why is the string conversion happening on the ID's?

 

Example of var_dump:

string(4) "1066"

string(4) "1066"

string(4) "1066"

This is the call to the function:

{assign var='brgdatas' value=$brgObj->fetchOneBrg(array(['id'=>$ststres[ststval].to_id]))}

 

Thank you for your help.

Edited by copperspeed
Link to comment
Share on other sites

$ststres = $db->dbh->prepare("SELECT * FROM ".USERS_STATS." WHERE user_id = :uID ORDER BY id DESC LIMIT :zero, :forty");
$ststres->bindParam(':uID',$udata['id'],PDO::PARAM_INT);
$ststres->bindValue(':zero',0,PDO::PARAM_INT);
$ststres->bindValue(':forty',40,PDO::PARAM_INT);
$ststres->execute(); 
$ststres=$ststres->fetchAll(PDO::FETCH_ASSOC);

 

Var_dump of expect outcome from $ststres and what execute expects to receive:

 

Example of var_dump($ststres[ststval].to_id):

string(4) "1066"

string(4) "1066"

string(4) "1066"

 

{assign var='brgdatas' value=$brgObj->fetchOneBrg(array(['id'=>$ststres[ststval].to_id]))}

 

public function fetchOneBrg(array $conditions){

   $db = Core::getInstance();
               $sql = "SELECT * FROM ".USERS_BRG." WHERE ";
               $params = array();
               $i=0;
          foreach ($conditions as $column => $value) {
            if (preg_match('/^[a-z-.-_]+$/', $column)) {
               if($i !=0){
                       $sql .= " AND ";
                   }
           $sql .= "$column = ?";
           $params[] = $value;
           $i++;
       }
   }   
   $res = $db->dbh->prepare($sql);                
   $res->execute(array_values($params));
 return $res->fetch(PDO::FETCH_ASSOC);

 

I hope this makes more sense.

Link to comment
Share on other sites

I sure am. ARE YOU? Because you're the one trying to use it.

As I said twice before your code doesn't indicate at all WHAT LIBRARY OR CLASS your $db object is. If you're trying to say it's just the basic PDO in PHP, then look at the manual!

 

This is your code that you say has the problem. 

$res = $db->dbh->prepare($sql);
$res->execute(array_values($params));

 

(array_values is superfluous).

 

But later you posted this code:

$ststres = $db->dbh->prepare("SELECT * FROM ".USERS_STATS." WHERE user_id = :uID ORDER BY id DESC LIMIT :zero, :forty");
$ststres->bindParam(':uID',$udata['id'],PDO::PARAM_INT);
$ststres->bindValue(':zero',0,PDO::PARAM_INT);
$ststres->bindValue(':forty',40,PDO::PARAM_INT);
$ststres->execute(); 

 

Which part is giving you the error?

 

{assign var='brgdatas' value=$brgObj->fetchOneBrg(array(['id'=>$ststres[ststval].to_id]))}

 

Does it work when you use the function in PHP instead of in your Smarty tpl? You have a lot going on in that line of Smarty code, and the problem could be in there. 

Link to comment
Share on other sites

The issue are the [] you have around the array entry in -

value=$brgObj->fetchOneBrg(array(['id'=>$ststres[ststval].to_id]))}

That's making an array of array(s) and when you do the array_values, you are getting not an array of the values, but an array of the original nested array, which when accessed produces that notice (because of the 'Array' keyword that is returned), but actually supplies an array to the ->execute() method with a key/value that works (tested.)

 

Remove the []

Link to comment
Share on other sites

@PFMaBiSmAd I have tried this before.

 

If I remove the [] I receive the following error:

{assign var='brgdatas' value=$brgObj->fetchOneBrg(array('id'=>$ststres[ststval].to_id))}" - Unexpected "=>", expected one of: "","" , ")"'

 

According to Smarty.net if I use associative arrays within Smarty it requires [].

 

If you have a workaround I would love to read it.

Link to comment
Share on other sites

@jessica Listen, I don't want to argue with you but you are asking some basic questions when it clearly states what $db is in the function.

 

$db = Core::getInstanse(); Is clearly a call to the database for login and setup for querying.

 

If you don't know what the code below is and how it is used you don't know PDO because you wouldn't have asked what execute() means.

 

$ststres = $db->dbh->prepare("SELECT * FROM ".USERS_STATS." WHERE user_id = :uID ORDER BY id DESC LIMIT :zero, :forty");
$ststres->bindParam(':uID',$udata['id'],PDO::PARAM_INT);
$ststres->bindValue(':zero',0,PDO::PARAM_INT);
$ststres->bindValue(':forty',40,PDO::PARAM_INT);
$ststres->execute();

 

It is clear that I am passing the results from $ststres into the Smarty call which I showed you what the var_dump results were to be passed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.