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.
Related
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";
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.
I have tried to make this work but it adds up all the rows not just 5 like I am trying to in the query
$username = $_SESSION['username'];
$stmt = $db->prepare("
select sum(correct) from exams where
username = :username ORDER BY :testID DESC LIMIT :5");
Due to the problems with not being able to callthe last 5 rows I am unable to test the ordering string.
I have read questions similar to this on stackoverflow but still cant seem to get it right. maybe there are other problems with my code but I can always add that if requsted :)
the full code
<?php
require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
$username = $_SESSION['username'];
$last5rate = $db->prepare("select sum(correct) from exams where username = :username ORDER BY :testID DESC LIMIT 5");
$last5rate->execute(array(':username' => $username));
for($i=0; $rows = $last5rate->fetch(); $i++){
//Edit this row
$last5 = $rows['sum(correct)'];
$last5final = $last5 / 10;
}
echo $last5final;
?>
if you are not binding limit then change your query
select sum(correct) from exams where username = :username ORDER BY :testID DESC LIMIT :5
$last5rate->execute(array(':username' => "$username"));
for($i=0; $rows = $last5rate->fetch(); $i++){
$last5 = $rows['sum(correct)'];
}
to
select sum(correct) as correct from exams where username = :username ORDER BY :testID DESC LIMIT 5
$last5rate->execute(array(':username' => $username,':testID' => $testID));
while ($row = $last5rate->fetch(PDO::FETCH_ASSOC)) {
echo $row['correct']; // or use it as you want
}
I need to get the last 5 results, that's why I order them by Date DESC but I need to display the results from older to newer. How can I do this?
$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5");
while($row = mysql_fetch_array( $data ))
{
print "<img class='badge' title='" . $row['Site'] . "' src='http://getfavicon.appspot.com/http://" . $row['Site'] . "?defaulticon=1pxgif' />";
}
$results = array();
while($row = mysql_fetch_array($data)) {
$results[] = $row;
}
$results = array_reverse($results);
foreach ($results as $row) {
echo $row['Site']; // etc
}
Manual link: http://php.net/function.array-reverse.php
$data = mysql_query("
SELECT * FROM (
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5)
t ORDER BY Date
");
Also:
Please use mysql_real_escape_string (http://at2.php.net/manual/en/function.mysql-real-escape-string.php) for the variable $user. Depending on where you get that from it could be a security leak through sql injection.
You can actually order a second time in the same query.
I assume that you have an auto incrementad id (I'll call it 'EntryId' in this example) and then you hopefully should get what you need?
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC, EntryId ASC LIMIT 5
I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.