Jump to content

Recommended Posts

Hi i'm trying to display a table thru mysql. Just learnt to do so and i haven't got a clue what is wrong with the code. In line 19 the error is that 'mysql_numrows() expects parameter 1 to be resource'. Can i get some help as to what to correct it with?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$host="localhost";
$username="root";
$password="";
$database="energy";


mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename";
$result=mysql_query($query);


$num=mysql_numrows($result);


mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif">Value1</font></td>
<td><font face="Arial, Helvetica, sans-serif">Value2</font></td>
<td><font face="Arial, Helvetica, sans-serif">Value3</font></td>
<td><font face="Arial, Helvetica, sans-serif">Value4</font></td>
<td><font face="Arial, Helvetica, sans-serif">Value5</font></td>
</tr>


<?php
$i=0;
while ($i < $num) {


$f1=mysql_result($result,$i,"field1");
$f2=mysql_result($result,$i,"field2");
$f3=mysql_result($result,$i,"field3");
$f4=mysql_result($result,$i,"field4");
$f5=mysql_result($result,$i,"field5");
?>


<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
</tr>
</table>
<?php
$i++;
}
?>
</body>
</html>

Edited by cleeclee
Link to comment
https://forums.phpfreaks.com/topic/270768-error-while-displaying-mysql-table/
Share on other sites

Hi i've decided to keep it simple. Here's what i did. Without the while loop i'm only able to display 1 line of data. I'd like to display the other lines too, so i used the loop and i have the same error msg as in my original post. "Warning: mysql_fetch_array() expects parameter 1 to be resource"

Edited by cleeclee
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.