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
}
Related
Ok so I have two Tables
Applicant list - this shows all applicants
User Table
Now I'm Providing news_id by Post method and I want to list details of all users(email,mobile,username) where the value for user_authToken and user_authtoken is same. Can Someone help me out with this logic using PHP.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile,
FROM appliers_list,user
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();
First of all, your naming is very inconsistent, it's hard to read and understand.
Second, please use prepare statement, otherwise you open your system to SQL injection.
$news_id = $_POST['job_id'];
$stmt = $con->prepare("SELECT email, mobile, user_name
FROM users
WHERE user_authtoken in (select user_authToken from appliers_list where news_id = ?)");
$stmt->bind_param("i", $news_id);
$stmt->execute();
$resultSet = $stmt->get_result();
while($row = $resultSet->fetch_assoc()) {
// data manipulation here
}
you can use left join to get record from both table :
$job_id = !empty($_POST['job_id']) ? intval($_POST['job_id']) : 0;
$resultSet = $con->query("SELECT appliers_list.*,users.email
FROM appliers_list
left join users on appliers_list.user_authToken = users.user_authToken
WHERE news.news_id = '$job_id'
ORDER BY news.id DESC
");
$rows = $resultSet->fetch_assoc();
You didn't specify a relationship between the user and appliers_list tables, so you're getting all rows in user. You also have an extra comma at the end of the SELECT list.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile
FROM appliers_list
JOIN user ON appliers_list.user_authToken = user.user_authToken
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();
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 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.
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 am getting a bunch of id's from the database - for each id, I want to do a mysql query to get the relating row in another table. This works fine although I also need to get similiar data for the logged in user. With the mysql query I am using - it duplicates the logged in user data according to how many id's it originally pulled. This makes sense although isnt what I want. I dont want duplicate data for the logged in user and I need the data to come from one query so I can order things with the timestamp.
<?php
mysql_select_db($database_runner, $runner);
$query_friends_status = "SELECT DISTINCT rel2 FROM friends WHERE rel1 = '" . $_SESSION ['logged_in_user'] . "'";
$friends_status = mysql_query($query_friends_status, $runner) or die(mysql_error());
$totalRows_friends_status = mysql_num_rows($friends_status);
while($row_friends_status = mysql_fetch_assoc($friends_status))
{
$friends_id = $row_friends_status['rel2'];
mysql_select_db($database_runner, $runner);
$query_activity = "SELECT * FROM activity WHERE user_id = '$friends_id' OR user_id = '" . $_SESSION['logged_in_user'] . "' ORDER BY `timestamp` DESC LIMIT 15";
$activity = mysql_query($query_activity, $runner) or die(mysql_error());
$totalRows_activity = mysql_num_rows($activity);
echo "stuff here";
}
?>
You can try this single query and see if it does what you need
$userID = $_SESSION['logged_in_user'];
"SELECT * FROM activity WHERE user_id IN (
SELECT DISTINCT rel2 FROM friends
WHERE rel1 = '$userID')
OR user_id = '$userID' ORDER BY `timestamp` DESC LIMIT 15";
You probably want something like this:
$user = $_SESSION['logged_in_user'];
$query_friends_status = "SELECT * FROM friends, activity WHERE activity.user_id = friends.rel2 AND rel1 = '$user' ORDER BY `timestamp` DESC LIMIT 15";
You can replace * with the fields you want but remember to put the table name before the field name like:
table_name.field_name
I hope this helps.
<?php
require_once "connect.php";
$connect = #new mysqli($host, $db_user, $db_password, $db_name);
$result = $connect->query("SELECT p.name, p.user, p.pass, p.pass_date_change,p.email, p.pass_date_email, d.domain_name, d.domain_price, d.domain_end, d.serwer, d.staff, d.positioning, d.media FROM domains d LEFT JOIN persons p
ON d.id_person = p.id AND d.next_staff_id_person != p.id");
if($result->num_rows > 1)
{
while($row = $result->fetch_assoc())
{
echo '<td class="box_small_admin" data-column="Imię i nazwisko"><div class="box_admin">'.$row["name"].'</div></td>';
echo '<td class="box_small_admin" data-column="Domena"><div class="box_admin">'.$row["domain_name"].'</div></td>';
}}
?>