0

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.

1
  • 2
    I swear, i'm going to have to write a PHP book that doesn't suck, just so i can point to it and say i know of one that doesn't still use the mysql extension. Commented Mar 22, 2012 at 5:09

3 Answers 3

3

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'];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, I don't think mysql_close() destroys the result set. There is a separate function that does that: mysql_free_result($result)
sikander, oh snaps you're right, I took a look at the source and it doesn't free results on a mysql_close call. I'll fix my answer, thanks.
2

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);

1 Comment

Teez, thank you for trying to help me, however sadly I don't believe it was the solution to the issue I'm experiencing. Still no data has been displayed. Previously it would make a 'row' for each name, it just wouldn't show the text inside. EDIT: Trying your fix for that real quick.
1

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.