Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Not Displaying Data

Posted on 2011-03-07
8
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 111

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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