Cannot get all data from php query - php

I want to make user notification for all user related updates. So as below I tried to make some sql query.
My 1st query get all follower id from follow table
2nd query get all pages id that user liked from likes_user table
Based on above id's 3rd query try to find out new update on update table
Here my query cannot get my all follower ids to make 3rd query.
Here give my detail work:
$_GET['uid'] = $session->id;
// Find out all follower id by user
$rows=array();
$q = mysqli_query($dbh,"SELECT friend_two
FROM follow
WHERE friend_one='".$_GET['uid']."'")
or die(mysqli_error($dbh));
while ($row = mysqli_fetch_assoc($q)) {
$rows[]["friend_two"] = $row['friend_two'];
}
// Find out all pages id liked by user
$rows=array();
$d = mysqli_query($dbh,"SELECT userid
FROM likes_user
WHERE ip='".$_GET['uid']."'")
or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($d)) {
$rows[]["userid"] = $row['userid'];
}
foreach($rows as $row) {
$f2_id = $row['friend_two'];
$pageid = $row['userid'];
// echo all ids for test
// **** cannot echo all $f2_id ids for test ****
echo $f2_id;
echo '<br>';
echo $pageid;
// Get all update ids
$g = mysqli_query($dbh,"SELECT id
FROM update WHERE
`to_id`='".$myid."'
AND `from_id`='".$f2_id."'
OR `to_id`='".$pageid."'
OR `from_id`='".$pageid."'
AND `to_id`=''")
or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($g)) {
$ids[]= $rows['id'];
// ids will go for next query
}
}
UPDATE: Here now 3rd query got few ids twice or more
Here problem on foreach .. replaced by
$q = mysqli_query($dbh,"SELECT friend_two FROM follow WHERE friend_one='".$_GET['uid']."'") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_assoc($q)) {
$f2_ids[] = $row['friend_two'];
}
$d = mysqli_query($dbh,"SELECT userid FROM likes_user WHERE ip='".$_GET['uid']."'") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_assoc($d)) {
$pg_ids[] = $row['userid'];
}
foreach($f2_ids as $indx => $value) {
//For test echo
echo $f2_ids[$indx].$pg_ids[$indx];
$g = mysqli_query($dbh,"SELECT id
FROM update WHERE
`to_id`='".$myid."'
AND `from_id`='".$f2_ids[$indx]."'
OR `to_id`='".$pg_ids[$indx]."'
OR `from_id`='".$pg_ids[$indx]."'
AND `to_id`=''")
or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($g)) {
$ids[]= $rows['id'];
}
}
foreach ((array) $ids as $id ){
echo $id;
}
}

Basically the $rows array that you have built does not look like you think it does!
To see what you have actually produced in the $rows array add a print_r() just before you try and process the $rows array and your problem should become obvious.
Like this
print_r($rows);
foreach($rows as $row) {
$f2_id = $row['friend_two'];
$pageid = $row['userid'];
How you solve this I am not sure as I dont know your database!
Also there are many other issues in this script, too many to mention.

Related

My for loop only allows one post to be displayed, over and over again

/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html

Foreach inside while

I have extracted all the domains I have from my database, I would like to draw all comments to the domains that are added in the database.
$results = $mysqli->query("SELECT domain_name, id_view FROM domain GROUP BY domain_name ORDER BY domain_name");
$comment = $mysqli->query("SELECT domain_name, id_view, comment FROM domain_comment");
$id_array = array();
while($row = mysqli_fetch_array($comment)) {
$id_array[] = $row['comment'];
$id_array[] = $row['id_view'];
}
while($row = mysqli_fetch_array($results))
{
$section = $row['id_view'];
foreach ($id_array as $sectionname) {
if ($sectionname == $section) {
echo $sectionname;
}
}
}
The data it receives is VIEW ID and not Comment, what am I doing wrong?
EDIT:
When he gives the Select query:
$comment = $mysqli->query("SELECT comment FROM domain_comment INNER JOIN domain ON domain_comment.id_view = domain.id_view");
while($row = mysqli_fetch_array($results))
{
while($row2 = mysqli_fetch_array($comment))
{
echo $row2['comment'];
}
}
It prints all the comments at the first domain.
The result I have:
The result he wants:
this part
$id_array[] = $row['comment'];
$id_array[] = $row['id_view'];
What are you trying to do?
inserting comment in the id array? in this case the id_array will have one comment and one id, i dont think this is what you want. and second loop definitely us $row2 it is safer and wont mix up. inner join the quickest way.you are already there i think.
try removing the line
$id_array[] = $row['comment'];
and check.
for innerjoin you can try to innerjoin on two keys i.e also domain.
$comment = $mysqli->query("SELECT comment FROM domain_comment INNER JOIN domain ON domain_comment.id_view = domain.id_view and domain_comment.domain_name = domain.domain_name");
while($row = mysqli_fetch_array($comment)) {
$id_array[$row['id_view']]['comments'][] = $row['comment'];
}
while($row = mysqli_fetch_array($results))
{
$id_array[$row['id_view']]['data'] = $row;
}
You can try this solution after just dump $id_array in this case it would be much more easier to handle data.

Fetch multiple records from php/mysql

I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array

php mysql fetch array and loop rows

i have a database table 'movies'. in this table there are 25 columns of information per row. Ie, movie title, poster, cast, synopsis etc.
At the moment i am fetching the information like this
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$moviedetails['title']=$row['title'];
$moviedetails['poster']=$row['poster'];
}
}
else {
echo 'NO RESULTS';
}
because i have 25 columns its long work writing out each variable. is there a way to fetch the information and i can then call to it by using
$moviedetails['column name']
ie
im new to php and mysql so any help appreciated.
thanks
lee
$moviedetails['title']
fetches the information from the 'title' column.
while($row = $result->fetch_assoc()) {
foreach ($row as $key => $value){
$moviedetails[$key]=$value;
}
}
This input the same key and the same value into new array
Is that what you're looking for?
$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
$result = $con->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($moviedetails = $result->fetch_assoc()) {
//just use moviedetails for what you need
}
} else {
echo 'NO RESULTS';
}
Try this:
while($moviedetails[]=$result->fetch_assoc()){}
then you loop by it like this:
foreach($moviedetails as $num->row)
{
echo $row['title'];
}

Returning each unique user from a MySQL table and also the count of the number of rows for each user

I am using the following MySQL query to generate a table for users in a database. The query is designed to just return one row for each user, even though there are multiple rows for each user. This works fine, however I also need to calculate the number of unique entries for each user, to enter into the table where it states HERE. Do I need to use another query to return the count for all entries, and if so how do I integrate this with the code I already have?
$query="SELECT from_user, COUNT(*) AS num FROM tracks GROUP BY from_user ORDER BY COUNT(*) DESC";
$result=mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$user = $row['from_user'];
echo "<tr>";
echo "<td>".$user."</td>";
echo "<td>uploads (**HERE**)</td>";
echo "<td>favourites (count)</td>";
echo "</tr>";
}
?>
</table>
Because you've already created the custom field 'num', you can use that to get the count!
Add the following line after user = ...
$count = $row['num'];
Then you can
echo "<td>uploads ($count)</td>";
It miss your table stucture to know your field name, but, if i well understand your question you can use count + distinct in mysql.
You can check this answer too.
SELECT DISTINCT(from_user) AS user,
COUNT(from_user) AS num
FROM tracks
GROUP BY from_user
ORDER BY num DESC";
For the second problem you can doing a second query, or do a join tracks .
I think, in your case it's easier to you to do se second query inside the loop to get all detail from 'user' result.
$query1="SELECT DISTINCT(from_user), COUNT(*) AS num
FROM tracks
GROUP BY from_user
ORDER BY COUNT(*) DESC";
$query2="SELECT * FROM tracks";
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
$user_array = array();
while ($row = mysql_fetch_array($result1)) {
$user = $row['from_user'];
$num = $row['num'];
$uploads_array = array();
while ($sub_row = mysql_fetch_array($result2)) {
if( $sub_row['from_user'] == $user ) {
//for example only due to the unknown structure of your table
$uploads_array[] = array(
"file_name" => $sub_row['file_name'],
"file_url" => $sub_row['file_url']
);
}
}
$user_array[] = array(
"name" => $user,
"num_entry" => $num,
"actions" => $uploads_array
);
}
// now the table with all data is stuctured and you can parse it
foreach($user_array as $result) {
$upload_html_link_arr = array();
$user = $result['name'];
$num_entry = $result['num_entry'];
$all_actions_from_user_array = $result['actions'];
foreach($all_actions_from_user_array as $upload) {
$upload_html_link_arr[] = sprintf('%s', $upload["file_url"],$upload["file_name"]);
}
$upload_html_link = implode(', ',$upload_html_link_arr);
$full_row = sprintf("<tr><td>%s</td><td>uploads : %s</td><td>favourites (%d)</td></tr>", $user, $upload_html_link, $num_entry);
// now just echo the full row or store it to a table for the final echo.
echo $full_row;
}
I hope this help, mike

Categories