Foreach inside while - php

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.

Related

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

Cannot get all data from php query

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.

Group the output of PDO query

I have a PDO query in which I am doing inner join on two tables and extracting the columns required by me.
Now these two columns are - status, script.
Status can be - passed, failed, incomplete and
script - scripts/testSuite/layer3Features/ManualStaticRouting/manualStaticRoutes.tcl , scripts/testSuite/hostAgentFeatures/dhcp/DHCP IPv6/RelayBBasicFunc/ipv6DhcpRelayEnableDhcpv6RelayGlobally.tcl
the output of --
while($row = $getFamily->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $key)
{
print_r($row);
}
is -
http://pastebin.com/WqcibEyp
the full query is -
$testType = 'TCL';
$getFamily = $conn->prepare('SELECT testcases2.script, results3.status FROM testcases2 INNER JOIN results3 ON testcases2.testcaseID = results3.testcaseID
WHERE testType = :testType');
$getFamily->execute(array(':testType' => $testType));
while($row = $getFamily->fetch(PDO::FETCH_ASSOC))
{
foreach($row as $key)
{
print_r($row);
$family[] = $key;
}
}
Now here what I want to do is, read the second location of each script (considering scripts at 0) and group all the values which have same stuff in the third location, with status together.
How this can be done.
Please guide.
You need to group your script and status like this,
while($row = $getFamily->fetch(PDO::FETCH_ASSOC))
{
$status[] = $row['status'];
$scripts[] = $row['script'];
}
print_r($scripts);
print_r($status);

PHP MySQLi while loop logic

I have switched over to the mysqli_ extension for PHP, and I have ran into a bit of an issue.
I have 2 databases. 1 database I am only allowed read privileges, the other I have all privileges. My end goal is to read what I need from database 1, and put it in a table in database 2.
I use a join on database 1 to get all the information I need. I then loop through the results with a while loop. I have a unique id (domainid) in both databases. Where I am encountering the issue is inside the while loop once I have the domainid from the read-only database, I need to check if it exists inside my all-privileges database. I am just unsure of how to accomplish this?
Here is the code:
require 'db/connect.php';
require 'db/connect2.php';
if($result = $db->query("
SELECT tblhosting.domain as domain, tblhosting.id as domainid, tblclients.email as email
FROM tblhosting
LEFT JOIN tblclients ON
tblclients.id = tblhosting.userid
")){
if ($count = $result->num_rows) {
while($row = $result->fetch_object()){
$domainid = $row->domainid;
$domain = $row->domain;
$email = $row->email;
$result2 = $db2->prepare("SELECT domainid FROM information WHERE domainid = ?");
$result2->bind_param('i', $row->domainid);
$result2->execute();
$result2->bind_result($domainid2);
//$result2->fetch();
while($row2 = $result2->fetch_object()){
$domainid2 = $row2->domainid;
if ($domainid == $domainid2) {
echo "Information exists in both Databases", '<br>';
}
else{
echo "New Information Added to Database 2", '<br>';
}
}
}
}
}
This is what I have tried, but was unsuccessful.
EDIT
Second attempt putting the results into an array then looping through them. The array is correct when I print them out. The issue is with the 2nd execute(); command.
$result = $db->query("
SELECT tblhosting.domain as domain, tblhosting.id as domainid, tblclients.email as email
FROM tblhosting
LEFT JOIN tblclients ON
tblclients.id = tblhosting.userid
");
$domainid_arr = array();
while($row = $result->fetch_object()){
$domainid_arr[] = array(
'domainid' => $row->domainid,
'domain' => $row->domain
);
}
foreach ($domainid_arr as $d) {
echo $d['domainid'], $d['domain'], '<br>';
$result2 = $db2->prepare("SELECT domainid FROM information WHERE domainid = ?");
$result2->bind_param('i', $d['domainid']);
$result2->execute();
$result2->bind_result($domainid2);
while($row2 = $result2->fetch_object()){
$domainid2 = $row2->domainid;
if ($d['domainid'] == $domainid2) {
echo "Information exists in both Databases", '<br>';
}
else{
echo "New Information Added to Database 2", '<br>';
}
}
}

display all cells in a mysql column

I need all values from a table column put into an assoc array. I am having trouble finding the answer.
the table is only six rows deep.
example:
|--id--|--name--|--A--|--B--|--C--|
|--01--|--xl33--| 1.30| 2.45| 4.40|
i would like to get an assoc array for column B
name[xl33]=2.45
name[xl34]=....and so on
The trick is that the form will tell the script which column to fetch. A,B,C,D,E,F OR G
I know i could re-format the table and accomplish what i want but I need it structured the way i have it.( i have left out some columns for simplicity)
function return_column($letter){
$result = mysql_query("SELECT name, $letter FROM example") or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) {
$return[$row['name']] = $row[$letter];
}
return $return;
}
$results = return_column('A');
print_r($results);
Something like :
$col = 'B';
$name = array();
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$name[$row['name']] = $row[$col];
}
This creates an array $name and uses the name column as the key and the $col column for the value ...
You are looking for the mysql_fetch_assoc() function.
Example/
$query = $this->query($YOUR_QUERY);
$returnMap = array();
while ($row = mysql_fetch_assoc($query)) { array_push($returnMap, $row); }
Use mysql_fetch_assoc — Fetch a result row as an associative array.
while ($row = mysql_fetch_assoc($result)) {
$data[$row["column1"]]= $row["column2"];
..............
.........
}

Categories