Jump to content

display code problem


182x

Recommended Posts

Hey guys, the following code is to help me display a list of data from a database but for some reason the data is not being displayed. Any suggestions? Thanks

[code]
$sql = "SELECT * FROM acc";

$query = mysql_query($sql);

  while ( $row = mysql_fetch_array($query))
  {

            $data = "".$row["data"]."";

            echo "$data <br><br>";
  }
[/code]
Link to comment
Share on other sites

Your query could be failing for all we know, you never check it before trying to use it. Try

[code]
<?php
$sql = "SELECT * FROM acc";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
      echo $row['data']."<br><br>";
    }
  } else {
    echo "no results found";
  }
} else {
  echo "query failed || ".mysql_error();
}
?>
[/code]
Link to comment
Share on other sites

Hi.

Try this little change of your code:

[code]
$sql = "SELECT * FROM acc";

$query = mysql_query($sql);

  while ( $row = mysql_fetch_array($query))
  {

            $data = ".$row['data'].";

            echo $data." <br><br>";
  }
[/code]

It should work now.

Link to comment
Share on other sites

Sorry. Here is the correct code:

[code]
$sql = "SELECT * FROM acc";

$query = mysql_query($sql);

  while ( $row = mysql_fetch_array($query))
  {

            $data = $row['data'];

            echo $data." <br><br>";
  }
[/code]
Link to comment
Share on other sites

Thanks again for tour reply, I am still having the problem that the data isn't actually being displayed although there is space being left for it. I have checked the database and there should be 34 records being displayed.

Any more ideas? and thanks again.
Link to comment
Share on other sites

Can you please copy the table creation query you use for creating the acc table, here?

What I mean is, you have somewhere in your script a query which starts with "CREATE TABLE acc ("
Now, copy this query here. It starts with "CREATE TABLE acc (" and ending with ";
Link to comment
Share on other sites

Here is the sql: (the table currently has over 30 records in it)

[code]
DROP TABLE IF EXISTS acc;
CREATE TABLE acc (
  traId int(19) NOT NULL auto_increment,
  tri varchar(29) NOT NULL default '',
  trao float(19) NOT NULL default '0',
  rId int(10) NOT NULL default '0',
  PRIMARY KEY  (traId)
);
[/code]
Link to comment
Share on other sites

Well, there is a problem!  ;D

You cannot do this:

$data = $row['[color=red]data[/color]'];

because, there is no [color=blue]data[/color] field in your database.

You have: tri, trao and rId fields. Which row from this 3 holds the data you want to display?
Link to comment
Share on other sites

Then you need to do this:

[code]
$sql = "SELECT * FROM acc";

$query = mysql_query($sql);

  while ( $row = mysql_fetch_array($query))
  {

            $data = $row['tri'];
            $data2 = $row['trao'];
            $data3 = $row['rId'];

            echo $data." <br><br>";
            echo $data2." <br><br>";
            echo $data3." <br><br>";
  }
[/code]
Link to comment
Share on other sites

That woks now thanks so much for your help :)

I have one more question for formatting purposes if I were to put data like that into a table then records get inserted or removed from the database how would the table know ot resize?

and thanks again!!!
Link to comment
Share on other sites

When you put data into table, you use INSERT for inserting new data, which will resize the table. If you use UPDATE for inserting data, it will update the fields you set in the query and it will not resize the table. When updating the table, all the rows data will be deleted and a new data will be inserted.
Link to comment
Share on other sites

Yes.

When you are creating the HTML table, you connect to the database and get out the results as you did in your script.

Before the while loop, you create your table's header, the upper rows which hold the name's.

<table border="1" width="658" height="246">
   <tr>
     <td width="99" height="56">My data name1</td>
     <td width="79" height="56">My data name2</td>
     <td width="74" height="56">My data name3</td>
   </tr>

Then you use the while loop, and in while loop you write the table codes:

while($row=mysql_fetch_array($result) {
    echo "<tr><td>".$row['tri']."</td><td>".$row['trao']."</td><td>".$row['rId']."</td></tr>";
}
echo "</table>";

After the while loop, you write the </table> which is the ending tag for table.

When you have 4 data in your table, 4 row will be written on site. When you have 7 data in your table, 7 rows will be written on your site. So, the HTML table will allways expand or shrink, dependent on how many results you have. And he is doing that dynamically.  :)


Link to comment
Share on other sites

I don't know if it will work on older php version's, but I think that the most web host companies have installed php version > 4.1.0 and this script will work on versions > 4.1.0 .

About php books: I don't know any good php book, because I learn php thrue internet, but somebody will maybe help you with that question.
Link to comment
Share on other sites

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.