I've got a simple PHP page with a list of events that are taking place at certain times on certain days, and this is my code:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("events"); 
//select the table
$result = mysql_query("select * from weekevent order by eventdate LIMIT 20");
//grab all the content
while($r=mysql_fetch_array($result));
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $event=$r["event"];
   $eventdate=$r["eventdate"];
echo "<tr><td><b>$event</b></td><td>showing on $eventdate</td>";
}
	// assumes that you have connected successfully to your database using mysql_connect and mysql_select_db
$result = mysql_query("select eventdate from events"); // runs a query in your database
if($result)
{
   $row = mysql_fetch_assoc($result); // gets the first result set row from the database for the query
   $databaseDate = $row["eventdate"]; // that is, reference the column name of your table
   $formattedDate = date("Y-m-d G:i:s", strtotime($databaseDate)); // this puts the date into the format "yyyy-mm-dd h24:mm:ss"
}
?>

yet I get this error:

Notice: Undefined variable: formattedDate in C:\www\vhosts\eventsweb\eventsweekly.php on line 20

What should I do so it shows events properly?

Recommended Answers

All 10 Replies

Have you tried this?

$formattedDate = date("Y-m-d G:i:s", $databaseDate);

I could be completely wrong but it would seem as though you wouldn't need strtotime() .
Correct me if I'm wrong.
You could also use

$databaseDate = strtotime('your preferred date format');

Where do I put it in the code?

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("events"); 
//select the table
$result = mysql_query("select * from weekevent order by eventdate LIMIT 20");
//grab all the content
while($r=mysql_fetch_array($result));
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $event=$r["event"];
   $eventdate=$r["eventdate"];
echo "<tr><td><b>$event</b></td><td>showing on $eventdate</td>";
}
	// assumes that you have connected successfully to your database using mysql_connect and mysql_select_db
$result = mysql_query("select eventdate from events"); // runs a query in your database
if($result)
{
   $row = mysql_fetch_assoc($result); // gets the first result set row from the database for the query
   $databaseDate = $row["eventdate"]; // that is, reference the column name of your table
   $formattedDate = date("Y-m-d G:i:s", strtotime($databaseDate)); // this puts the date into the format "yyyy-mm-dd h24:mm:ss"
}
?>

Replace line 24 with this $formattedDate = date("Y-m-d G:i:s", $databaseDate);

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORD"); 
//select which database you want to edit
mysql_select_db("events"); 
//select the table
$result = mysql_query("select * from weekevent order by eventdate LIMIT 20");
//grab all the content
while($r=mysql_fetch_array($result));
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $event=$r["event"];
   $eventdate=$r["eventdate"];
echo "<tr><td><b>$event</b></td><td>showing on $eventdate</td>";
}
	// assumes that you have connected successfully to your database using mysql_connect and mysql_select_db
$result = mysql_query("select eventdate from events"); // runs a query in your database
if($result)
{
   $row = mysql_fetch_assoc($result); // gets the first result set row from the database for the query
   $databaseDate = $row["eventdate"]; // that is, reference the column name of your table
$formattedDate = date("Y-m-d G:i:s", $databaseDate);
}
?>

OK, I've done that but now get the following error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource inin C:\www\vhosts\eventsweb\eventsweekly.php in line 10

what do I do next?

Sounds like something is wrong with your query

$result = mysql_query("select * from weekevent order by eventdate LIMIT 20");

Make sure 'weekevent ' is the proper table name and 'eventdate' is the proper column name. They are case sensitive and must match the db exactly.

I have to head out in a few so you might also want to write the query as

$result = mysql_query("select * from weekevent order by eventdate LIMIT 20") or die(mysql_error());

That will give you an idea of what's wrong with the query. You can also try to echo the query ...

echo $result; die;

I added your code and it almost fixed the problem, but the data from the database isn't being extracted.

http://www.phpsabox1.200u.com/tvguide/index.php is the link to the page (that's from my PHP development site, just as a sandbox for educational purposes, not trying to spam the site here)

Is it giving you an error? Describe what you mean when you say data isn't being extracted.

It's just displaying "showing on" and nothing else in the link above.

$event and $eventdate variables seem to be empty. Need to find out why that is. Try this out:

$result = mysql_query("select * from weekevent order by eventdate LIMIT 20");
$count = mysql_num_rows($result);
echo $count; die;

If you get a blank screen your query is not returning any results which I'm guessing is exactly what is happening. echo $result; die; can help you try to narrow down what's wrong with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.