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
Related
So if you feel the question is repeated ,I have tried other solutions but not working.I have link of images stored in Database(phpMyAdmin) as string.I want to load the image as a part of the second column of a table from DB using the links.
<table style="width:100%" id="to1">
<tr>
<th>ID</th>
<th>Featured Image</th>
<th>Title</th>
<th>Brand Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Shipping Cost</th>
<th>Delivery Time </th>
<th>Delivery Distance</th>
</tr>
<?php
$conn = mysqli_connect("localhost","root", "", "products");
if ($conn-> connect_error){
die("Connection Failed: ". $conn-> connect_error);
}
$sql = "SELECT id,featured_image,title,brand_name,price,quantity,shipping_cost,delivery_time,delivery_distance from products";
$result = $conn-> query($sql);
if ($result-> num_rows > 0){
while ($row = $result-> fetch_assoc()){
$val = $row["featured_image"];
echo "<tr><td>".$row["id"] ."</td><td>". '<img src='<?= **HERE** ?>' alt="">' ."</td><td>".$row["title"] ."</td><td>".$row["brand_name"] ."</td><td>".$row["price"] ."</td><td>".$row["quantity"] ."</td><td>".$row["shipping_cost"] ."</td><td>".$row["delivery_time"] ."</td><td>".$row["delivery_distance"] ."</td></tr>";
}
echo "</table>";
}
else{
echo "0 tables";
}
$conn-> close();
?>
I have shown the place as here .please suggest some ways so that I can show images instead of links in the table.
Here is what I want it to look like.
I think, you had quotes problems:
UPDATED:
echo "<tr><td>".$row["id"] ."</td><td><img src='".$row["featured_image"]."' alt=''></td><td>".$row["title"]."</td><td>".$row["brand_name"]."</td><td>".$row["price"]."</td><td>".$row["quantity"]."</td><td>".$row["shipping_cost"]."</td><td>".$row["delivery_time"]."</td><td>".$row["delivery_distance"] ."</td></tr>";
echo "
<tr>
<td>.$row["id"].</td>
<td>.<img src='$row["featured_image"]' alt="">.</td>
<td>.$row["title"].</td>
<td>.$row["brand_name"].</td>
<td>.$row["price"].</td>
<td>.$row["quantity"].</td>
<td>.$row["shipping_cost"].</td>
<td>.$row["delivery_time"].</td>
<td>.$row["delivery_distance"].</td>
</tr>"
Try this
i have a website project, where customers will need to search and the results will display, so i have done all that, but the problem is when a search results comes up,if there's more than 1 row,it dublicate the results on the same row,here are the code.
<h1 style="font-family:calibri;color:#13CC0D;text-align:center;">BODABODA SEARCH RESULTS</h1>
<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
<tr>
<th style="border-left: 1px solid #C1DAD7"> Region</th>
<th> District </th>
<th> Ward </th>
<th> Street</th>
<th> Driver Name </th>
<th>Identification Type</th>
<th>Identification Number</th>
<th>Motorcycle Type</th>
<th>Motorcycle Reg No</th>
<th>Phone Number</th>
</tr>
</thead>
<?php
$conn = mysqli_connect('localhost', 'efalococ_calcia', '*ad4#zNQ=nfN') or die('can not connect to the server'.mysqli_error);
mysqli_select_db($conn, 'efalococ_safirii');
if(isset($_POST['search'])){
$query = $_POST['region'];
$query1 = $_POST['district'];
$query2 = $_POST['ward'];
$query3 = $_POST['street'];
$results = mysqli_query($conn,"SELECT * FROM bodaboda WHERE (`region` LIKE '%".$query."%') && (`district` LIKE '%".$query1."%') && (`ward` LIKE '%".$query2."%') && (`street` LIKE '%".$query3."%')") or die(mysql_error());
if(mysqli_num_rows($results) >0){
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
}
}else{
echo '<span style="color:red;font-family:cursive;font-size:14px;">No results found, Please Modify your search </span> Click here'.mysqli_error($conn);
}
}
?>
</table>
And the results shows like this!
Can you please help on that? any idea?
You just need to use the <tr> for every row.
The tag defines a row in an HTML table.
Learn more about HTML tr tag
while($row = mysqli_fetch_array($results)) {
echo "<tr>";
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "</tr>";
}
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
Basically, I want to make each tuple point to different URL so that I can view the semesters of the clicked department, and I am viewing this in a table.
I tried passing an href tag before echo in this code, but that is taking me to the same link when I press any tuple in the table.
<!Doctype html>
<table border="1" id="table">
<p>
<tr>
<th bgcolor="#9999FF">Departements</th>
</>
</tr>
<?php $servername="localhost" ; $username="root" ; $password="" ; $dbname="dbms_pro" ; // Create connection $conn=n ew mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT dep_name FROM department"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
<tr>
<td department:>" . $row["dep_name"]. "</tr>
</td>"; } } else { echo "0 results"; } $conn->close(); ?>
</table>
</html>
There is basically one tag in html to make hyperlinks and it is a (http://www.w3schools.com/tags/tag_a.asp):
You need to iterate over array and add a unique id to href attribute to make what you want. Example code below, however it's not good to mix html and php in one file.
<!Doctype html>
<head></head>
<body>
<table border="1" id="table">
<p>
<tr>
<th bgcolor="#9999FF">Departements</th>
</>
</tr>
<?php foreach ($departements as $row) {
echo "
<tr>
<td><a href='/department/{$row["id"]}'>{$row["dep_name"]}</tr>
</td>";
}
?>
</table>
</body>
</html>
I have a datatable to show fields from a MYSQL database however I keep getting the error:
DataTables warning (table id = 'My Table'): Requested unknown
parameter '0' from the data source
Here is my code:
<table class="table table-striped table-bordered" id="sample_1">
<thead>
<tr>
<th style="width:8px;"><input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes" /></th>
<th>Username</th>
<th class="hidden-phone">Email</th>
<th class="hidden-phone">Points</th>
<th class="hidden-phone">Joined</th>
<th class="hidden-phone"></th>
</tr>
</thead>
<tbody>
<?php
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products ")or die('You need enter a catagory ' );
echo "<tr>"; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$prodname = $row['prodname'];
$prodID = $row['prodID'];
$catagory = $row['catagory'];
echo "<tr class='odd gradeX'>
<td><input type='checkbox' class='checkboxes' value='1'/></td>
<td class='hidden-phone'>$prodID</td>
<td class='hidden-phone'>$prodname</td>
<td class='hidden-phone'>$catagory</td>
<td class='hidden-phone'><a href='deleteproduct.php?q=$prodID'><button class='btn btn-danger'type='submit'><font color='white'>Delete</font> </a>
";
echo "</tr><tr>"; // it's time no move to next row
}
echo "</tr>"; // last row ending
?>
</tbody>
</table>
Does anyone know what this error means?
The number of td's should match what you have given while calling out the datatable();
Quick fix(if you do not want to customize the default functionalities) would be :
$('#sample_1').dataTable();
If you have given anything inside datatable({/configs/}) just remove it completely and it should work well.