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;
}
}
}
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 want to print
<?php echo isset($meta['category_id']) ? $meta['category_id'] : '' ; ?>
This code is here in place number 2
`<?php
$sql = "SELECT title FROM posts WHERE category_id = '2'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["title"]. "<br>";
}
} else {
echo "0 results";
}
?>`
This is a screenshot, with the knowledge that "category_id" will call the number of the group to which the post belongs.
Thank you all the solution was
$ sql = "SELECT title FROM posts WHERE category_id =". $ meta ['category_id'];
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.
this PHP code, is supposed to get the amount of mails received by the user (internal mail system). It works fine, until i add the "AND deleted = '0'" in the SQL code below. And in the SQL Table, all mails is set to "0" in the deleted input.
$user = $get2['username'];
$sql = "SELECT COUNT(*) FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['COUNT(*)']. "";
}
} else {
echo "0";
}
$conn->close();
Try with below code..
`$user = $get2['username']`;
$sql = "SELECT COUNT(*) AS COUNTS FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['COUNTS']. "";
}
} else {
echo "0";
}
$conn->close();
Already solved.
Did not connect properly to the database which i was sure it did.
You have written query with just a little mistake.
So please try following query :
$sql = "SELECT COUNT(*) AS counted FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['counted']. "";
}
} else {
echo "Sorry";
}
$conn->close();
I hope this will work.
Change AND deleted = '0' ORDER BY to AND deleted = 0 ORDER BY
I'm guessing that the column DELETED is some sort of number data format instead of a string format.
i have a database table 'movies'. in this table there are 25 columns of information per row. Ie, movie title, poster, cast, synopsis etc.
At the moment i am fetching the information like this
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$moviedetails['title']=$row['title'];
$moviedetails['poster']=$row['poster'];
}
}
else {
echo 'NO RESULTS';
}
because i have 25 columns its long work writing out each variable. is there a way to fetch the information and i can then call to it by using
$moviedetails['column name']
ie
im new to php and mysql so any help appreciated.
thanks
lee
$moviedetails['title']
fetches the information from the 'title' column.
while($row = $result->fetch_assoc()) {
foreach ($row as $key => $value){
$moviedetails[$key]=$value;
}
}
This input the same key and the same value into new array
Is that what you're looking for?
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($moviedetails = $result->fetch_assoc()) {
//just use moviedetails for what you need
}
} else {
echo 'NO RESULTS';
}
Try this:
while($moviedetails[]=$result->fetch_assoc()){}
then you loop by it like this:
foreach($moviedetails as $num->row)
{
echo $row['title'];
}