"undefined variable" error

“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

Could i please get some advice on this code? it is part of the same project so posting a new topic seems like bad idea.[code]

<?php require_once 'app/init.php'; if(!isset($_GET['poll'])){ header('Location: index.php'); }else{ $id=(int)$_GET['poll']; $pollQuery = $db->prepare(" SELECT id, question FROM polls WHERE id = :poll AND DATE (NOW()) BETWEEN starts AND ends "); $pollQuery->execute([ 'poll'=>$id ]); $poll = $pollQuery->fetchObject(); print_r($poll); } ?>

[/code]
& this is the error msg i got in xampp

[quote]SQL query: Documentation

SELECT id, question
FROM polls
WHERE id = :poll
AND DATE (NOW()) BETWEEN starts AND ends
LIMIT 0, 25
MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘:poll
AND DATE (NOW()) BETWEEN starts AND ends
LIMIT 0, 25’ at line 3
[/quote]
thank you

I can’t see where the problem is in your PHP code. Is it your PHP code generating that error, or are you pasting the SQL code from your query directly into a SQL query executor (like the one in PHPMyAdmin or MySQL WorkBench)?

Regarding your code, I have two criticisms. The first is that your query is retrieving the id attribute from your polls relation even though you already have the ID value (from $_GET['polls']). The second (and perhaps a little pedantic) criticism is that you’ve already ensured that the $id variable has a safe value (because of the explicit type cast (int), forcing it to be an integer), and so putting it through a prepared query seems a little redundant. (I’ve never really been a strong advocate of the Defense in Depth security principle, since redundant safeguards seem like a waste of resources to me).

If all your users are benign and/or in an office environment, you probably don’t have much to worry about. But if a site/app is public facing - redundancies are paramount.

Remove the [ and ] from the execute, they’re messing up the execution of the prepared statement

PDO’s execute() method requires either no arguments passed, or a single array argument (either an associative array with the keys being the named placeholders, or a simple indexed array for positional placeholders). He therefore requires those square brackets to denote the argument as an array.

I still can’t see a syntax error in OPs SQL code. The only thing that I can think of (as I said above) is that he’s pasting the SQL code as-is (including the named placeholder :polls) into some sort of SQL code executor.

Thank you SpacePhoenix for the suggestion but that didn’t work. tpunt. appreciate the explanation. I don’t know what “he” is doing, but as for me. nope am typing the code as i see it. (and the cat never learned how to type…)
the vid is using mysql pro i think, i am using xampp
just in case, here it is.

If you do a var_dump on $id what is output by the var_dump?

not sure where in the code i should put that sorry.
D

after that line

sorry was multitasked away.
I did

}else{
	$id=(int)$_GET['poll'];
	var_dump ($id);

& got

thx
D

Looking at the info about execute() it says:

What’s the field type of the ID field?

the id is a integer.

Looks like the ID isn’t binding as execute is trying to bind a string to what is supposed to be an integer. You’re going to have to move the place where the binding is done

$pollsQuery->$sth->bindParam(‘poll’, $id, PDO::PARAM_INT);

tried. thanks for your time but at this point i give up. will try again later.

Can you please post the code as it currently stands? Does the tutorial that you’re working from have a place where the codebase can be downloaded from?

Hello, sorry been busy w/other task but thank you for trying to help.
i’ll try this again over the weekend and post the files on g+ then.
you all have a great day