hmmm. tried posting this eariler but sitepointforum’s databases seem to be ready to check out for the weekend w/a couple of beers lately.
so…Am trying out a phpaccademy tutorial to create a poll.
Am getting this error:
Notice: Undefined variable: polls in C:\xampp\htdocs\pollPhp\index.php on line 11
line 11 has “echo ‘<pre>’, print_r($polls) ,‘</pre>’;”
I have this in the index.php.
<?php
require_once 'init.php';
$pollsQuery = $db->query("
SELECT id, question
FROM polls
WHERE DATE(NOW())BETWEEN starts AND ends
");
while($row = $pollsQuery->fetchObject()){
$polls[] = $row;
}
echo '<pre>', print_r($polls) ,'</pre>';
?>
Not sure why $polls is returning as undefined. Unless there are no records being returned from the query. Did you run the query in a QA? Also, it’s been a while since I worked with PHP, but isn’t the period (.) supposed to concatenate, not the comma (,)?
The only way your $polls variable would be undefined is if the query returned no rows, thereby making the first assignment to the $row variable in the condition of your WHILE loop return false (and so not executing the loop body). So there’s no error in your code; you just simply aren’t finding any results. Good practice, however, would dictate that you first initialise the $polls variable before trying to push elements onto it. Therefore, if no rows were found, you’d be printing out an empty array rather than seeing an ugly error (as is your case). (It’s also marginally faster to push items onto initialised arrays.)
So you may want to consider adding the following line before you while loop:
$polls = array();
You’re correct in that PHP uses the period to concatenate strings, however in this case you can use commas to effectively pass arguments to echo to output to the screen. This makes echo (and print, for that matter) a variadic function (though strictly speaking, echo and print language constructs, not functions - but let’s not go down that road :))
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…
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.
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
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):
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.
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