Solved

PHP Not Displaying Data

Posted on 2011-03-07
8
323 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:wantabe2
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Line 13 - how do you know whether that query worked?
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
Comment Utility
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Add error_reporting(E_ALL) to the top of the script.

Line 13 change to
$result=mysql_query($sql) or die( mysql_error() );

Line 30 change to
while($rows = mysql_fetch_assoc($result)){

Lines 33-35 change to
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>

Remove extraneous lines 47-49
0
 
LVL 17

Expert Comment

by:shinuq
Comment Utility
<?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
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 3

Expert Comment

by:pius_babbun
Comment Utility
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
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
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

0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
srry instead or row it should be print_r(rows);
To check whether the data is actually present in table or not
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points; it's a really good question.  Another victory for data visualization! ~Ray
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now