Almost everything is working fine except on the first column, the data is preceded by "1".
Here's my output :
The first column,ie,strength is preceded with 1. The data in my database is just a single digit number.
<?php
include 'index.php';
$sql = "SELECT t.strength, t.timeslot, t.cust_phno , c.fname, c.lname FROM tables t,customer c WHERE t.cust_phno = c.cust_phno";
$result = mysqli_query($con,$sql);
?>
<div id="view">
<table style="width:90%">
<thead>
<tr>
<th> Table Strength </th>
<th> Time Slot </th>
<th> Customer Phone Number</th>
<th> Customer Name </th>
</tr>
</thead>
<tbody>
<?php
while( $array = mysqli_fetch_assoc($result)) {
echo
print "<tr> <td>";
echo $array["strength"];
print "</td> <td>";
echo $array["timeslot"];
print "</td> <td>";
echo $array["cust_phno"];
print "</td> <td>";
echo $array["fname"];
print " ";
echo $array["lname"];
print "</td> </tr>";
}
?>
</tbody>
</table>
The 1 is the result of echoing the return from print... the following lines need to be slightly different..
while( $array = mysqli_fetch_assoc($result)) {
echo
print "<tr> <td>";
You don't need the echo...
while( $array = mysqli_fetch_assoc($result)) {
print "<tr> <td>";
<?php
while( $array = mysqli_fetch_assoc($result)) {
print "<tr> <td>";
echo $array["strength"];
print "</td> <td>";
echo $array["timeslot"];
print "</td> <td>";
echo $array["cust_phno"];
print "</td> <td>";
echo $array["fname"];
print " ";
echo $array["lname"];
print "</td> </tr>";
}
?>
Remove the echo from your code.
It is the simple and easy process, that I will show you as an example and source code.
At first connect database using mysqli in PHP. Using this code : $con=mysqli_connect(‘localhost’,’root’,’password’,’Database Name’);
Now select all data using mysqli from MySQL table in PHP. If you using MySQL query for select record or fetch the record in PHP project, so it’s simple use for you. Fetch result in your table page using while loop.
<?php
include "db_connect.php";
?>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
$mysqli_qry=mysqli_query($con,"SELECT * from `Table Name` ORDER BY `id` DESC");
while($row=mysqli_fetch_array($mysqli_qry))
{
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row['1']; ?></td>
<td><?php echo $row['3']; ?></td>
<td><?php echo $row['2']; ?></td>
<td><?php echo $row['4']; ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
Related
I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>
I want to make a table based on a result from an SQL query in PHP. I know that if my query returned 2 results I could use
echo '<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<tr>
<td>$row[0]['username']</td>
<td>$row[0]['answer']</td>
</tr>
<tr>
<td>$row[1]['username']</td>
<td>$row[1]['answer']</td>
</tr>
But how do I go about this if I don't know how many results will be returned
EDIT:
I have used a loop as advised but the results appear underneath the table instead of inside. Only the table headers are inside. Any ideas?
echo '<table class="collabtable">
<tr>
<th>User</th>
<th>Answer</th>
</tr>';
foreach($rows as $row){
if (isset($row['collabans'])){
echo '<tr>
<td>'.$row['username'].'</td>
<td>'.$row['collabans'].'</td>
</tr>';
}
echo '</table>';
}
}
You can use loop. See below.
<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<?php foreach ($rows as $row) {
if (isset($row['collabans'])){ ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['answer']; ?></td>
</tr>
<?php }
} ?>
</table>
You need to loop through all results of your query result as below:
if ($result->num_rows() > 0) {
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
<tr>
<td>$row['username']</td>
<td>$row['answer']</td>
</tr>
}
}
Hope it helps you.
I have 4 columns in sqltable.
How can I update solution cell value to SQL table. Need to update value based on checkbox value as userid.
<?php
$result = mysql_query("SELECT * FROM `tbl_query`);
?>
<table id="t01" >
<thead>
<tr>
<th>User Id</th>
<th>Query</th>
<th>Solution</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
?>
<tr>
<td><?php echo $row['userid']; ?></td>
<td><?php eco $row['userquery']; ?></td>
<td contenteditable='true'><?php echo $row['solution']; ?></td>
<td><?php $row['querydate']; ?></td>
<td style='display:none;'><input type='checkbox' id='<?php echo $row['userid'];?>' name='check_list[]' value='<?php echo $row['userid']; ?>' checked /></td>
</tr>
<?php
}
?>
</tbody>
</table>
first of all, you should not use mysqlanymore since its deprecated. Use mysqli or pdo instead.
second, you are using double-quote at the beginning and single-quote at the end of your query.
and third: you forgot the singlequot in the [], thats not how you access an item from the array, you need $row['userid']
I'm writing some php that renders a table from a database, this should be simple, but for some reason, it is rendering extra cells and all cells are empty. Here is my code:
<?php
$db= new PDO("mysql:host=localhost;dbname=mydb", "user", "password");
$query= $db->query("SELECT yarnId, yarnName, longYarnDescription, sale_price, cost, contents, onSale, yarnImage, activeFlag FROM yarn");
$result= $query->fetchAll();
?>
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php for($r=0; $r<count($result); $r++){?>
<tr>
<?php for($c=0; $c<count($result[0]); $c++){?>
<td><?php echo $result[r][c];?></td>
<?php }?>
<td><button name=edit>edit</button></td>
</tr>
<?php }?>
</table>
If anyone can tell me why it's empty and why there are extra cells, it would be greatly appreciated.
The following code uses while()loop instead of for()
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php
while($row = $query->fetch()) {
echo "<tr>";
for ($x=0;$x<= 8; $x++) {
echo "<td>" . $row[$x] . "</td>";
}
echo "<td><button name=\"edit\">edit</button></td></tr>\n";
}
?>
</table>
What I am trying to do is get it so there is a different hyper link address for each row echoed. Code is below:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Product Name</th> <th>Product Description</th> <th>Product Price</th> <th>Product Image</th> <th>View Product Details</th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['Product_Name'] . '</td>';
echo '<td>' . $row['Product_Description'] . '</td>';
echo '<td>' . $row['Product_Price'] . '</td>';
echo '<td>Picture Enlarge</td>';
echo '<td>View Details</td>';
echo "</tr>";
}
echo "</table>";
You can put php variables inline in your echo'ed anchors just as you are doing with the other variables.
Assuming that you're using the id field in your database you can do this:
echo '<td>View Details</td>';
If you echo a php variable (e.g. $row['id']) then it will echo out to HTML (not necessarily text). So as this one is contained in an anchor tag (in the HTML) it echos to the anchor tag and builds the id part. :)
<table border='1' cellpadding='10'>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
<th>Product Image</th>
<th>View Product Details</th>
</tr>
while($row = mysql_fetch_array( $result )) {
<tr>
<td> <?php echo $row['Product_Name']; ?></td>
<td><?php echo $row['Product_Description']; ?></td>
<td><?php $row['Product_Price']; ?></td>
<td>Picture Enlarge</td>
<td>View Details</td>
</tr>
}
</table>
I would probably go for something like that. This assumes that your table returns a unique id, from something like an auto incremented column.
This doesn't include any escaping so could be vulnerable to exploitation. I would also look into templating. It may help you with your presentation, makes it a little easier to read the mixed html and php.
"<td align=\"center\" valign=\"top\"><<") . "</td>\n"