I am performing a routine to check if books exist in a database given a range. I want to echo if no books are found. I have the following:
$search = mysqli_query($con,$query);
while(list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)){
if(!empty($book_id)) {
echo "Book ID: " . $book_id . "<br/>";
echo "Book Title: " . $title . "<br/>";
echo "Authors: " . $authors . "<br/>";
echo "Description: " . $description . "<br/>";
echo "Price: " . $price . "<br/>";
echo "<br/>";
}
if(empty($book_id)){
echo "Fail";
}
}
If no books are found nothing is printed. The echo does not work? How come?
Thanks
Because if no records are returned, you won't enter in the while at all, as $book_is will contain the value false and while(false)... you know
In this situation you may use mysqli_num_rows to check if there are rows found
Your no results echo is inside the while loop which will never get called
while (list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)) {
if (!empty($book_id)) {
echo "Book ID: " . $book_id . "<br/>";
echo "Book Title: " . $title . "<br/>";
echo "Authors: " . $authors . "<br/>";
echo "Description: " . $description . "<br/>";
echo "Price: " . $price . "<br/>";
echo "<br/>";
}
}
if (empty($book_id)) {
echo "Fail";
}
Move it outside like the code above
It is because your while loop has not been accessed at all. Try to echo something out of the if conditions, and see if it get's displayed.
Related
Ive included a block of code below detailling how im placing sections of data from a table in my database in their own divs. However, im new to PHP and cant find out how to also output the "cup_id" from the database into their respective divs here: echo $cup["cup_name"] . "<br />"; Thanks for all the help in advance!
So in short how do i get this to work: echo $cup["cup_id", "cup_name"] . "<br />";
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
}
?>
Do you want to concatenate strings? use the dot operator:
echo $cup["cup_id"] . $cup["cup_name"];
And if you want to print it in another div, make this:
echo '<div class="cup-info">';
echo $cup["cup_id"] . "<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"] . "<br />";
echo '</div>';
if you want the name and id to be in the same raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"] . $cup["cup_name"]."<br />";
echo '</div>';
}
?>
if you want the name and id to be in different raw then you can follow this method
<?php
require_once("action/dbcon.php"); // Get the database connection
$get_cup = "SELECT * FROM cups";
$show_cup = mysqli_query($conn, $get_cup);
if (!$show_cup) {
echo "Could not load cup. " . "(" . mysqli_error($conn) . ")";
}
while ($cup = mysqli_fetch_assoc($show_cup)) {
echo '<div class="cup-info">';
echo $cup["cup_id"]."<br />";
echo '</div>';
echo '<div class="cup-info">';
echo $cup["cup_name"]."<br />";
echo '</div>';
}
?>
I have a select query that pulls all the data from the database but it displays it like so:
First name: Jasmine
Last name: Santiago
Address: 123 Butt street
City: Butt hill
Customer ID: 12
I'm trying to get it to display like this:
First name: Last name: Address: City: Customer ID:
Jasmine Santiago 123 Butt street Butt hill 12
with more rows here of just the data not the labels
But I have more than one row of data...So I'm not sure where i'm going wrong. Here's my query and echo statement.
if(isset($_POST["get-user-info"])) {
$user_info_sql = "SELECT * from customer_info";
$user_info_result = $conn->query($user_info_sql);
if ($user_info_result->num_rows > 0) {
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><th>First name: </th>" . $row["First_name"]. "</tr></br>" . "<tr>Last name: " . $row["Last_name"] . "</tr></br>" . "<tr>Address: " . $row["Address"]. "</tr></br> " . "<tr>City: " . $row["City"] . "</tr></br>" . "Customer ID: " . $row["Customer_id"] . "</br>" . "</br></br>";
}
} else {
echo "No results found";
}
}
As you can see I kind of started to do a tr/th/td but i got lost...I assume maybe I need to echo out everything like....
echo table
echo tr
echo th
...but then i get lost right here because this would echo a label again...
You should display the header only once:
if ($user_info_result->num_rows > 0) {
// display table header
echo "<table><tr><th>First name: </th><th>Last name: </th><th>Address: </th><th>City: </th><th>Customer ID: </th></tr>";
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><td>". $row["First_name"]. "</td>" .
"<td>". $row["Last_name"]. "</td>" .
"<td>". $row["Address"]. "</td>" .
"<td>". $row["City"]. "</td>" .
"<td>". $row["Customer_id"]. "</td></tr>";
}
echo "</table>";
}
try something like this:
<?php
if(isset($_POST["get-user-info"])) {
$user_info_sql = "SELECT * from customer_info";
$user_info_result = $conn->query($user_info_sql);
if ($user_info_result->num_rows > 0) {
echo "<tr><th>First name: </th><th>Last name:</th><th>Address:</th><th>City:</th><th>Customer ID:</th></tr>";
while($row = mysqli_fetch_assoc($user_info_result)) {
echo "<tr><td>". $row["First_name"]."</td><td>" . $row["Last_name"] . "</td><td>" . $row["Address"]. "<td></td>" . $row["City"] . "<td></td>" . $row["Customer_id"] . "</td></tr>";
}
} else {
echo "No results found";
}
}
Sorry for the confusing title. I am as confused.
SO what I am trying to do is print results from the MySQL database I've got. I have a checkbox value as "yes" in my DB and I would like to replace this to some other word while printing out the results.
I've tried different ways but all of them break the page, because I'm new to this and have no idea what I am doing.
Here is my code so far (only put what I think is relevant):
$keyword= "";
if (isset($_POST["keyword"])) {
$keyword = ($_POST["keyword"]);
}
$results = mysqli_query($con, "SELECT * FROM pcdata WHERE name LIKE '$keyword' LIMIT 0, 25");
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}
Why don't you try a simple if inside your while:
$myvariable='';
if($row['help']='yes'){
$myvariable='put_something_here';
}
And in your echo just do:
echo "Need help ASAP: " . $myvariable . "<br>";
Or a ternary solution:
$row['help'] == 'yes' ? 'put_something_here' : 'what_do_you_want_to_print_if_it_is_not_yes'
Try this code:
$keyword= "";
if (isset($_POST["keyword"]))
$keyword=($_POST["keyword"]);
$results=mysqli_query($con,"
SELECT *
FROM pcdata
WHERE name LIKE '$keyword' LIMIT 0,25");
if (!$results) {
echo "Not found...";
}
else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results))
{
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: ";
if ($row['help'] === 'yes'){
echo 'YES';
} else {
echo 'NO';
}
echo '<br>';
}
We check the value of $row['help'] and if it "yes" printing 'YES', if other - printing 'NO'
you can also use select statement combined with Case statement which will result as desired try following code
$keyword= "";
if (isset($_POST["keyword"]))
{
$keyword = ($_POST["keyword"]);
}
//used different variable to build query
$selectquery="SELECT id,name,model,pctype,info CASE WHEN help='yes' THEN 'Your Yes String' WHEN help='no' THEN 'Your No String' else 'nothing' END as help FROM pcdata where name like '$keyword'" ;
//passed $selectquery to mysqli_qery
$results = mysqli_query($con, $selectquery);
if (!$results) {
echo "Not found...";
} else {
echo "Found...<br>";
}
while ($row = mysqli_fetch_array($results)) {
echo "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Model: " . $row['model'] . "<br>";
echo "Operating system: " . $row['model'] . "<br>";
echo "Type of computer: " . $row['pctype'] . "<br>";
echo "Other information: " . $row['info'] . "<br>";
echo "Need help ASAP: " . $row['help'] . "<br>";
}
<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.
I have the following code:
$recent = apc_fetch('recn');
$recent[9] = $recent[8];
$recent[8] = $recent[7];
$recent[7] = $recent[6];
$recent[6] = $recent[5];
$recent[5] = $recent[4];
$recent[4] = $recent[3];
$recent[3] = $recent[2];
$recent[2] = $recent[1];
$recent[1] = $recent[0];
$rec = array_pop($recent);
$recent[0] = $name;
apc_store('recn', $recent);
Each time the page is reloaded, I wanted $name to be 1st, and than move whatever is 2nd down the list, and so on. I echo the array like so:
echo "Most Recent Songs: <br>";
echo "1. " . $name. "<br>";
echo "2. " . $recent[1] . "<br>";
echo "3. " . $recent[2] . "<br>";
echo "4. " . $recent[3] . "<br>";
echo "5. " . $recent[4] . "<br>";
echo "6. " . $recent[5] . "<br>";
echo "7. " . $recent[6] . "<br>";
echo "8. " . $recent[7] . "<br>";
echo "9. " . $recent[8] . "<br>";
echo "10. " . $recent[9] . "<br>";
But only the first entry shows up.
Found it! Thanks to #giaour for the array_unshift suggestion, and #Chelsea for helping me understand array_pop Heres my new (working) code:
$recent = apc_fetch('recn');
array_unshift($recent, $name);
$rec = array_pop($recent);
apc_store('recn', $recent);