I am working on a small project, and I have code that contains an "if" statement. However, I noticed whenever I fetch data with the value of 0 it does bring it back (var_dump()) but it doesn't display it because I guess the code thinks it's a useless value?
<?php
include_once 'dbh.inc.php';
$sqli = "SELECT datum, v, vo, nav FROM months;";
$result = mysqli_query($conn, $sqli);
$resultCheck = mysqli_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>Datum</th>";
echo "<th> Vakantie uren</th>";
echo "<th> Vakantie uren opgennomen </th>";
echo "<th> Nieuw aantal vakantie uren</th>";
echo "</tr>";
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) {
echo "<tr>";
echo "<td>".$row['datum']."</td>";
echo "<td>".$row['v']."</td>";
echo "<td>".$row['vo']."</td>";
echo "<td>".$row['nav']."</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>
please notice the if ($row['datum'] etc.
When I take this piece of code out it renders even the values of 0, however if I end up having values that haven't been filled out (for example u have 5 tables in your database and the user fills 4 in because the fifth one is a useless one for him) it fetches the 5th one back as well and I end up having a very ugly empty spot. It doesn't look very nice.
My question is, is there a possible way to tell php (even with an "if" statement) that the value 0 is a value I want displayed?
If I understand you correctly, You don't want to display the row when there are no values but you want to display it when there are values even if the values are zeros.
Your if statement does not work because 0 = false in php.
Therefore consider modifying your if statement like so:
<?php
include_once 'dbh.inc.php';
$sqli = "SELECT datum, v, vo, nav FROM months;";
$result = mysqli_query($conn, $sqli);
$resultCheck = mysqli_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>Datum</th>";
echo "<th> Vakantie uren</th>";
echo "<th> Vakantie uren opgennomen </th>";
echo "<th> Nieuw aantal vakantie uren</th>";
echo "</tr>";
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
if( (!empty($row['datum']) || $row['datum'] == 0)
&& (!empty($row['v']) || $row['v'] == 0)
&& (!empty($row['vo']) || $row['vo'] == 0)
&& (!empty($row['nav']) || $row['nav'] ==0)
) {
echo "<tr>";
echo "<td>".$row['datum']."</td>";
echo "<td>".$row['v']."</td>";
echo "<td>".$row['vo']."</td>";
echo "<td>".$row['nav']."</td>";
echo "</tr>";
}
}
echo "</table>";
}
?>
Well, in PHP, empty is evaluated as false, so your if condition will be false.
If you want to display 0 where the value is empty, you can use the Ternay Operator:
$foo = '';
echo $foo ? $foo : '0'; // 0
Check it out: https://3v4l.org/Beem1.
Related
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I am currently trying to output a the results of a MySQL table in PHP. I have a general understanding of web based programming but not enough to debug my code. I know the SQL is good, and the database is linked to my site it's just a matter of making it post to a table. I will post the code and would appreciate some help:
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
All that I get from this is a no results output.
You forgot to close the query with a double quote.
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
$stmt -> execute();
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
Add $stmt -> execute();
You can also use $stmt -> store_result(); after the execute part
I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.
This is my results code by id from the search script. and returns table for each person
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo"</thead>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
echo"</table>";
echo "</center>\n";}
You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement
$id = $_GET['id'];
$id=mysql_real_escape_string($id);
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
$row=mysql_num_rows($rst);
if($row>0){
//outside while loop
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th>";
echo"</thead>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
}
//outside while loop
echo"</table>";
echo "</center>\n";
}
Note:- Don't use mysql because it is deprecated instead use mysqli or
PDO
To prevent sql injection check this link How can I prevent SQL-injection in PHP?
You have to get table part outside from while loop.
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo "</thead><tbody>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";
}
echo"</tbody></table>";
echo "</center>\n";
I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
I'm having trouble echoing data into a HTML table.
It comes out like that:
But it should be:
Here's the code. What am I doing wrong?
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</table>";
}
}
else{
echo "No results";
}
}
?>
The problem is that you keep outputting a new table for each iteration.
Your code should look like this:
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table>"; // Start the table
// Output the table headers
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>"; // End the table
}
else{
echo "No results";
}
}
?>
just put echo "<table>"; and first tr creation statment out side of while loop and also put closing of table after finishing of while loop and see i am sure it will work.
Try
<?php
echo "<table><tr><td>Model name</td><td>Year</td></tr>";
while($results = mysql_fetch_array($raw_results))
{
echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>";
}
echo "</table>";
?>
use this code, you have to first start the table, use while loop to iterate the result, and then close the table.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
Remove table code from while loop and put outside.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
replace your if block, use the following code , hope it may help you
if(mysql_num_rows($raw_results) > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}echo "</table>";
}
i try to store the booking booths into database based on user selection, there are 10 check boxes for each booths and user can choose which day they want to reserve booths. For each check box has it own field in database, if user choose booth A01, D1 and D2, when he press reserve button, it will insert value into D1 and D2. But I dont know how to get the checked checkbox value to store in database
my booth interface
http://i.imgur.com/umYcI.gif
my table structure
http://i.imgur.com/vKh6R.gif
My coding
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
}
include('db.php');
if($_REQUEST){
$id = $_REQUEST['search_category_id'];
$query2 = mysql_query("SELECT filenameBig, filename, url FROM eventinfo where eventID ='$id'");
$row = mysql_fetch_array($query2, MYSQL_ASSOC);
if($id == -1)
{
echo "<style type='text/css'>#btn_submit{visibility:hidden}</style>";
}
else{
/*echo "<a href='{$row['url']}'>Click me!</a>";*/
echo "<p><br><img src='{$row['filename']}' alt='' /></p>";
echo "<p></p>";
echo "<p align='right'><a href='$row[filenameBig]' target='_blank'>Click to view large image</a></p>";
echo "<hr size='1'>";
echo "<div style='padding-left:4px;' align='left'><strong>Booths Listing</strong>";
echo "<p></p>";
$query = "select boothAlias, totalDay from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";
$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);
echo "<table border='0' style='width:400px;table-layout:fixed' >";
$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
if($day==0){
echo "<th>Booth</th>";
}else{
echo "<th>D".$day."</th>";
}
}
while($rows = mysql_fetch_array($result2)){
$boothAlias=$rows['boothAlias'];
$totalDay=$rows['totalDay'];
echo "<tr><td>$boothAlias</td>";
for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
echo "<td><input name='day2[]' type='checkbox' value='$day2' /></td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I think that SET type would be good solution for this.
http://dev.mysql.com/doc/refman/5.0/en/set.html