retrieve data from db and display it in table in php .. see this code whats wrong with it?
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.
php code to retrieve data from mysql database and display in html table
I agree with the other answers but before that you need to actually query.
your $sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
is just a string within the if block
.
set
$sql="";
outside.
if (isset($_POST['button'])) {
$sql="select Mid,Mname,Mnic,amount,month,bank from payments ";
}
And call
$res = mysqli_query($conn,$sql);
and
while( $row = mysqli_fetch_assoc($res))
How to retrieve data from database and display it in table
You can get rid of the "Array to string conversion" error quite easy.
In these lines, you are creating arrays:
$site_id = ['siteID'];
$equipment_type = ['equipmentTYPE'];
$lat=['latitude'];
...
$sub_company = ['subcontractorCOMPANY'];
...which you later are trying to echo. You simply can't echo arrays.
Just change the above to be strings instead:
$site_id = 'siteID';
$equipment_type = 'equipmentTYPE';
$lat = 'latitude';
...
$sub_company = 'subcontractorCOMPANY';
Note: As others already pointed out, your code is wide open to SQL Injections. You should really escape your data, before using it in any queries.
problem in php when retrieving data from database
try:
<?php
include("auth_user.inc.php"); // authrization page
$uname = $_SESSION['user']; // logged user name
$connection = mysql_connect("localhost","root"," ") or die('Could not connect to database: '.mysql_error());
mysql_select_db("userrecord",$connection) or die('Could select database: '.mysql_error());
$sql = "select * from table_user where uname ='$uname'";
$result = mysql_query($sql) or die ('Mysql error: '.mysql_error.' - '.mysql_errno());
$row = mysql_fetch_array($result);
?>
all values not displaying from SQL data table in PHP
<?php
// Attempt select query execution
$sql = "SELECT * FROM registers";
$test=array();
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
array_push($test,$row);
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
In your view part, you have add foreach
to print all values:
<?php
foreach($test as $record){?>
<tr>
<td><?php echo $test['id']; ?></td>
<td><?php echo $test['name'];?></td>
<td><?php echo $test['email'];?></td>
<td><?php echo $test['company']?></td>
</tr>
<?php } ?>
Related Topics
Pass Array Literal to Postgresql Function
Composer Cannot Download Files
How to Paginate Lines in a Foreach Loop with PHP
Check If Row Exists in the Database Before Inserting
Call Another PHP Script and Return Control to User Before the Other Script Completes
PHP Convert Xml to JSON Group When There Is One Child
Cron Command to Run Url Address Every 5 Minutes
PHP Prepend Associative Array with Literal Keys
How to Validate a Domain Name Using Regex & PHP
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
Hide Variation Info from Cart Item Title in Woocommerce 3+
Setting a Connect Timeout with Pdo
How to Bind an Arbitrary Number of Values to a Prepared Statement in MySQLi