Pass id between multiple queries in mysql? - php

I'm displaying customer forms id row by row and that works fine. But I would also like to display the score of each form in the same row. How can i get the form_id from query 1 to work in query 2? I need the form_id to be able to sum the score for query 2.
I have added $WHAT-TO-PUT-HERE? in the code where I'm lost, what would be the correct way to solve this?
// get user_if from url
$user_id = $_GET['userid'];
// fetch form_id for form list
$query1 = "SELECT DISTINCT form_id FROM answers where user_id=$user_id";
$result1 = #mysqli_query($con, $query1);
while($row1 = mysqli_fetch_array($result1)) {
// get score based on form_id from query 1?
$result2 = mysqli_query($con,"SELECT SUM(answer_value) AS Poang FROM answers q JOIN questions a ON q.question_id = a.question_id WHERE form_id = $WHAT-TO-PUT-HERE? AND question_sort = 1");
while($row2 = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td><a href='currentform?formid=" . $row1['form_id'] . "</a></td>";
echo "<td>".$row2['Poang'] ."</td></tr></table>";
}}

You need to use row1['form_id'] instead of $WHAT-TO-PUT-HERE.
In your
while($row1 = mysqli_fetch_array($result1)) {
The current result of your Select will be saved into $row1 as array, so you can access these values like a normal array.
If you give the select an alias the key of the array will have the name of the alias, else it will have the column name from the database.
You also need to concate the strings like this
$result2 = mysqli_query($con,"SELECT SUM(answer_value) AS Poang FROM answers q JOIN questions a ON q.question_id = a.question_id WHERE form_id = ".$row1['form_id']." AND question_sort = 1");

replace $WHAT-TO-PUT-HERE by $row1['form_id']
like
$result2 = mysqli_query($con, "SELECT SUM(answer_value) AS Poang FROM answers q
JOIN questions a ON
q.question_id = a.question_id
WHERE form_id = '".$row1['form_id']."' AND question_sort = 1");

This can be done with just one query:
SELECT
a.form_id,
SUM(answer_value) AS Poang
FROM
answers a
JOIN
questions q
WHERE
a.user_id = $user_id
AND
q.question_sort = 1
GROUP BY
a.form_id

Your $row1 variable contains an associative array including your database row.
There for use $row1['form_id'] or $row1[0] (column 0) to access the value

Related

Get values from different tables inside a while loop statement using php

I'm having an issue of retrieving values from two different tables. Here's the code so far:
$result = mysqli_query($conn,"SELECT * FROM articles");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$uid=$row['_uid'];
$result2 = mysqli_query($conn, "SELECT _username FROM users WHERE _id = '$uid' ";
$num2 = mysqli_num_rows($result2);
while ($row2 = mysqli_fetch_array($result2)) {
$username = $row2['_username'];
}
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
I've been reading that I should preform this while inside a while with multiple query, also found on w3 that you could directly assign a value to a variable directly using:
"SELECT _username INTO $username FROM users WHERE _id = '$uid' LIMIT 1";
But this works in SQL inside myadmin, in a php I can't find how to cast it.
I have also replace the fetch_row for fetch_assoc and still nothing, im struggling for two days already with this.
you could select al the value using a single query
SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id
..
$result = mysqli_query($conn,
"SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
$echo $divtext;

1 JSON object, 2 queries, 2 tables

Here is my simple query:
$sql = "SELECT * FROM donations Order By userid";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()){
$json[] = $row;
}
$data['data'] = $json;
I use it to display all data from the 'donations' in a table. Fields are: userid,date,amount.
In that same table, I'd like to add firstname and lastname of corresponding userid which are stored in mymembers table. The condition should be WHERE donations.userid = mymembers.id.
I need help adding that condition for every row resulting from the $sql query.
Use join and change query to
SELECT * FROM donations
INNER JOIN mymembers on (donations.userid = mymembers.id)
Order By donations.userid

Looping through a mysqli result

I'm trying to display a list of status updates from artists that a logged in user is following.
So far I have this:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
}
But i'm not sure how to loop through and display the returned status updates?
This isn't a strong point of mine, so any pointers would be greatly appreciated!
What prevented you from doing similar to what you'd already done for the first query? Something like follows:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
while($status_result_row = mysqli_fetch_assoc($status_result)) {
echo $status_result_row['mycol']; // This is where you know better than us
}
}
Or if those two tables artist_likes and status_updates have artist_id in common then you could just use one query with a join. (But don't know if you are asking for that).
Just for avoiding multiple query, you can use one query like this:
SELECT l.*, s.*
from artist_likes l, status_updates s
WHERE
l.artist_id = s.artist_id and
l.user_id = '1'
or
SELECT l.*, s.*
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'

Counting rows from table B using id from table A php mysql

I must be exhausted because I know I have done this before but I can't remember how I did or or find that snippet. I have two tables "questions" and "answers". The primary "id" from "questions" is also "id" in "answers". I'm trying to count "answers" based on "id" from that table.
<?php
$query = "SELECT * FROM questions ORDER BY id DESC";
$result = mysql_query("$query");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$date = $row['date'];
$question = $row['question'];
$time = date("U");
$likes = $row['likes'];
$query2 = "SELECT * FROM answers WHERE id = $id";
$num_rows = mysql_num_rows($query2);
print("<span style='font-size: .7em';><a href='qplusone.php?id=$id'>+1</a>
- Likes:$likes</span> $question - <a href='answer.php?id=$id&pp=$time'>answer it</a>
or <a href='answers.php?id=$id&pp=$time'>read $num_rows answers</a>
<span style='font-size: .7em';>($date)</span><br />");
}
?>
Well, first of all you never execute $query2. But even better than returning all the rows and counting them, simply return the number of rows counted by mysql.
$query2 = "SELECT count(*) FROM answers WHERE id = $id";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
$count = $row['count(*)']; // $count holds the number of matching records.
To get the count of answers per question you can do this
select q.id,
count(*) as answers
from answer a
left join questions q on q.id = a.id
group by q.id

Get a number in one table and print the number's referenced name in another table

I need to create a friend system, but my loop always skips the first match and sometimes it prints copies of the same name
$result = mysql_query("SELECT * FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.username_id");
while($row = mysql_fetch_array($result))
{
if ($row['username_id'] == 1)//the 1 will be a variable, username_id is in friends
$count = $row['friendname'];//friendname is in friends
if ($row['id'] == $count)//id is in users
echo $row['username'];//username is in users
}
Can someone see what my problem is ?
2 things:
if ($row['username_id'] == 1)
you should put that in your sql:
$result = mysql_query("SELECT username, friendname FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.username_id where tbusers.id = ".$yourVariable);
In your query, user and friend are linked, and can only be equal.
Now, this:
$count = $row['friendname'];//friendname is in friends
if ($row['id'] == $count)//id is in users
is equal to
if ( $row['id'] == $row['friendname'] )
this sounds plain wrong. You compare a numerical id with a name. Moreover, your sql query already retrives all friends from users. In the version I showed here, it retrieves only friends of the user you are interested in.
finally, you print (echo) the name of the user, not of the friend. In my opinion the following code will do what you want:
$result = mysql_query("SELECT friendname FROM tbfriends WHERE username_id = ".$yourUserVariable);
while($row = mysql_fetch_array($result))
{
echo $row['friendname']; // or better: echo $row['friendname'], '<br>';
}
edit: after comment...
so if ( $row['id'] == $row['friendname'] ) means the user is his own friend. can that happen?
this code shall print what you want, friend names.
/*
$result = mysql_query("
SELECT username as friend_name,
friendname as friend_id,
username_id as user_id
FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.username_id
WHERE tbusers.id = ".$yourVariable);
*/
$result = mysql_query("
SELECT username as friend_name,
friendname as friend_id,
username_id as user_id
FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.friendname
WHERE tbfriends.username_id = ".$yourVariable);
while($row = mysql_fetch_array($result))
{
echo $row['friend_name']; // or better: echo $row['friend_name'], '<br>';
}

Categories