guymclarenza Posted February 24, 2021 Share Posted February 24, 2021 I am testing some new code, trying to learn me some OOP. I know the SQL works because I tested it in phpmyadmin. There is something wrong and I cannot fathom why, <? $site= '15'; Class Dbh { private $user='xxx'; private $pass='xxx'; private $db='xxx'; private $host='localhost'; private $charset = 'utf8mb4'; protected function connect() { $dsn = 'mysql:host='.$this->host.'; dbname='.$this->db.';charset='.$this->charset; $pdo = new PDO($dsn, $this->user, $this->pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); return $pdo; } } Class GetData extends Dbh { public function gettable($site) { $sql = "SELECT table_cats.tablec_name, tables.table_question, tables.table_answer FROM table_cats INNER JOIN tables ON table_cats.tablec_id = tables.table_cat WHERE table_cats.tablec_site = ? ORDER BY table_cats.tablec_name"; $stmt = $this->connect()->prepare($sql); $stmt->execute($site); $fcid = $stmt->fetchAll(); foreach ($table as $table) { echo $table["tablec_name"]."<br /><br />"; echo $fquest = $table["table_question"]."<br /><br />"; echo $table["table_answer"]."<br /><br />"; } } } $tableob = new getData(); $tableob->gettable($site); The error messages I am getting are as follows [24-Feb-2021 06:02:34 UTC] PHP Warning: PDOStatement::execute() expects parameter 1 to be array, string given in /home/imagimediaco/public_html/entrepreneur.za.bz/faq.php on line 32 [24-Feb-2021 06:02:34 UTC] PHP Notice: Undefined variable: faq in /home/imagimediaco/public_html/entrepreneur.za.bz/faq.php on line 35 [24-Feb-2021 06:02:34 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/imagimediaco/public_html/entrepreneur.za.bz/faq.php on line 35 Line 32 is this line. $stmt->execute($site); I suspect the faq variable is undefined because it's not being populated because it expects $site to be an array, Why is an array expected for $site? Please explain to me in simple terms, I can be a little stupid. Quote Link to comment https://forums.phpfreaks.com/topic/312208-errors-that-make-no-sense-to-me-what-am-i-doing-wrong/ Share on other sites More sharing options...
requinix Posted February 24, 2021 Share Posted February 24, 2021 56 minutes ago, guymclarenza said: Why is an array expected for $site? Please explain to me in simple terms, I can be a little stupid. https://www.php.net/pdostatement.exec The next problem is the faq variable, but there is no "$faq" in the code you posted. After that is the foreach problem. Where is $table being defined? Then after you fix that you may or may not have another problem with the foreach - depends what you do to fix the $table issue. Quote Link to comment https://forums.phpfreaks.com/topic/312208-errors-that-make-no-sense-to-me-what-am-i-doing-wrong/#findComment-1584741 Share on other sites More sharing options...
guymclarenza Posted February 24, 2021 Author Share Posted February 24, 2021 foreach ($table as $table) is the line that contains the faq variable in the code, I replaced it with table to anonymise my code. Quote Link to comment https://forums.phpfreaks.com/topic/312208-errors-that-make-no-sense-to-me-what-am-i-doing-wrong/#findComment-1584742 Share on other sites More sharing options...
Solution kicken Posted February 24, 2021 Solution Share Posted February 24, 2021 59 minutes ago, guymclarenza said: Why is an array expected for $site? A query can have more than one parameter (eg, multiple WHERE conditions), as such PDO::execute() needs to be able to accept more than one value to bind to those parameters. The way it does that is by taking in an array of values (even if it only needs one). Pass an array to execute with $site as an element of that array to fix that problem. $stmt->execute([$site]); Your foreach error is due to the variable you provide ($table) not being an array (or other Traversable object). You don't actually define $table anywhere in your code, it just magically appears. If you want to loop over the results of your query, then you have a couple options. PDO Statements can be used directly in foreach, so just loop over $stmt. If you do this, you do not call $stmt->fetchAll() first. Grab the results using $stmt->fetchAll() and loop over that variable ($fcid in your code). Quote Link to comment https://forums.phpfreaks.com/topic/312208-errors-that-make-no-sense-to-me-what-am-i-doing-wrong/#findComment-1584743 Share on other sites More sharing options...
guymclarenza Posted February 24, 2021 Author Share Posted February 24, 2021 Thank you kind sirs, Quote Link to comment https://forums.phpfreaks.com/topic/312208-errors-that-make-no-sense-to-me-what-am-i-doing-wrong/#findComment-1584744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.