简体   繁体   中英

Php Syntax help for a noobie

Im working on a noobie app to pull some dates from the database and then a year later send me an alert (with cron). So far im struggling a bit and am trying to make a page which does my maths which I can work on building up.

So far ive been debugging all day and reading tutorial after tutorial but am still getting errors. Very frustrating :)

My code looks like this....

<?php
$con = mysql_connect("localhost","root", "etc");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("db", $con);

$result = mysql_query('SELECT CheckDate FROM Data') or exit(mysql_error());
while ($row = mysql_fetch_array($result))
{
extract($row);

$validfromdate = CheckDate;

$alert=strtotime("+1 year", $validfromdate);

echo $alert;

?>

Here are a couple of thoughts :

You should not use the "root" account (or any account that has administration privileges) to connect to MySQL, except when it's needed -- here, it is not


Also : you should probably not use extract : it'll create variables out of the blue -- if one day you add a column in your table, it'll mean a new variable in your PHP script, which could conflict with an existing one (not much risk here as you are not doing a "select *" ; but, one day or another, ...)

  • instead, use syntax like $row['CheckDate']
  • with extract , still, CheckDate is a variable, which means you have to prefix it with a $ : $CheckDate

For more informations, see the extract manual page : in the example, there are $ in front of each variables -- actually, that's the syntax for variables in PHP ;-)


A third one : it seems there is some "}" missing at the end of your script, to mark the end of the while-loop.


And, one more for the road : when developping you should use the highest possible error-reporting level ; that would have indicated you the "CheckDate" with no $ was wrong, here.

For more informations, you can take a look at :

Using something like this at the top of your script will help :

error_reporting(E_ALL);
ini_set('display_errors', 'On');

Or, in the php.ini file :

error_reporting = E_ALL
display_errors = On

(You'll have to find and replace the existing values)


Hope this helps... And have fun !

> $validfromdate = CheckDate;

也许你是说

$validfromdate = $row["CheckDate"];

Try:

$validfromdate = $row['CheckDate'];

$validfromdate = CheckDate;

should be

$validfromdate = $CheckDate;

Your're also missing a close-brace for the loop.

You would be better off accessing $row as an array rather than calling extract on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM