Forum Moderators: coopster

Message Too Old, No Replies

select script not showing table data

         

nomadsoulkarma

11:36 am on Apr 17, 2010 (gmt 0)

10+ Year Member



I am wondering why the data is not showing in the little html table.
Is this code possibly deprecated?
I am testing this but hope to modify and build on it some more.
There are no errors and the html table expands the more I enter into the MySql table. But, nothing in the table.
The connection works and the cnd and form inserts also work.
Any insight?
 <?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="10%"><? echo $rows['id']; ?></td>
<td width="10%"><? echo $rows['username']; ?></td>
<td width="30%"><? echo $rows[password']; ?></td>

</tr>
</table>

<?php
}
// close while loop

// close connection
mysql_close();

?>

Tommybs

1:00 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



Hi,

You're missing a single quote ( ' ) around password. Though I don't know if this is a typo here. As a side note, why are you creating a table each time in the while loop? Is that intentional or do you just mean to create new rows for each entry?

Just as a test, after your query why don't you run something such as :

if($result){
echo "<p>There are ". mysql_num_rows($result)." rows in the database</p>";
}

nomadsoulkarma

12:27 am on Apr 18, 2010 (gmt 0)

10+ Year Member



Thanks,
It works now, I didn't see the missing quote.
I just wanted the results to have a little structure with the table.

Matthew1980

6:35 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there nomadsoulkarma,

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="10%"><?php echo $rows['id']; ?></td>
<td width="10%"><?php echo $rows['username']; ?></td>
<td width="30%"><?php echo $rows['password']; ?></td>

</tr>
</table>

<?php
}
// close while loop


Just a pointer really, try not to use the short tags, as this has the potential to cause errors when/if you change servers, as the short style tags are enabled via the php.ini file, and 98% of servers/hosts have this option off so that full tags <?php are only used.

Also, $sql="SELECT * FROM $tbl_name"; can be done like this: $sql = "SELECT * FROM '".$tbl_name."'"; Basically just adding the var into the string, IMHO this is making the code easier to read, again, only an opinion ;-)

And lastly :)

mysql_connect($host, $username, $password)

there is no need for the quotes around the vars, bad habit to get into IMO.

As you are developing this project, a handy tool to have on would be error_reporting(E_ALL); as this would help you in the long run, this will flag up any errors that would not normally get flagged up. :)

Other than that, have fun with the rest of the project!

Cheers,
MRb