Solved

PHP Not Displaying Data

Posted on 2011-03-07
8
326 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 109

Expert Comment

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

Expert Comment

by:Sudaraka Wijesinghe
ID: 35062027
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 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35062073
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 35065884
<?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
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35066742
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
ID: 35067060
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
ID: 35067067
srry instead or row it should be print_r(rows);
To check whether the data is actually present in table or not
0
 
LVL 109

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

777 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