"undefined variable" error

it seems to be something in how i set up the database. will take a screen shot & post.
thx
D

wow…i love sitepoint but posting here has been a bit of a challenge lately.
Couldn’t attach the screenshot but have it on my google acct.
https://plus.google.com/u/0/photos/114950823627120269815/albums/6056742138651189521/6056742143319490578?pid=6056742143319490578&oid=114950823627120269815

& here is flicker if it is easier
Imgur

Unsigned simply means that the integer must be positive (or 0). Your field is set to be auto-incremented upon new insertions anyway, which means the column will only contained unsigned integers anyway (AUTO_INCREMENT in MySQL starts from 1). I have already outlined the problem in my above post - your query isn’t returning any results.

Your screenshot does, however, show us that the table we are dealing with is called users, and not polls. That’s most probably why you aren’t finding any records…

no…the db connects. but will go double check and see if that is it.
thx
D

i think i am correct.
the code is calling on polls not users.

<?php
require_once 'init.php';
$pollsQuery = $db->query("
	SELECT id, question
	FROM polls
	WHERE DATE(NOW()) BETWEEN starts AND ends
	
");
var_dump($pollsQuery);
while($row = $pollsQuery->fetchObject()){
	$polls[] = $row;
}
echo '<pre>',  print_r($polls) ,'</pre>';
?>

& in init i am accessing the correct db

<?php
session_start();
$_SESSION['user_id'] = 1;

$db = new PDO('mysql:host=localhost; dbname=website', 'root', '');
?>

Could you please execute the following and tell me what is output:


$q = $db->query('show tables');
print_r($q->fetchAll(PDO::FETCH_ASSOC));

I tried but am not sure where i should have entered the code.

<?php
require_once 'init.php';
$pollsQuery = $db->query("
	SELECT id, question
	FROM polls
	WHERE DATE(NOW()) BETWEEN starts AND ends
");
var_dump($pollsQuery);
	while($row = $pollsQuery->fetchObject()){
		$polls[] = $row;
		$q = $pdo->query('show tables');
	   print_r($q->fetchAll(PDO::FETCH_ASSOC));
}
echo '<pre>',  print_r($polls) ,'</pre>';

?>

I am guessing that is incorrect?

Paste it in after the require statement. The query simply shows the tables you have in your database, which should confirm one of my previous posts that you’re querying the incorrect table.

Array ( [0] => Array ( [Tables_in_website] => polls ) [1] => Array ( [Tables_in_website] => polls_answers ) [2] => Array ( [Tables_in_website] => polls_choices ) [3] => Array ( [Tables_in_website] => users ) ) object(PDOStatement)#3 (1) { ["queryString"]=> string(81) " SELECT id, question FROM polls WHERE DATE(NOW()) BETWEEN starts AND ends " }


Notice:  Undefined variable: polls in C:\\xampp\\htdocs\\pollPhp\\index.php on line 14

Hope this help.
D

Right, so your query isn’t returning any results because it isn’t finding any matches then.

ok so will start from scratch.
however as you can see my php skills are pretty meager right now and i def need to brush up on that little i knew.
can you please tell me brake down that printed row?
i see it recognized that there are the tables i created in the “website” database as it should be. So why is it not reading the questions?
thx
D

If you need all fields of all records returned by a query then you should consider using fetchAll http://php.net/manual/en/pdostatement.fetchall.php to grab all the rows of the result set in one hit.

Before grabbing the result set, add:

$polls=array();

That will set up the $polls variable as an empty array so that any function handed $polls that expects to be given an array to work with won’t generate any error.

Reread my first post to this thread for an explanation as to why you’re receiving the error. At the moment, your query is not finding any results because the WHERE clause in your SQL statement has specified a condition that doesn’t match any rows in your table.

As evidence of this, execute the following code snippet in place of your previous one (here):

<?php
require_once 'init.php';
$pollsQuery = $db->query("
  SELECT id, question
  FROM polls
");

$polls = array();
while($row = $pollsQuery->fetchObject()){
    $polls[] = $row;
}
echo '<pre>',  print_r($polls) ,'</pre>';

I’ve simply omitted your WHERE clause condition and put in an empty definition for your $polls variable (as said in my initial post). You should now see results and no errors.

2 Likes

ok thx. back to coding.
D

Thank you tpunt that workd and it returned the two test questions i had

but i still don’t understand why it was “unassigned” i guess. i re read several time your answer.

but i don’t understand why the query wouldn’t return the rows. everything connected. the rows are there. w/auto increments.
Just trying to understand this.
thx
D

“unsigned” is an SQL thing.

For example, if you have a field as tinyint but aren’t using any negative numbers it can only go up to 127 if signed… But it can go up to 255 if unsigned

True, you could change the data type, but why “waste” the allowance for negative numbers if you’re not using them?

all right, thank you Mittineague, (still a bit confused but pretty sure that won’t change soon ) but how could i have avoided getting the error message i got?
i attached a screenshot of the database.
thx
D


Okay, so having seen a screenshot of the data in your table, things are clear why your initial query was finding no results. Your initial query was:

 SELECT id, question
 FROM polls
 WHERE DATE(NOW())BETWEEN starts AND ends

So let’s dissect your WHERE clause. The NOW() function provides the current time with an accuracy of seconds, like this: 2014-09-09 21:57:24. Now when this value is wrapped in the DATE() function, the accuracy drops to days (effectively truncating the value). So the aforementioned NOW() value would look like the following when passed into DATE(): 2014-09-09.

Next we have the BETWEEN clause which will perform a check that the left hand operand (our 2014-09-09) is between the specified start and end date (inclusive). Now looking at your two records, your start dates are both 2014-09-05, however your end dates are before both of your start dates, which means that you will never find any dates that start from 2014-09-05 to 0000-00-00. If you switched the two dates around, then you’ll find plenty of dates (including your DATE(NOW()) function).

Now you keep asking about your error message (which is actually simply a notice): Notice: Undefined variable: polls in C:\xampp\htdocs\pollPhp\index.php on line 11

As explained in my first post, the problem is with the following:

while($row = $pollsQuery->fetchObject()){
    $polls[] = $row;
}

If your query finds no results (and it didn’t, for the aforementioned), then the condition of your while loop ($row = $pollsQuery->fetchObject()) will return false, and your while body ($polls = $row;) will not execute. This means that the $polls variable will never be created, and thus will produce a warning when you try to use it in the following line:

echo '<pre>',  print_r($polls) ,'</pre>';

The solution would be to define the $polls variable above the while loop, so that in case your query does not find any results, your $polls variable will still be defined (albeit as an empty array).

I hope that clears everything up - just let me know if you need anything else explained.

Yes much clearer thx. what i find annoying is that i did define the end date in same dd & mm, but w/2015 as the end year.
when i saved i saw them changed 00-00-0000. i thought that it was some how normal. next time i’ll know better thx you all for your time
D