copperspeed Posted December 18, 2012 Share Posted December 18, 2012 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. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/ Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 Please use our code tags, your post is very hard to read, especially with the background color. Most likely, the execute function expects a string with your SQL query, then the parameters to bind. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400165 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 @Jessica but how do I resolve it? Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400167 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 Well it depends what your database class expects. You haven't said what class you're using, or any info on it. Look at what execute() expects to receive as arguments. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400169 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 $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 https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400172 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 That has nothing to do with what I said. Clearly execute() wants a string. Did you write the function execute() or are you using a third party library? Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400173 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 Jessica are you familiar with PDO because execute() is a function of it? PDO protects your code from sql injection. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400174 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 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 https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400177 Share on other sites More sharing options...
PFMaBiSmAd Posted December 18, 2012 Share Posted December 18, 2012 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 https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400180 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 @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 https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400182 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 Nevermind, re-read the function. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400186 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 @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 https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400190 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 Good luck dude. Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400191 Share on other sites More sharing options...
copperspeed Posted December 18, 2012 Author Share Posted December 18, 2012 Solved: The end result was to remove array() from Smarty call like so: {assign var='brgdatas' value=$brgObj->fetchOneBrg(['id'=>$ststres[ststval].to_id])} [/color] Link to comment https://forums.phpfreaks.com/topic/272150-how-to-resolve-%E2%80%9Cnotice-array-to-string-conversion-in%E2%80%9D/#findComment-1400197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.