Jump to content

Errors that make no sense to me. What am I doing wrong?

Go to solution Solved by kicken,

Recommended Posts

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_EMULATE_PREPARES, false);
			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);
			$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(); 


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.


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. 

Link to post
Share on other sites
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. 


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.

Link to post
Share on other sites
  • Solution
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.


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.

  1. PDO Statements can be used directly in foreach, so just loop over $stmt.  If you do this, you do not call $stmt->fetchAll() first.
  2. Grab the results using $stmt->fetchAll() and loop over that variable ($fcid in your code).


Link to post
Share on other sites

Join the conversation

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

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.