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.
Related
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 ";
I got the following Mysql command:
// Call on a random style ID to display in rating window which user hasn't seen yet.
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE NOT
viewedByUser = (NOT LIKE '%$userID%') ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
I want to use the WHERE NOT function to search through the field "viewedByUser" after a string that does not contain the same string as the variable $userID.
What options have I got here?
Try This :-
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE
viewedByUser NOT LIKE '%$userID%' ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
You can directly use != instead of NOT LIKE and there is nothing called WHERE NOT
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser != $userID ORDER BY RAND() LIMIT 1");
Since they are email addresses, you can do:
$userId = 'abc#abc.com xyz#xyz.com';
$userId = explode(' ', $userId);
echo $userStr = implode("', '", $userId);
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser NOT IN ('" . $userStr . "') ORDER BY RAND() LIMIT 1");
If you need a (not) like on the viewedByUser you can simply use
"SELECT pictureID,userID
FROM styles W
WHERE viewedByUser = NOT LIKE '%$userID%'
ORDER BY RAND()
LIMIT 1";
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.
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
I am NOT experienced with php/mysql at all, this is what I have so far.
I want to make a section on my webpage that shows 5 random users, so I have made 5 querys output 1 user each, its working fine, but I have some things I need help adding, I want the username I am currently logged in as excluded, and I want there to be no repeat usernames. My code I have so far is below, help is extremely appreciated.
include "connect.php";
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random2 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random3 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random4 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random5 = $row['username'];
echo "
<div class='well well-sm' style='padding-top:4px;padding-bottom:8px; margin-bottom:8px; overflow:hidden; background:white;'>
<a href='./$random'>$random</a><br>
<a href='./$random2'>$random2</a><br>
<a href='./$random3'>$random3</a><br>
<a href='./$random4'>$random4</a><br>
<a href='./$random5'>$random5</a>
</div>
</div>
$query = mysqli_query('SELECT * FROM users WHERE username <> "' . mysqli_real_escape_string($connection, $username) . '" ORDER BY rand() LIMIT 5');
while($row = mysqli_fetch_array($query)) {
$random[] = $row['username'];
}
foreach ($random as $random_user) { //echo name of each random user
echo $random_user . '<br />';
}
If your username is stored in $username you can do
$list= array();
$list[]=$username;
$query = mysql_query("SELECT * FROM users WHERE username NOT IN $list ORDER BY rand() LIMIT 5");
Then you can simply add the usernames from each iteration to the list.
I'd also think a loop would be more effective.
I'm a little confused though, Limit 5 indicates you expect 5 results from your query but you pull one from the assoc array.
If your query returns 5 rows(which you can check by running it in phpMyAdmin or whatever SQL control panel you have), then you don't need to keep running it and it shouldn't return duplicates anyway. You just run :
while($row = mysql_fetch_assoc($query)){
$list[]=$row;
}
Then you can use the list to limit further iterations AND to store all of the users you've acquired.
Also, you should note that the mysql extension is deprecated, you want mySQLi or PDO_MySQL for security.