Link to home
Create AccountLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP Not Displaying Data

Can someone else eyeball this code to see if you can tell me why it is not displaying the mysql data in a browser?
<?php
$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="password"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // 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");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['lastname']; ?></td>
<td><? echo $rows['email']; ?></td>

// link to update.php and send value of id 
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Line 13 - how do you know whether that query worked?
Code works fine for me.
I assume you are not seeing any errors,  and make sure you have data on the table ;)

On another note, you have a // comment in the in the HTML
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Shinesh Premrajan
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL^E_NOTICE);

?>

Place this code in the top of the script, this will show up the actual error in the script, later you can disable this code.

Hope this helps
Avatar of pius_babbun
pius_babbun

This code works perfectly fine for me .

Please add this code at the top of you page and check
ini_set('error_reporting',E_ALL);
ini_set('display_startup_errors','On');
ini_set('display_errors','On');

Open in new window

123.JPG
Hi,
please paste the o/p of the code below


<?php
$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="password"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // Table name 

ini_set('error_reporting',E_ALL);
ini_set('display_startup_errors','On');
ini_set('display_errors','On');

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

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_assoc($result)){
echo "<pre>";print_r($row);exit;
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>

// link to update.php and send value of id 
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

Open in new window

srry instead or row it should be print_r(rows);
To check whether the data is actually present in table or not
Thanks for the points; it's a really good question.  Another victory for data visualization! ~Ray