Reputation: 683
I'm very new to PHP; and am doing a few sample projects to help me learn, this project was done for fun. There's probably some idiotic and highly unintelligibly created code in here. Considering I'm a newbie, there's probably a syntax error but I've been searching for hours without any luck.
Basically I'm making a extremely simple comment/shoutbox system in which the user who posts the data is kept anonymous. I'm doing things separate at the moment; then I plan on combining them into one. I know that the submitting part is working correctly as I've checked the MySQL database via PHPMyAdmin, however I can't seem to get the data to display right. I've searched for over 2 hours for my simple syntax mistake, and have given up deciding to bring it to a reliable and friendly community.
I've removed all of the connection information to my database, as I know all of this is correct and I'd rather not expose it on a public 'forum'.
<html>
<body>
<?php
$username="";
$password="";
$database="";
mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM data";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="2" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name(No specific order)</font></th>
</tr>
<?php
$i=0;
while ($i < $num) {
$ name=mysql_result($result,$i,"");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name; ?></font></td>
</tr>
<?php
$i++;
}
?>
</body>
</html>
I have two tables, id and name. ID is a automatic increment INT, and name is set up a text. ID is also the primary of the database. The submitting system is working perfectly fine, however the displaying isn't.
When I view the page, where there should be text($name), instead I get a blank area, and no text. This is what I received when I visited the page, with only one database entry:
<html>
<body>
<table border="2" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name(No specific order)</font></th>
</tr>
<tr>
<td><font face="Arial, Helvetica, sans-serif"></font></td>
</tr>
</body>
</html>
Any help would be appreciated, thanks.
Upvotes: 0
Views: 1468
Reputation: 3141
I would recommend not using the older mysql_*() functions. You could do something like this instead (via PDO_mysql extension):
<?php
$pdo = new PDO('mysql:host=localhost;dbname=YOURDBNAME', $username, $password);
$query = "SELECT * FROM data";
$stmt = $pdo->prepare($query);
$stmt->execute(array());
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
Then later, you would loop through with something like:
foreach ($results as $res) {
echo $res['column_name'];
}
Upvotes: 3
Reputation:
Something is wrong in while loop
where you trying to display names.
Replace with below :
<?php
if($num >0) {
while ($row=mysql_fetch_assoc($result)) {
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['name']; ?></font></td>
</tr>
<?php
}
}
?>
change :
$num=mysql_numrows($result);
to
$num=mysql_num_rows($result);
Upvotes: 2
Reputation: 2286
You have the query result from MySQL but you're not looping through it properly. Try:
$result=mysql_query($query);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
// print data
}
}
Upvotes: 1