Forum Moderators: coopster
I am using w3schools.com for learning purpose and as per the tutorials there I coded as follows
$result = mysql_query("SELECT * FROM table_name") ;
echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
The page loads properly but no table is displayed... while everything within HTML tags is displayed properly
as if PHP script is completely bypassed
Whats wrong here? Have spent quite some time on it now but I am not able to figure out any error
Thanks
$result = mysql_query("SELECT * FROM table_name") ;
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
} //end close off the if statement
script is enclosed in <?php ?
This is the full script again
<html>
<head>
<title> View contacts </title>
<link rel="stylesheet" type="text/css" href="phbook.css">
</head>
<body>
<?php include("backlinks.php"); ?>
<form action="result.php" method="post">
<b> Search for: </b>
<input type="text" size="40" name="searchfor"/>
<input type="submit" value="Find"/>
<input type="hidden" name="user" value="<?php echo $_POST["user"]; ?>"/>
</form>
<?php
// Establishing connection or displaying error msg in case of a failure
$con=mysql_connect("localhost","username","");
if(!$con)
{
die('Could not Connect:' . mysql_error());
}
// Selecting the database
mysql_select_db("myapp",$con);
$u = $_POST["user"];
if($u=="user1")
{
$result = mysql_query("SELECT * FROM user1");
$result2 = mysql_query($result);
if(!$result)
{
die('Error:' . mysql_error());
}
if(mysql_num_rows == 0)
{
echo "No records found";
}
else
{
echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";
while($row = mysql_fetch_array($result2,MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
//....... similar script for other users 2,3, and 4 using else if
//closing the connection
mysql_close($con);
?>
</body>
</html>
------------------------------------------------------------
Thanks again for your help ..
Actually, initially it was showing an error on this line in the above script
echo "<td>" . $row['Name'] . "</td>";
but there was also another error which was
echo "<table border="1"... >";
which I changed to echo "<table border='1'... >";
After doing this it stopped showing any error .. not even the one which it was showing earlier.. so neglected it.
Is there anything wrong in this particular line
echo "<td>" . $row['Name'] . "</td>";
PS> The fieldname in the table is 'Name' only..
Have you tried checking your error logs?
I see you have adequate error checks, but even so, put this at the top of your script:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Remove it after going live.
also I don't see a header, probably not important, but put this somewhere before output, top of script is fine
header("content-type: text/html\n\n");
So your table name is user1, right?
At various points do a var dump. Top of script,
var_dump($_POST);
or
var_dump($GLOBALS);
Something else is going on here, these should work.
//check to see if they clicked the button
if(isset($_POST['user'])){
//then do your check
if($u == "user1"){
//do your code
}//if u = user
}//if isset
Also, the var_dump is a good debugging tool. Run it on all of your variables and see if they are the same type and value you think they should be. One last thing for readability. You can write strings like this
$str = "<th>{$row['Name']}</th>";
I just think that is easier to read, just a personal preference.