$db = mysql_connect("localhost", "root", "");
$er = mysql_select_db("ram");
$query = "insert into names values('$name','$add1','$add2','$mail')";
$result = mysql_query($query);
print "<p> Person's Information Inserted </p>";
$result = mysql_query("SELECT * FROM names");
?>
<table border="2">
<tr>
<th>Name</th>
<th>Address Line 1</th>
<th>Address Line 2 </th>
<th>E-mail Id </th>
</tr>
<?
while ($array = mysql_fetch_row($result));
{
print "<tr> <td>";
echo $array[0];
print "</td> <td>";
echo $array[1];
print "</td> <td>";
echo $array[2];
print "</td> <td>";
echo $array[3];
print "</td> </tr>";
}
?>

- 20,697
- 12
- 65
- 90

- 121
- 1
- 1
- 5
-
2Could you possibly elaborate more? Posting a chunk of code and no question body generally isn't received well on here. – alex Dec 02 '10 at 03:26
-
8Another game of "GUESS! THE! ERROR!"... – Ignacio Vazquez-Abrams Dec 02 '10 at 03:28
-
@Ignacio Vazquez-Abrams Who is game enough to be our first contestant? Winner gets 10 rep and maybe an accepted answer! – alex Dec 02 '10 at 03:29
-
You are not connecting to your database and/or your tables don't exist ;) – Jason McCreary Dec 02 '10 at 03:32
-
there is nothing error in your code what error message it shows ? – Meena Dec 02 '10 at 06:48
-
@Amyth Not entirely relevant seeing that he could have short tagging enabled. – Evan Stoddard Sep 07 '13 at 17:10
-
1Please don't use `mysql_*` as it has been depreciated. Please use `mysqli_*` or `PDO` instead. – Evan Stoddard Sep 07 '13 at 17:11
5 Answers
Try this:
<?php
# Init the MySQL Connection
if( !( $db = mysql_connect( 'localhost' , 'root' , '' ) ) )
die( 'Failed to connect to MySQL Database Server - #'.mysql_errno().': '.mysql_error();
if( !mysql_select_db( 'ram' ) )
die( 'Connected to Server, but Failed to Connect to Database - #'.mysql_errno().': '.mysql_error();
# Prepare the INSERT Query
$insertTPL = 'INSERT INTO `name` VALUES( "%s" , "%s" , "%s" , "%s" )';
$insertSQL = sprintf( $insertTPL ,
mysql_real_escape_string( $name ) ,
mysql_real_escape_string( $add1 ) ,
mysql_real_escape_string( $add2 ) ,
mysql_real_escape_string( $mail ) );
# Execute the INSERT Query
if( !( $insertRes = mysql_query( $insertSQL ) ) ){
echo '<p>Insert of Row into Database Failed - #'.mysql_errno().': '.mysql_error().'</p>';
}else{
echo '<p>Person\'s Information Inserted</p>'
}
# Prepare the SELECT Query
$selectSQL = 'SELECT * FROM `names`';
# Execute the SELECT Query
if( !( $selectRes = mysql_query( $selectSQL ) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
}else{
?>
<table border="2">
<thead>
<tr>
<th>Name</th>
<th>Address Line 1</th>
<th>Address Line 2</th>
<th>Email Id</th>
</tr>
</thead>
<tbody>
<?php
if( mysql_num_rows( $selectRes )==0 ){
echo '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
while( $row = mysql_fetch_assoc( $selectRes ) ){
echo "<tr><td>{$row['name']}</td><td>{$row['addr1']}</td><td>{$row['addr2']}</td><td>{$row['mail']}</td></tr>\n";
}
}
?>
</tbody>
</table>
<?php
}
?>
Notes, Cautions and Caveats
Your initial solution did not show any obvious santisation of the values before passing them into the Database. This is how SQL Injection attacks (or even un-intentional errors being passed through SQL) occur. Don't do it!
Your database does not seem to have a Primary Key. Whilst these are not, technically, necessary in all usage, they are a good practice, and make for a much more reliable way of referring to a specific row in a table, whether for adding related tables, or for making changes within that table.
You need to check every action, at every stage, for errors. Most PHP functions are nice enough to have a response they will return under an error condition. It is your job to check for those conditions as you go - never assume that PHP will do what you expect, how you expect, and in the order you expect. This is how accident happen...
My provided code above contains alot of points where, if an error has occured, a message will be returned. Try it, see if any error messages are reported, look at the Error Message, and, if applicable, the Error Code returned and do some research.
Good luck.

- 10,357
- 2
- 26
- 41
-
1
-
Personally, I have never used PDO. Mainly as I have learnt as I have gone along, and as such worked with alot of Open Source packages, none of which appear to be using it. – Luke Stevenson Dec 02 '10 at 07:07
Here is the solution total html with php and database connections
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "database-username";
$password = "database-password";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Employee_id</th>
<th>Employee_Name</th>
<th>Employee_dob</th>
<th>Employee_Adress</th>
<th>Employee_dept</th>
<td>Employee_salary</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row\['employee_id'\]}</td>
<td>{$row\['employee_name'\]}</td>
<td>{$row\['employee_dob'\]}</td>
<td>{$row\['employee_addr'\]}</td>
<td>{$row\['employee_dept'\]}</td>
<td>{$row\['employee_sal'\]}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>

- 187
- 13
This is a very simple code I use and you manipulate it to change the colour and size of the table as you see fit.
First connect to the database:
<?php
$connect=mysql_connect('localhost', 'root', 'password');
mysql_select_db("name");
//here u select the data you want to retrieve from the db
$query="select * from tablename";
$result= mysql_query($query);
//here you check to see if any data has been found and you define the width of the table
If($result){
echo "<table width ='340' align='left'>
<tr color ='#5D9951>";
$i=0;
If(mysql_num_rows($result)>0)
{
//here you fetch the data from the database and print it in the respective columns
while($i<mysql_num_fields($result))
{
echo "<th>".mysql_field_name($result, $i)."</th>";
$i++;
}
echo "</tr>";
$color=1;
while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
{
If ($color==1){
echo "<tr color='#'#cccccc'>";
foreach ($rows as $data){
echo "<td align='center'>".$data. "</td>";
}
$color=2;
}
$color=1;
}
} else {
echo"no results found";
echo "</table>";
} else {
echo "error running query:".MYSQL_error();
}
?>
It's a very elementary piece of code but it helps if you are not used to using functions.
-
1would be much better, if it's a function and the table name, db etc. are parameters and the solution uses MySQL PDO. well done bro! – Fayyaz Ali May 18 '17 at 19:20
In your while statement just replace mysql_fetch_row
with mysql_fetch_array
or mysql_fetch_assoc
... whichever works...

- 1,732
- 3
- 23
- 27

- 22
- 2
- 10
<html>
<head>
<meta charset="UTF-8">
<title>LoginDB</title>
</head>
<body>
<?php
$con= mysqli_connect("localhost", "root", "", "detail");
<!-- detail is the database in MySqli Database -->
if(!$con)
{
die('not connected');
}
$con= mysqli_query($con, "select * from signup");
<!-- signup is the table in the detail_Database -->
?>
<div>
<td>Login Page Database</td>
<table border="1">
<th> First Name</th>
<th>Last Name</th>
<th>UserName</th>
<th>Password</th>
<th>Gender</th>
<th>D.O.B.</th>
<th>Phone Number</th>
<th>Address</th>
</tr>
<?php
while($row= mysqli_fetch_array($con))
<!-- Fetch each row from signup Table -->
{
?>
<tr>
<td><?php echo $row['FirstName']; ?></td>
<td><?php echo $row['LastName']; ?></td>
<td><?php echo $row['Username']; ?></td>
<td><?php echo $row['Password'] ;?></td>
<td><?php echo $row['Gender'] ;?></td>
<td><?php echo $row['DOB'] ;?></td>
<td><?php echo $row['PhoneNumber'] ;?></td>
<td><?php echo $row['Address'] ;?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
-
1
-
This answer doesn't do anything and it has major syntax errors. – Funk Forty Niner Feb 15 '20 at 19:00