PHP table echoing 0 results - php

I'm trying to create a table to show users in my database and it's echoing 0 results for some reason. There are over 40,000 entries in my database so there is definitely something wrong with my PHP code but I'm just not sure where.
<?php
require 'session.php';
require 'header.php';
include 'database.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM clients ORDER BY time_edit DESC LIMIT 45";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border=1>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>ID</th>";
echo "<th>IP</th>";
echo "<th>Connections</th>";
echo "<th>Last Seen</th>";
echo "<th>Rank</th>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
$name=$row['name'];
$id=$row['id'];
$ip=$row['ip'];
$connections=$row['connections'];
$seen=$row['time_edit'];
$rank=$row['group_bits'];
echo "<tr>";
echo "<td align=center> $name </td>";
echo "<td align=center> $id </td>";
echo "<td align=center> $ip </td>";
echo "<td align=center> $connections </td>";
echo "<td align=center> $seen </td>";
echo "<td align=center> $rank </td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

Related

How to make sql query to display 1 result based on id in the database table

I need help, I cannot figure out, I cannot find why I am having errors and I am not able to achieve something freaking simple.
Long story short, I have a website to manage projects, so when I run the search function it throws a table with some records from the database, there is a button called "see details" which is assigned to a project id with database i.e. 21, 1, 48 etc, the problem is that when I click "see details" it throws everything from the table proposals instead of 1 project, no matter which button I click on, if its id 1, 21, 48, it prints everything.
details page
details.php:
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table WHERE id LIKE '_%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Notes</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
// 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);
}
?>
search/result page
proposals.php
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Options</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td> <a class='btn btn-primary' href='details.php?id={$row['id']}'>See details</a></td>";
echo "</tr>";
}
echo "</table>";
// 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);
}
?>
If you want to show only the selected element on your details page then you need to fetch only that selected item from the database.
First of all you should separate HTML from PHP. The best would be to have them in separate files. In PHP you prepare the data to be displayed and then in HTML you fill in the blanks with PHP values.
To select a value from MySQL using a given ID you must use prepared statements with parameter binding. So if you create your link in this way:
echo "<td> <a class='btn btn-primary' href='details.php?id=".urlencode($row['id'])."'>See details</a></td>";
You can receive this ID in your details page using $_GET['id']. You can bind that value to your WHERE clause in SQL.
<?php
include '../includes/config.php';
// Attempt select query execution
$stmt = $link->prepare("SELECT * FROM proposals_table WHERE id=?");
$stmt->bind_param('s', $_GET['id']);
$stmt->execute();
$proposals = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if($proposals) {
?>
<table class='table table-bordered'>
<tr>
<th>Organisation</th>
<th>Project</th>
<th>Proposal Date</th>
<th>Date Received</th>
<th>Notes</th>
</tr>
<?php foreach($proposals as $row): ?>
<tr>
<td><?=$row['company'] ?></td>
<td><?=$row['project'] ?></td>
<td><?=$row['proposal_date'] ?></td>
<td><?=$row['date_received'] ?></td>
<td><?=$row['notes'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
} else {
echo 'No records matching your query were found.';
}
And of course your config.php page should look like this:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'user', 'pass', 'db');
$link->set_charset('utf8mb4'); // always set the charset

while loop only returning 1 product row

Evening all,
I have the following script setup on the backend of an eCommerce platform I am developing. Essentially I want to pull the products through and also pull all of the current categories through so that products can be assigned to a particular category.
However my product edit page is only displaying 1 result.
Its interesting to note that if i remove the script relating to the last SQL query my products show but with no category options. With the script only 1 product shows with the correct categories.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
while($row = $result->fetch_array()) {
$category = $row['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
I would love any help on how I can show all products with all categories.
I have tried everything I can think of.
Thanks.
Stan.
You've used the same names of variables for products and categories queries. While the categories are inside of products loop you reassign that variable with new query and that's why it's crashes. Change name of variables for second query and that should fix your script.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql2="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result2 = mysqli_query($connection, $sql2);
// Counting Results
$count=mysqli_num_rows($result2);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result2 = mysqli_query($connection, $sql2)){
while($row2 = $result2->fetch_array()) {
$category = $row2['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

Combine two rows together

Maybe someone can help me out. Any help is appreciated! I'm trying to Combine the "???" row with "unknown files" row. So the total would be 4245. Just one row.
I'm using a while loop. Here is my code
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, username, COUNT(company), username FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company ORDER BY company ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date(' \ F jS Y - l');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
<th>Users</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You will have to calculate total of ??? and unknown file outside the while loop
$total = 0;
while($row = mysql_fetch_assoc($result))
{
if($row['company'] == NULL || $row['company'] == "unknown file")
$total += $row['COUNT(company)'];
}
Then you can use that total in the main output loop
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
if($row['company'] == "unknown file")
echo "<td>" . $total . "</td>";
else
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}

Display mysql data in formatted table

I am retrieving data from a database and displaying in a table. I would like the table to be in a 4 x 3 layout. The data retrieves just fine, it's the layout that isn't working. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href=''>";
echo "<img src='".$row['image']."' width='160' height='54'>";
echo "</a>";
echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
What is supposed to go in each cell is a picture of the vehicle with the name underneath the picture.
Here is an example from another website as to how I would like it to look:
http://www.denisonmotors.com
I am not stealing any information from their site I am just trying to get my data to display in the format that they have on their site.
I am not concerned about the empty href tag as they will be filled in later.
After doing some research, I think that I have to use 2 for loops to create the table stating how many columns and row that I want.
Is it as simple as moving the last 3 lines that close the outer table outside the while loop?
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
echo "<tr>";
echo "<td>";
while($row = $result->fetch_assoc())
{
echo "<table border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
The table generation is not correct. I think you don't mean to create a table inside another table, so I think this is the way you should do:
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
You have to create the table once, and then a row for each record.

Categories