sql statement select from DB limit didn't work - php

i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work

SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC

Related

How can i order by id ascending?

I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";

php select reords from multiple tables and compare results

This first statement selects all users a user is following and the second statement select all notes of the user and all the other users they are following, I am trying to combine two select statement:
$get_user = $user_data['username']; $get_following = mysqli_query($con,"SELECT * FROM follows WHEREfollower= '$get_user' ")
$query = mysqli_query($con,"SELECT * FROM posts WHERE username = '$followe' OR username = '$get_user' ORDER BY date_added DESC LIMIT 0,5 ");
I tried using the following code, but it only fetches a single user's when the user is user following more than person:
$get_following = mysqli_query($con,"SELECT * FROM follows WHEREfollower= '$get_user' ");
$ffg = mysqli_fetch_assoc($get_following);
$ff = $ffg['follower'];
$query = mysqli_query($con,"SELECT * FROM posts WHERE username = '$ff' OR username = '$get_user' ORDER BY date_added DESC LIMIT 0,5 ");
Pls kindly help fix this problem, thanks.

How to display multiple fetched data?

I am using the following to fetch the latest articles from the database:
//Latest Article
$title_query1 = "SELECT title FROM articles ORDER BY id DESC LIMIT 1";
$description_query1 = "SELECT description FROM articles ORDER BY id DESC LIMIT 1";
$content_query1 = "SELECT content FROM articles ORDER BY id DESC LIMIT 1";
$image_query1 = "SELECT image FROM articles ORDER BY id DESC LIMIT 1";
$title_result1 = mysqli_query($con, $title_query1) or die(mysqli_error($con));
$description_result1 = mysqli_query($con, $description_query1) or die(mysqli_error($con));
$content_result1 = mysqli_query($con, $content_query1) or die(mysqli_error($con));
$image_result1 = mysqli_query($con, $image_query1) or die(mysqli_error($con));
//Second Latest Article
$title_query2 = "SELECT title FROM articles ORDER BY id DESC LIMIT 2,1";
$description_query2 = "SELECT description FROM articles ORDER BY id DESC LIMIT 2,1";
$content_query2 = "SELECT content FROM articles ORDER BY id DESC LIMIT 2,1";
$image_query2 = "SELECT image FROM articles ORDER BY id DESC LIMIT 2,1";
$title_result2 = mysqli_query($con, $title_query2) or die(mysqli_error($con));
$description_result2 = mysqli_query($con, $description_query2) or die(mysqli_error($con));
$content_result2 = mysqli_query($con, $content_query2) or die(mysqli_error($con));
$image_result2 = mysqli_query($con, $image_query2) or die(mysqli_error($con));
However, i'm not sure how I can then do something like this:
<h1>Here is the first article: <?php $title_result1 ?><h1>
<h2>Here is the first article description: <?php $description_result1 ?>
<h1>Here is the second article: <?php $title_result2 ?><h1>
<h2>Here is the second article description: <?php $description_result2 ?>
Also, is this method not good? If I am going to do this for 100+ articles, will it cause the web page to load slowly?
Thanks
You do not need to do a single query for each column. You can get all columns for a row by doing select * Also, as mentioned, you can fetch as many rows as you want, and loop through them.
I prefer the while method.. Example, show last 100 articles
// fetch latest 100
$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT 100";
if ($result = mysqli_query($con, $sql)){
// got results, convert result object to array called $row
while ($row = mysqli_fetch_array($result)) {
// echo out $row array elements with
//the column names from the database as array index
echo '<h2>'. $row['title'] .'</h2>'; // example wrap results with HTML
echo '<b>' .$row['description'] .'</b>';
echo $row['content'];
echo <img src="'. $row['image'] .'" title="'.$row['description'].'">';
echo '<br>'; //so next result is on new line
} // end of while loop
} else {
//no result, error
}

How to select rows in MySQL/PHP?

I am trying to display my first 10 users and their information by uid. When I use the code below, it only returns a single uid:
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
echo $result->fetch_object()->uid;
What function am I supposed to use to display rows correctly?
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($var = $result->fetch_object()->uid){
echo $var;
}
Should do the trick
If you get a list of object you should use a loop for show them
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_object($result)) {
$row->uid;
}

Random Image PHP/SQL

I have 2 database tables. loppe containing username and loppeID, and billeder containing loppeID and billedeNavn.
I would like to have a div containing 2 random billedeNavn from the same username. I have tried some things but I can only figure out how to get 2 billedeNavn from the same loppeID (username). I'm pretty new to all this. So would me grateful for any help.
<?php
$frontimage = ("SELECT * FROM loppe RIGHT JOIN billeder ON loppe.loppeID=billeder.loppeID WHERE loppe.username = '$username' ORDER BY RAND()");
$st = $db->prepare($frontimage);
$st->execute();
$row = $st->fetch();
$loppeid = $row['loppeID'];
$loppe = $loppeid;
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";
$stmt = $db->prepare($sql);
$stmt->execute(array("loppeID" => $loppe));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$fname = $row['billedeNavn'];
echo '<div class="col-xs-6 loppe-pic-outer"><img class="img-responsive loppe-pic" src="loppebilleder/'.$fname.'"></div>';
}
?>
Have you tried this change your
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";?
to this
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
A good way to figure out what is going on when using php and database is after you create your query do something like
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
die($sql);
So with this you will see what the database is really querying and you can check for in your sql string or query.

Categories