display results of mysqli_multi_query in table - php

I am trying to echo the results of two queries into a table. It seems to be splitting the results into separate rows rather than beside each other. The values for spaceid and venueid match in the db.
Here is my code:
<?php
$con=mysqli_connect("host","username","pword","mydb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT venue.venuename FROM venue INNER JOIN space ON space.spaceID=venue.venueID;";
$sql .= "SELECT space.room, space.costoftables FROM space INNER JOIN venue ON space.spaceID=venue.venueID;";
?>
<table class="tablesorter" width="600px" border="1" cellpadding="1" cellspacing="1" id="myTable">
<thead>
<tr>
<th>Venue Name</th> <th>Room</th> <th>Cost</th>
</tr></thead><tbody>
<?php
// Execute multi query
if (mysqli_multi_query($con,$sql)){
do{
// Store first result set
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['venuename']."</td>";
echo "<td>".$row['room']."</td>";
echo "<td>".$row['costoftables']."</td>";
}
?>
</tr>
<?php
}
}
while (mysqli_next_result($con));
}
mysqli_close($con);
?>
</tbody>
</table>
Here is the result:
Results

Related

PHP MySql Result as Table in html

Im trying to list the result from MySQl results as a Table in my HTML.
My PHP file:
$stmt = $this->get('database_connection')
->executeQuery(
'SELECT * FROM members'
);
while (false !== ($objMember = $stmt->fetch(\PDO::FETCH_OBJ)))
$template->listTable= implode(', ', $objMember['firstname']);
return $template->getResponse();
And in HTML:
block('content'); ?>
<p><?= $this->listTable ?></p>
<?php $this->endblock(); ?>
But I dont get any reults in the html file. What is the correct way to list all results from SQL to a Table?
I don't know if it is the right way or not but it works as this :
I create some html table
<table class="table">
<thead class=" text-primary">
<th>table header</th>
<th>table header 2</th>
</thead>
<tbody>
In here my PHP begins
<?php
$query = mysqli_query($mysql_connection, "SELECT * FROM tbale WHERE conditions");
while ($array = mysqli_fetch_array($query)) {
$k_exmaple_1 = $array['column1'];
$k_exmaple_2 = $array['column2'];
echo "
<tr>
<td>$k_exmaple_1</td>
<td>$k_exmaple_2</td>
</tr>
";
}
?>
PHP Ends
I connected the table in MySQL, and selected my rows then print it with echo in html format.
</tbody>
</table>
Table ends

Make a flate table in php

There is a problem with table, why the field's row repeat in all register?
Here is the code;
<?php
----
-------
-------
$result=mysqli_query($dbcon,$consult);
while($row=mysqli_fetch_row($result)){
echo "<table><tr><th>NIF</th><th>Name</th><th>lastname</th> <th>Edad</th></tr>";
echo "<tr><td>";
echo $row[0]."</td><td>";
echo $row[1]."</td><td>";
echo $row[2]."</td><td>";
echo $row[3]."</td></tr>";
}
echo "</table>";
?>
You need to put the field rows out side of the loop
<?php
// DB Query
$result=mysqli_query($dbcon,$consult); $i=0;
// Starting the table
print ("
<table>
<tr>
<th>NIF</th>
<th>Name</th>
<th>lastname</th>
<th>Edad</th>
</tr>");
// Result loop
while($row=mysqli_fetch_row($result)){
// Table content
print ("
<tr>
<td>".$row[0]."</td>
<td>".$row[1]."</td>
<td>".$row[2]."</td>
<td>".$row[3]."</td>
</tr>");
$i++;
}
// No results
if ($i==0){
print ("
<tr>
<td colspan='4'>No results</td>
</tr>");
}
// Closing table
print ("</table>");
?>

Why is my PHP inserting the correct amount of rows but in the rows there are no data?

I am trying to add a script that makes a request to my MySQL database and displays the data back in an html table. At the minute the request is made and the amount of rows in the table corresponds to the amount of rows in the mysql data however no physical data is there.
The code that I am trying to use is:
<div class="container" style="margin-top:2em;margin-bottom:33em;">
<table border="1" class="table table-striped" style="margin-top: 2em;">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>passcode</th>
</tr>
</thead>
<tbody>
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$servername="localhost";
$username="wvptszyl_sc";
$password="wvptszyl_sc";
//!##$%^&*(
$db="wvptszyl_sc";
$conn=mysqli_connect($servername,$username,$password,$db);
//mysql_select_db($db);
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno($conn) . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error($conn) . PHP_EOL;
exit;
}
#session_start();
$result = $conn->prepare("SELECT id, username, passcode FROM admin");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><label><?php echo $row['id']; ?></label></td>
<td><label><?php echo $row['username']; ?></label></td>
<td><label><?php echo $row['passcode']; ?></label></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
At the minute there are 2 rows in the table and 2 rows are inserted however what I was hoping for was the data to be displayed in the rows.
You have misunderstood the for loop. It's used to repeat specific actions until the specified condition is false. Your condition is an assignment, so there's the error.
So change this
for($i=0; $row = $result->fetch(); $i++){
to this:
foreach ($result->fetch_all() as $row) {
$result->fetch_all() returns an associative array of the data.
With the foreach loop, you access every element in that array and can access it with $row['any-key-you-want']

PHP With Row Number And If Else

I am running a sql query and getting a count of the results using PHP. The below is the relevant parts of my syntax and what I want to do is if the count returned is >= 1 then display the grid (as it will be populated with 1 or more rows), but if the row count returned is 0 then display a warning on screen notifying the user.
My issue with the code is that it always returns the grid!
<?php
//Connection to server to run sql query and return results
$numofrows = mssql_num_rows($tsql);
?>
if ($numofrows >= 1) {
//Build out HTML Table
} else {
//write on screen there are no results to display
}
EDIT
This is how I am setting it up which appears to be the same as the link in the comments
<?php
//Connection to server to run sql query and return results
$numofrows = mssql_num_rows($tsql);
?>
if ($numofrows >= 1) {
<table border="1">
<thead>
<tr>
<th >Header 1 </th>
<th>Header 2 </th>
<th>Header 3 </th>
<th>Header 4 </th>
<th>Header 5</th>
<th>Header 6 </th>
</tr>
</thead>
<tbody>
<?php
foreach( $query as $res ) {
print "<tr>";
print "<td>" .$res->Field1."</td>";
print "<td>" .$res->Field2."</td>";
print "<td>" .$res->Field3."</td>";
print "<td>" .$res->Field4."</td>";
print "<td>" .$res->Field5."</td>";
print "<td>" .$res->Field6."</td>";
print "</tr>";
}
?>
} else {
echo "<strong>No Results</strong>"
}
Here's an approximate example, to help you out.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select Database
mysqli_select_db($conn, "test");
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body bgcolor="#06160F">
<table frame="hsides" style="color:#ffffff;">
<!-- Headers of the table -->
<tr>
<th>Header 1 </th>
<th>Header 2 </th>
<th>Header 3 </th>
<th>Header 4 </th>
<th>Header 5</th>
<th>Header 6 </th>
</tr>
</table>
</div>
<div>
<div>
<table border="1" width="960px">
<?php
$sql = "SELECT * FROM abc";
$result = mysqli_query($conn,$sql);
if($result)
{
if ($result->num_rows > 0)
{echo"<table>";
// output data of each row
while($row = $result->fetch_assoc())
{
echo"<tr>";
echo"<td width=120px align=\"center\">".$row["feild1"]."</td>";
echo"<td width=170px align=\"center\">".$row["feild2"]."</td>";
echo"<td width=120px align=\"center\">".$row[""feild3"]."</td>";
echo"<td width=170px align=\"center\">".$row["feild4"]."</td>";
echo"<td width=420px align=\"center\">".$row["feild5"]."</td>";
echo"<td width=420px align=\"center\">".$row["feild6"]."</td>";
echo"</tr>";
}
echo"</table>";
}
else
echo "<h3 align=\"center\">Nothing to show.</h3>";
}
else
echo "<br> Database error.";
$conn->close();
?>
</table>
</div>
<br>
</div>
</body>
</html>
By the way I'm using MySQL.
You can also check this answer: Checking for empty result (php, pdo, mysql)
I think also, you should use PDO.
Your if loop is outside the php section, so it won't be evaluated

Got stuck with populating mysql table in html table [duplicate]

Here the value of $table will be passed from another file and it will be a table name in a database.
With that table name ($table) am trying to fetch its column names and all the values in the table and make the table look like an actual one we see in phpMyAdmin.
As a result of the code below. I can only get the column names. But not the data. Please tell me what should i do to perform the task of showing the datas in the table
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php
//echo"select * from $table";
$qq=mysql_query("show columns from $table");
if(mysql_num_rows($qq)>0){
//$i=1;
echo '<tr>';
while($rs = mysql_fetch_row($qq))
{
$sel=mysql_query("select * from $table");
$fetch=mysql_fetch_object($sel);
//while($fetch=mysql_fetch_object($sel))
//{
?>
<td><?php echo $fetch->$rs[0];?></td>
<?php
//}
//$i++;
}
echo '</tr>';
}
else
{
?>
<tr>
<td colspan="11">No data to display</td>
</tr>
<?php
}
?>
</tbody>
</table>
Yes only columns will be printing because you are printing $rs[0];?> where $rs holds object for mysql_query qq and it holds your column query only not
"select * from $table"
this.
Why dont you try displaying columns as a single row in html table and then try displaying data from other php set with seperate query structure .
Hope this way it helps.
I coudn't fix your code because it was getting ugly, so i remade it:
php:
function mysql_fetch_all($res) {
$result = array();
while ($row = mysql_fetch_row($res)) {
$result[] = $row;
}
return $result;
}
function getColumnsAndData($table) {
$table = mysql_real_escape_string($table);
$q1 = mysql_query("show columns from $table");
$q2 = mysql_query("select * from $table");
return array(mysql_fetch_all($q1), mysql_fetch_all($q2));
}
list($columns, $data) = getColumnsAndData($table);
?>
html
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php foreach ($columns as $column): ?>
<td><?php echo $column[0] . ' ' . $column[1] ?></td>
<?php endforeach; ?>
<tr>
</thead>
<tbody>
<?php if (count($data) > 0): ?>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value ?></td>
<?php endforeach; ?>
<tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td>No data to display</td>
</tr>
<?php endif; ?>
</tbody>
</table>

Categories