Jump to content

help with displaying from database


Ameslee

Recommended Posts

hope someone can help me.  Im trying to display data from database on to my website.  It displays the first record only.  Why its meant to show at least 7.  why isnt it working?

here is the code:
[code]  <?php
$hostname = localhost;
$username = ;
$password = ;

$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("", $conn);


$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
$mysql_result=mysql_query($query,$conn);
$row=mysql_fetch_row($mysql_result);

echo "<table border=0 cellpadding=0 cellspacing=0>";
$i=1;
while (($row!="") && ($i<=7))
{
$date=$row[2];
$day=substr($date,8,2);
$month=substr($date,5,2);
$year=substr($date,0,4);
$date2=$day." - ".$month." - ".$year;
$event = $row[1];


echo("<tr><td width=35%>$event</td><td width=25%>$date2</td></tr>
                    ");
$row=mysql_fetch_row($mysql_result);
$i++;
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/28602-help-with-displaying-from-database/
Share on other sites

ok i have changed the $row=mysql_fetch_array($mysql_result); instead of $row=mysql_fetch_row($mysql_result);

it is still doing the same thing, only displays one record.  Here is the code again with the change and also the table, maybe it has something to do with where it is positioned.  Its a table within another table.

[code]<table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td>
                              <?php
$hostname = localhost;
$username = greenac_Admin;
$password = greenac;

$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("greenac_VORNExpo", $conn);


$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
$mysql_result=mysql_query($query,$conn);
$row=mysql_fetch_array($mysql_result);

echo "<table border=0 cellpadding=0 cellspacing=0>";
$i=1;
while (($row!="") && ($i<=7))
{
$date=$row[2];
$day=substr($date,8,2);
$month=substr($date,5,2);
$year=substr($date,0,4);
$date2=$day." - ".$month." - ".$year;
$event = $row[1];


echo("<tr><td width=35%>$event</td><td width=25%>$date2</td></tr>
                    ");
$row=mysql_fetch_array($mysql_result);
$i++;
}

?>
                            </td>
                          </tr>
                          <tr>
                            <td><font size="2" face="Georgia, Times New Roman, Times, serif"><a href="display_events.php">Click
                              here to view all events and details!</a> </font></td>
                          </tr>
                        </table>[/code]
Pls try like this

1. i have removed $row=mysql_fetch_array($mysql_result) which is after mysql_query
2. i have replaced $row!="" with $row=mysql_fetch_array($mysql_result) in while
3 removed $row=mysql_fetch_array($mysql_result) which comes after echo

I am not sure this will work. But give a try . And i hope your database has more than one record and the query $query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC"; results in more than one record.

<table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td>
                              <?php
$hostname = localhost;
$username = greenac_Admin;
$password = greenac;

$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("greenac_VORNExpo", $conn);


$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
$mysql_result=mysql_query($query,$conn);


echo "<table border=0 cellpadding=0 cellspacing=0>";
$i=1;
while (($row=mysql_fetch_array($mysql_result)) && ($i<=7))
{
$date=$row[2];
$day=substr($date,8,2);
$month=substr($date,5,2);
$year=substr($date,0,4);
$date2=$day." - ".$month." - ".$year;
$event = $row[1];


echo("<tr><td width=35%>$event</td><td width=25%>$date2</td></tr>
                    ");
$i++;
}

?>
                            </td>
                          </tr>
                          <tr>
                            <td><font size="2" face="Georgia, Times New Roman, Times, serif"><a href="display_events.php">Click
                              here to view all events and details!</a> </font></td>
                          </tr>
                        </table>
i have at least 5 other records in there already, i check phpmyadmin and there are 5.

[quote]Are you sure it should be WHERE Date >now() and not <?[/quote] if i was to do this it would make it less than the present date to be displayed.  Now that i have read that, i am soo stupid.  Ok i am sure i have it now.  thanks u guys!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.