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
https://forums.phpfreaks.com/topic/27658-display-code-problem/
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126506
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126644
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126685
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126690
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126710
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126722
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126743
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
https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126763
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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