Is there a way to run a mysql $query and specify and order by the value within a column. so if you have a table with column:name, and there are 2 people with the name 'john' but the first john has 'id':10 and the second john has 'id':20. I want to echo the first john(10) first. so something like:
$sql = mysql_query("SELECT firstname FROM users
WHERE email='$email'
AND id='(*smallest value*)'");
while($info = mysql_fetch_assoc($sql))
echo $info['firstname']
SELECT firstname FROM users WHERE email='$email' order by id asc limit 1
Try
SELECT firstname FROM users WHERE email='$email' order by id asc limit 1
$sql = mysql_query("SELECT firstname FROM users
WHERE email='$email'
ORDER BY id ASC");
will give you the John(10) before the John(20).
$sql = mysql_query("SELECT firstname FROM users
WHERE email='$email'
ORDER BY id ASC
LIMIT 1");
will give you only John(10).
Related
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 can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.
I wonder... is this possible to select a random row in my DB where only the record that contains field->Status = 0 ? because i only need a row that contains 0 in the field name=Status. if the status contains = 1 , then the row will not belong in the randomization.
my code in the query is
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1");<br>
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1 Where Status=0");
But it does not work.. i appreciate your replies.. thank you very much!
Try below code:
$result = mysql_query("SELECT * FROM `".$haha."` Where Status=0 ORDER BY RAND() limit 0,1");
Your where clause should be before then the order by clause.
And want to know what is the value of $haha.
You use the wrong syntax. The good one:
$result = mysql_query( " SELECT * FROM `$haha` Where Status=0 ORDER BY RAND() limit 1");
I have an SQL statement:
$sql = "SELECT * FROM user_events WHERE userID =" . $uid . " OR groupID = (IN ($gId) WHERE userID IS NULL) ORDER BY timestamp DESC";
This looks like this when printed:
SELECT * FROM user_events WHERE userID = 34 OR groupID =(IN (44,45) WHERE uID IS NULL) ORDER BY timestamp DESC
What I'm trying to do is select from the database any row where either the userId matches a specific variable or where the groupID matches a CSV variable where the userID is NULL.
Can anyone explain what I'm doing wrong here? I'm quite stuck!
IN is an operator, not a... that. You also want AND, not WHERE.
$sql = "SELECT * FROM user_events
WHERE userID = $uid
OR groupID IN ($gId) AND userID IS NULL
ORDER BY timestamp DESC";
$sql = "SELECT * FROM user_events
WHERE userID =" . $uid . "
OR (groupID IN ($gId) AND userID IS NULL) ORDER BY timestamp DESC";
I think that you want something like:
SELECT * FROM user_events
WHERE (userID = 34 OR groupID IN (44,45)) and uID IS NULL
ORDER BY timestamp DESC
In PHP, this might look like:
$sql = "SELECT * FROM user_events WHERE (userID =". $uid . " OR groupID IN ($gId)) AND userID IS NULL ORDER BY timestamp DESC";
How can i select the first 5 results and then add a see more option?
Below is the current code:
<?php
$query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "";
$i=0;
while ($i < $num) {
$otheris=mysql_result($result,$i,"sender_full_name");
$sysid=mysql_result($result,$i,"sender_id");
$dob=mysql_result($result,$i,"dob");
// If $dob is empty
if (empty($dob)) {
$dob = "No new messages -
<a id=missingdob href=/test.php?id=$uid>
<bold>check later</bold></a>";
}
echo "<br><div id=linkcontain>
<a id=otherlink href=$mem/profile.php?id=$uid>
$manitis</a>
<br><div id=dobpres>$dob</div></div>";
echo "";
$i++;
}
?>
You should try to select 6 rows first time and if you get 6 records then show first 5 with a "show more option"
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 0, 6";
For subsequent times you should have your query like this:
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 6, 5";
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 11, 5";
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 16, 5";
...
...
And every time "show more option" if you are able to fetch requested number of records.
$query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 5";
http://dev.mysql.com/doc/refman/5.5/en/select.html
you could consider LIMIT 6 display only up to 5 if 6th exist display there is more options...
window.onload = function(){
$(".box").hide();
$(".box").slice(0, 5).show(); // select the first ten
$("#load").click(function(e){ // click event for load more
e.preventDefault();
$("div:hidden").slice(0, -1).show(); // select next hidden divs and show them
$("#load-more-div").html(' ');
});}