I have a function called RandomPic() it will display a Random Photo from the Database. Now i want a Placeholder Image for the Gallerys Categories that are empty. but my code doesnt work. it doesn't show the placeholder image....any ideas? here is the code:
$sql = "SELECT * FROM wp_gallery WHERE catid = $id ORDER BY RAND() LIMIT 1";
if($result = $conn->query($sql)){
while ($row = mysqli_fetch_array($result))
{
if (mysqli_num_rows($result) > 0) {$imglos = '<img class="img-responsive" src="http://www.anyurl.com'.$row["filename"].'">';}
}
}
else{$imglos = 'PLACEHOLDERIMAGE GOES HERE';}
return $imglos;
}
Slightly change your code structure:
$sql = "SELECT * FROM wp_gallery WHERE catid = $id ORDER BY RAND() LIMIT 1";
if($result = $conn->query($sql))
{
if (mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_array($result))
$imglos = '<img class="img-responsive" src="http://www.anyurl.com'.$row["filename"].'">';
}
else
$imglos = 'TEST';
return $imglos;
}
else
return "database error!";
Some notes:
The mysqli_num_rows never changes after executing a query: it returns the number of rows in the resultset. So it makes no sense to check for this within a while.
There is no need for while, because your query can only return 0 or 1 results. If you do want to return multiple images you can append to $imglos:
while ( $row = mysqli_fetch_array($result) )
$imglos .= '<img class="img-responsive" src="http://www.anyurl.com'.$row["filename"].'">';
Related
/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
I've created different pages for 20 different rows in my php code, please have a look-
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = '';
}
if($page=='' || $page==1)
{
$page1=0;
}
else
{
$page1=($page*20)-20;
}
$query="SELECT * FROM issued_books limit $page1,20";
if($did_query_exec=mysqli_query($conn,$query))
{
while($query_exec=mysqli_fetch_array($did_query_exec, MYSQLI_ASSOC))
{
$isret = $query_exec["Date_returned"];
$dateret = $query_exec["Date_returned"];
$dateis = $query_exec['Date_issued'];
//echoing the values here
}
}
$query = "SELECT * FROM `issued_books`";
$Result = mysqli_query($conn, $query);
$cou = mysqli_num_rows($Result);
$a = $cou/20;
echo "</br></br>";
$a = ceil($a);
echo 'Page ';
for($b=$a; $b>=1; $b--)
{
echo "<a href='return-books.php?page=$b' style='text-decoration:none'>$b </a>";
}
The above code creates the link for all the pages with row 1 of page 1 contains the first record inserted by me.
I wish to display the recent most created record in row 1 of page 1
any kinda help would be really appreciated, thanks :)
You have to add ORDER BY with DESC:-
$query="SELECT * FROM `issued_books` ORDER BY id DESC limit $page1,20";
And
$query = "SELECT * FROM `issued_books` ORDER BY id DESC";
Note:- change the column name in ORDER BY according to your wish.
I want to display banner after first result of MySQL query, after banner it will display the remaining results.
Here is my code:
$result = mysqli_query($con,"SELECT id, name FROM database
WHERE id > '".$id."' LIMIT 5");
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_array($result)) {
$name = $row['name'];
echo $name;
}
}
Example Result:
Peter
Banner Code Here
Steve
David
Adam
As you not explained your question in well manner so tried on the basis of my understandings try this maybe helpful,
$isDispaly_Banner = true;
$result = mysqli_query($con,"SELECT id, name FROM database WHERE id > '".$id."' LIMIT 5");
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_array($result)) {
$name = $row['name'];
echo $name;
if($isDispaly_Banner){
echo "Banner COde Here";
$isDispaly_Banner = false;
}
}
}
In Mysql database I have column name "product_id" and there is while statement code to check if row exist ...
If value of product_id exist, then it will show "note-icon.png" image, if not exit it will say "No" but the problem is that let say that in "product_id" has four product_id's SAME NUMBER and then result will SHOW FOUR images, my question is that how to write code to show ONE image instead of FOUR images!
<?php
$sql = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
$query = "SELECT * FROM notes WHERE product_id = $product_id";
$results = mysqli_query($sql, $query);
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(isset($row['$product_id'])) {
echo "<p>No </p>";
} else {
echo '<img src="images/note-icon.png" width="16" height="16" />';
} }
?>
Select only one row:
MySQL:
SELECT * FROM notes WHERE product_id = ? LIMIT 1
SQL:
SELECT FIRST 1 * FROM notes WHERE product_id = ?
If you want to get all notes but draw one image per product you can do this:
$product_ids = array();
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
if(!isset($row['product_id'])) {
echo "<p>No </p>";
} else {
if(!isset($product_ids[$row['product_id']])) {
$product_ids[$row['product_id']] = 1;
echo '<img src="images/note-icon.png" width="16" height="16" />';
}
}
}
Some code annotations:
$product_id in your query allowed, only if you are cast it to int before
else use placeholders (prepared statements)
if(isset($row['$product_id']))
What's this? think should be:
if(!isset($row['product_id']))
also you forgot to check $results, that it is not false, before using..
U can use grouping
SELECT * FROM notes WHERE product_id = ? GROUP BY product_id
<?php
function comment($postid,$db_con)
{
$commentdiv='';
$sql="SELECT userid,time,comment FROM comments WHERE postid='$postid' LIMIT 3";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$uid=$row["userid"];
$timecomment=$row["time"];
$comment=$row["comment"];
$sql="SELECT username,photo FROM users WHERE id='$uid'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
$username=$row["username"];
$photo=$row["photo"];
$userphoto='<img src="xxx/'.$username.'/'.$photo.'">';
if($photo== NULL){
$userphoto = '<img src="xxx/default.png">';
}
$commentdiv.='<div class="xxxxxxx"><div class="yyyyyy">'.$userphoto.'</div><div class="zzzzz">'.$username.'</div><div class="vvvvv">'.$comment.'</div></div>';
}
return $commentdiv;
}
?>
I am new to PHP, I am trying to return 3 comments from above PHP code, but above code returns only 1 row from database, why does fetch array return only 1 row when there is more then 1 row?
Try,I use $query2 = mysqli_query($db_conx, $sql); for your second query,Because it will reset first loop
<?php
function comment($postid,$db_con)
{
$commentdiv='';
$sql="SELECT userid,time,comment FROM comments WHERE postid='$postid' LIMIT 3";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$uid=$row["userid"];
$timecomment=$row["time"];
$comment=$row["comment"];
$sql="SELECT username,photo FROM users WHERE id='$uid'";
$query2 = mysqli_query($db_conx, $sql); // added new variable
$row = mysqli_fetch_array($query2,MYSQLI_ASSOC);
$username=$row["username"];
$photo=$row["photo"];
$userphoto='<img src="xxx/'.$username.'/'.$photo.'">';
if($photo== NULL){
$userphoto = '<img src="xxx/default.png">';
}
$commentdiv.='<div class="xxxxxxx"><div class="yyyyyy">'.$userphoto.'</div><div class="zzzzz">'.$username.'</div><div class="vvvvv">'.$comment.'</div></div>';
}
return $commentdiv;
}
?>
You are overwriting the mysql resource variable that gives the result inside the while loop.
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
^^^^^^ Original resource variable
and inside the while loop again you are using the query variable
$query = mysqli_query($db_conx, $sql);
^^^^^^ Overwriting $query inside the while loop
One suggestion would be to rename the variable inside the loop to something else.