简体   繁体   中英

PHP using mysql_fetch_array to fill a table

Ok Before you tell me to use mysqli I am using the depreciated methods on purpose for a webapp lesson. I am not a student, this is not homework, it is to help me teach an understanding for web application security.

I cannot figure out why this wont work. Basically, All I want to do is create a page that takes the data from the mysql database and places it into a table as shown. At this point I am open to anything.

Thank you in advance.

  <html> <head><title>Untitled</title></head> <body> <h1>Weblog Example</h1> <dl> <?php mysql_connect("localhost","root",""); mysql_select_db("blog1"); $query ="SELECT entrytitle, entrytext,"; $query.=" DATE_FORMAT(entrydate, '%M %d, %Y') AS entrydate"; $query.=" FROM weblog ORDER BY entrydate DESC LIMIT 10"; $result=mysql_query($query); ?> <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="15%" align="center" bgcolor="#E6E6E6"><strong>Title</strong></td> <td width="75%" align="center" bgcolor="#E6E6E6"><strong>Entry</strong></td> <td width="15" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytitle']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytext']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['entrydate']; ?></td> </tr> <?php } ?> </dl> </body> </html> 

Ok- made some minor edits- it now gives me 3 rows in the table but doesn't populate the data...

You're using the wrong variable in your while loop, you're also not referencing the correct date column from your query result.

This should give you what you're looking for:

<?php
  while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytitle']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytext']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrydate']; ?></td>
</tr>

<?php
}
?>

Do a var_dump on $entrytitle and $entrytext and you'll understand you error. Data is temporary stored into $rows when you do a mysql_fetch_array.

<?php
  while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytitle"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
</tr>

<?php
}
?>

Both your $query.= should read as $query .= that alone will be an issue and will not concatenate properly because of the missing space before the dots.

You're also missing a </table> tag.

Plus, make sure that short tags are set/on, otherwise do <?php echo $rows...
instead of <? echo $rows... <? echo $rows... .

You should also check for possible query errors using:

$result = mysql_query($query) or die(mysql_error());

and using error reporting .

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.



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