PDO Query from Results of Another PDO Query - php

I have this code which hits a view in mysql called vAlbums this returns a JSON array of the query results
function getAlbumPics($arno){
$aSql = "SELECT albu_ablumid, albu_name_en, albu_name_de, albu_name_fr, albu_name_nl, albu_name_es, albu_name_it, albu_photourl FROM vAlbums WHERE site_arno=:arno";
try {
$db = getConnection();
$aStmt = $db->prepare($aSql);
$aStmt->bindParam(":arno",$arno);
$aStmt->execute();
$albums = $aStmt->fetchAll(PDO::FETCH_OBJ);
$arrAID = $aStmt->fetchColumn(2);
$db = null;
echo '{"albums": ' . json_encode($albums) . '}';
} catch(PDOException $e) {
echo '{"error":[{"text":"'. $e->getMessage() .'"}],';
echo '"SQL": ' . json_encode($aSql) .'}';
}
}
I need to do a sub query to place an array of photos in each album in the array like thus
{
"albums": [
{
"albu_ablumid": "1",
"photos": [
{
"photourl": "photo1"
},
{
"photourl": "photo2"
},
{
"photourl": "photo3"
}
]
},
{
"albu_ablumid": "2",
"photos": [
{
"photourl": "photo1"
},
{
"photourl": "photo2"
},
{
"photourl": "photo3"
}
]
}
]
}
Can someone show how to achieve this the MySQL query for photos is:
SELECT * FROM photos WHERE album_id = x
Thanks

Why not just do a join and group concat in a single query?
SELECT
albu_ablumid,
albu_name_en,
albu_name_de,
albu_name_fr,
albu_name_nl,
albu_name_es,
albu_name_it,
albu_photourl,
group_concat(photourl) // Guessing at column Name
FROM
vAlbums a
join photos b
on a.albu_ablumid=b.album_id
WHERE
site_arno=:arno
group by
albu_ablumid,
albu_name_en,
albu_name_de,
albu_name_fr,
albu_name_nl,
albu_name_es,
albu_name_it,
albu_photourl
This will return all the photos in a comma separated string in each row that matches the album. You can then easily split it up as needed in your code. It saves a lot of connections and multiple queries being run.
Here is a little example from my play db to show how it functions:
mysql> select * from table1;
+---------+------+------+-------------+
| autonum | ID | name | metavalue |
+---------+------+------+-------------+
| 1 | 1 | Rose | Drinker |
| 2 | 1 | Rose | Nice Person |
| 3 | 1 | Rose | Runner |
| 4 | 2 | Gary | Player |
| 5 | 2 | Gary | Funny |
| 6 | 2 | Gary | NULL |
| 7 | 2 | Gary | Smelly |
+---------+------+------+-------------+
7 rows in set (0.00 sec)
mysql> select ID, group_concat(metavalue) from table1 group by ID;
+------+----------------------------+
| ID | group_concat(metavalue) |
+------+----------------------------+
| 1 | Drinker,Nice Person,Runner |
| 2 | Player,Funny,Smelly |
+------+----------------------------+
2 rows in set (0.00 sec)
and with some simple code to take care of the NULLs if you need to for some reason:
mysql> select ID, group_concat(coalesce(metavalue,'Empty')) from table1 group by ID;
+------+-------------------------------------------+
| ID | group_concat(coalesce(metavalue,'Empty')) |
+------+-------------------------------------------+
| 1 | Drinker,Nice Person,Runner |
| 2 | Player,Funny,Empty,Smelly |
+------+-------------------------------------------+
2 rows in set (0.00 sec)

Related

The Best Way For Two Sequential SELECT Table

Hello I have two tables to send consecutive queries.
For example, table A yields 1,2,3 ..
Then, look for data in table B of query 1,2,3 ..
tableA
_____________________
| uid | rate |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
tableB
_________________________
| rate | text |
| 1 | ONE |
| 2 | TWO |
| 3 | THREE |
| 4 | FOUR |
===
<?php
$sql = $con->query("SELECT * FROM tableA WHERE uid=1");
$user = $sql->fetch_array();
$ratings = $user['rate']; //1,2,3
$sql2 = $con->query("SELECT * FROM tableB WHERE rate IN('".$ratings."')");
$text = $sql2->fetch_array();
$results = $text['text']; //ONE, TWO, THREE
?>
How best to do that?
You can use this query:
Select tableA.*,tableB.*
from tableA
join tableB on tableA.rate=tableB.rate
where tableA.uid=1

for each value of one table get and display count number of coresponding values from another table

unfortunately i have to do this in mysql / php . I looked for three days, and there is like 10.000 explantions of this but NONE (and I repeat NONE) works for me. I tried it all. I have to ask, sorry.
I have two tables - articles and control.
table "articles"
------------------
art_id | name |
------------------
1 | aaa |
2 | bbb |
3 | ccc |
4 | ddd |
table "control"
--------------------------------------------
con_id | art_id | data |
--------------------------------------------
1 | 1 | something-a |
2 | 2 | something-b |
3 | 1 | something-a |
4 | 2 | something-c |
5 | 3 | something-f |
art_id exists in both tables. Now what i wanted - for query:
"select * from articles order by art_id ASC" displayed in a table
to have also one cell displaying the count for each of art_id's from table CONTROL...
and so i tried join, left join, inner join - i get errors ... I also tried for each get only one result (for example 2 for everything)... this is semi-right but it displays the array of correct results and it's not even with join!!! :
$query = "SELECT art_id, count(*) as counting
FROM control GROUP BY art_id ORDER BY con_id ASC";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)) {
echo $row['counting'];
}
this displays 221 -
-------------------------------------------------
art_id | name | count (this one from control) |
-------------------------------------------------
1 | aaa | 221 |
2 | bbb | 221 |
3 | ccc | 221 |
and it should be:
for art_id(value1)=2,
for art_id(2)=2,
for art_id(3)=1
it should be simple - like a count of values from CONTROL table displayed in query regarding the "articles" table...
The result query on page for table articles should be:
"select * from articles order by art_id ASC"
-------------------------------------------------
art_id | name | count (this one from control) |
-------------------------------------------------
1 | aaa | 2 |
2 | bbb | 2 |
3 | ccc | 1 |
So maybe i should go with JOIN or with join plus for each... Tried tha too, but then i'm not sure what is the proper thing to echo... all-in-all i'm completely lost here. Please help. Thank you.
So imagine this in two steps:
Get the counts per art_id from the control table
Using your articles table, pick up the counts from step 1
That will give you a query that looks like this:
SELECT a.art_id, a.name, b.control_count
FROM articles a
INNER JOIN
(
SELECT art_id, COUNT(*) AS control_count
FROM control
GROUP BY art_id
) b
ON a.art_id = b.art_id;
Which will give you the results you're looking for.
However, instead of using a subquery, you can do it all in one shot:
SELECT a.art_id, a.name, COUNT(b.art_id) AS control_count
FROM articles a
INNER JOIN control b
ON a.art_id = b.art_id
GROUP BY a.art_id, a.name;
SQL Fiddle demo
SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id DESC;
If I understood your question right, this query should do the trick.
Edit: Created the tables you have described, and it works.
SELECT * FROM articles;
+--------+------+
| art_id | name |
+--------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
+--------+------+
4 rows in set (0.00 sec)
SELECT * FROM control;
+--------+--------+------+
| con_id | art_id | data |
+--------+--------+------+
| 1 | 1 | NULL |
| 2 | 2 | NULL |
| 3 | 1 | NULL |
| 4 | 2 | NULL |
| 5 | 3 | NULL |
+--------+--------+------+
5 rows in set (0.00 sec)
SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC;
+--------+------+----------------+
| art_id | name | count_from_con |
+--------+------+----------------+
| 1 | aaa | 2 |
| 2 | bbb | 2 |
| 3 | ccc | 1 |
| 4 | ddd | 0 |
+--------+------+----------------+
You haven't quite explained what you want to accomplish with the print out but here is an example in PHP: (Use PDO instead of mysql_)
$pdo = new PDO(); // Make your connection here
$stm = $pdo->query('SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC');
while( $row = $stm->fetch(PDO::FETCH_ASSOC) )
{
echo "Article with id: ".$row['art_id']. " has " .$row['count_from_con'].' connected rows in control.';
}
Alternatively with the mysql_ extension:
$result = mysql_query('SELECT *, (SELECT COUNT(control.con_id) FROM control WHERE control.art_id = articles.art_id) AS count_from_con FROM articles ORDER BY art_id ASC');
while( $row = mysql_fetch_assoc($result) )
{
echo "Article with id: ".$row['art_id']. " has " .$row['count_from_con'].' connected rows in control.';
}
This should be enough examples to help you accomplish what you need.

How to map matched records returned from a join instead of separate rows

Scenario :
I have two tables, structured as following.
Table 1 : images
+--------+------------+
| img_id | img_name |
+--------+------------+
| 1 | image1.jpg |
| 2 | image2.jpg |
| 3 | image3.jpg |
+--------+------------+
Table 2 : image_tags
+---------+--------+----------+--------+--------+
| cord_id | img_id | tag_text | xcord | ycord |
+---------+--------+----------+--------+--------+
| 1 | 1 | Tag1 | 28.1 | 30.4 |
| 2 | 1 | Test Tag | 23.4 | 4.5 |
+---------+--------+----------+--------+--------+
Now i want the images along with their tags which is quite simple using the left join
SELECT img_id, img_name,tag_text, xcord,ycord FROM images t1 LEFT JOIN image_tags t2 ON t1.id=t2.id
This query results in the following data set
+--------+------------+----------+--------+--------+
| img_id | img_name | tag_text | xcord | ycord |
+--------+------------+----------+--------+--------+
| 1 | image1.jpg | Tag1 | 28.1 | 30.4 |
| 1 | image1.jpg | Test Tag | 23.4 | 4.5 |
| 2 | image2.jpg | NULL | NULL | NULL |
| 3 | image3.jpg | NULL | NULL | NULL |
+--------+------------+----------+--------+--------+
Problem :
Now using PHP (or MYSQL if possible), i want the results from the image_tags table to be concatenated with each row in form of an array.
So that when i loop through the records in Angular, i have images along with their tags in form of an array instead of two separate rows as you can see the first two rows in the result set.
Desired result example,
{
cord_id : 1,
img_id : 1,
tags : [{
tag_text : "Tag1",
xcord : 28.1,
ycord : 30.4,
},{
tag_text : "Test Tag",
xcord : 23.4,
ycord : 4.5,
}
]
}
I have studied about map() function in PHP but unable to achieve this.
Note: I am using Codeigniter, so if that has some relevant support to achieve this, that would also work for me.
Any help would be highly appreciated. Thanks
It is possible to do it with just one query.
Since you have a unique ID you can use it as an index for your array.
$images = array();
foreach($queryResults as $result){
if(!isset($images[$result['img_id']])){
$images[$result['img_id']] = array();
$images[$result['img_id']]['cord_id'] = $result['cord_id'];
$images[$result['img_id']]['img_id'] = $result['img_id'];
$images[$result['img_id']]['tags'] = array();
}
$tag = array(
'tag_text'=>$result['tag_text'],
'xcord'=> $result['xcord'],
'ycord'=> $result['ycord']
);
$images[$result['img_id']]['tags'][] = $tag;
}

Dump data in json format from two tables from MySQL

I have the following scenario.
I have 2 tables as follow
Table 1
ID, Name, Desc, Seo
Table 2
ID, Table1_ID, Relation_Table1_ID
in Table 1 I have all my data that I need as:
-----------------------------------
|ID | Name |Desc |Seo |
-----------------------------------
| 1 | Smith |Father |f |
| 2 | Jonh |Son |j |
| 3 | Margat |Mother |m |
| 4 | Bis3 |son |b1 |
| 5 | Bis2 |son |b2 |
| 6 | Bis1 |son |b3 |
| 7 | Lanos |Brother |l |
-----------------------------------
And then we have our table 2 as follow
-------------------------------------
|ID | Table1_ID |Relation_Table1_id|
--------------------------------------
| 1 | 1 | 4 |
| 2 | 1 | 5 |
| 3 | 3 | 6 |
| 4 | 3 | 2 |
| 5 | 7 | 0 |
--------------------------------------
So far I have my first table dump with jSON() as follow:
<?php
include ('config.php');
$dump1 = mysql_query("SELECT * FROM Table1 ") or die(mysql_error());
$getList = array();
while ($row = mysql_fetch_assoc($dump1)) {
$getList[] = $row;
}
print json_encode($getList); exit;
?>
That code will give me the following:
[
{
"ID":"1",
"Name":"Smith",
"Desc":"Father",
"Seo":"f"
},
{
"ID":"2",
"Name":"Jonh",
"Desc":"Son",
"Seo":"j"
},
{
"ID":"3",
"Name":"Margat",
"Desc":"Mother",
"Seo":"m"
}... ... ...
]
What I can't figure is how do I get the following
[
{
"ID":"1",
"Name":"Smith",
"Desc":"Father",
"Seo":"f",
"Relations":[
{
"ID":"4",
"Name":"Bis3",
"Desc":"Son",
"Seo":"b1"
}
]
},
{
"ID":"3",
"Name":"Margat",
"Desc":"Father",
"Seo":"f",
"Relations":[
{
"ID":"6",
"Name":"Bis2",
"Desc":"Son",
"Seo":"b2"
},
{
"ID":"2",
"Name":"Jonh",
"Desc":"Son",
"Seo":"j"
}
]
}... ... ...
]
In plain text it would be something like
ID 1 Smith
| |_ID 4 Bis3
|
|_ ID 3 Margat
|_ID 5 Bis2
|_ID 2 Jonh
I'm learning how to use json and I just got the first part as you can see, but my lack of knowledge of SQL and php wont let me get the what I really want, so please can any one help me achieve this scenario please.
Thank you.
you can loop through $getlist
then query
for($i = 0; $i <= count($getlist); $i++){
"SELECT * FROM Table2 WHERE Table1_ID = $getlist[$i]['ID']"
// get the result however way
$getlist[$i]['something'] = $result;
}
then take the result of that, inside the loop, and assign it to whichever key of getlist you want
for example
hopefully thats specific enough. let me know if you have problems with that.
You need to join across the two tables. First, join the second table with the first table. This can be done with the following code:
select * from table_2 as t2 join table_1 as t1 on t1.id = t2.rel_id;
which results in:
+------+-----------+--------+------+------+------+------+
| ID | table1_id | rel_id | ID | name | desc | seo |
+------+-----------+--------+------+------+------+------+
| 4 | 3 | 2 | 2 | John | Son | j |
| 1 | 1 | 4 | 4 | Bis3 | son | b1 |
| 2 | 1 | 5 | 5 | Bis2 | son | b2 |
| 3 | 3 | 6 | 6 | Bis1 | son | b3 |
+------+-----------+--------+------+------+------+------+
now we want to join the columns if the table1_id is the same. This can be done with the GROUP_CONCAT command:
select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on
t2.rel_id = t1.id group by t2.table1_id
which results in:
+-----------+-----------+----------+---------+--------+
| table1_id | name_rel | desc_rel | seo_rel | id_rel |
+-----------+-----------+----------+---------+--------+
| 1 | Bis2,Bis3 | son,son | b2,b1 | 5,4 |
| 3 | John,Bis1 | Son,son | j,b3 | 2,6 |
+-----------+-----------+----------+---------+--------+
The as command can be used to rename columns in the output. Try running the query without it and you should see column names like group_concat(t1.name) instead of name_rel.
Now we want to join this selection with table_1 where the table1_id is the same as the id in table1. This can be done with the following query:
select * from (
select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on
t2.rel_id = t1.id group by t2.table1_id
) as joined_table
join table_1 as t3 on t3.id = joined_table.table1_id;
which results in:
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| table1_id | name_rel | desc_rel | seo_rel | id_rel | ID | name | desc | seo |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| 1 | Bis2,Bis3 | son,son | b2,b1 | 5,4 | 1 | Smith | Father | f |
| 3 | John,Bis1 | Son,son | j,b3 | 2,6 | 3 | Margat | Mother | m |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
As you can see, you know have all the data. All that is left is to turn the *_rel columns into separate objects, which can be done in php. Go ahead and give that a shot. I've gotta go for now, but I'll edit this post later if you're still having trouble.
Alright, I'm back. My php is rusty, but something along these lines should work:
while ($row = mysql_fetch_assoc($dump1)) {
$name_rel = explode("," , $row["name_rel"]);
$desc_rel = explode("," , $row["desc_rel"]);
$seo_rel = explode("," , $row["seo_rel"]);
$id_rel = explode("," , $row["id_rel"]);
$relations = array();
for($i = 0; $i < count($id_rel); ++$i){
$temp = array("ID" => $id_rel[$i],
"Name" => $name_rel[$i],
"Desc" => $desc_rel[$i],
"Seo" => $seo_rel[$i],);
$relations[] = $temp ;
}
unset($row["id_rel"]);
unset($row["name_rel"]);
unset($row["desc_rel"]);
unset($row["seo_rel"]);
$row["Relations"] = $relations ;
$getList[] = $row;
}
print json_encode($getList); exit;
This will create each of the Relations objects and add them to the row. The unset commands should remove the keys we no longer care about (id_rel, name_rel, desc_rel, and seo_rel). We no longer care about them because their data should have been moved to the Relations object.

Json encode for nested data using PDO PHP

I have following tables:
+---------+-----------+ +----------+---------+-------------+
| item_id | item_name | | image_id | item_id | image_url |
+---------+-----------+ +----------+---------+-------------+
| 1 | phone | | 1 | 1 | http://url1 |
| 2 | computer | | 2 | 1 | http://url2 |
| 3 | keyboard | | 3 | 2 | http://url2 |
+---------+-----------+ +----------+---------+-------------+
What should I do in php using PDO and json encode to have the following ouput:
[
{
"item_id":"1",
"name":"phone",
"images": [
{
"image_id":"1",
"url":"http://url1"
},
{
"image_id":"2",
"url":"http://url2"
}
]
},
{
"item_id":"2",
"name":"computer",
"images": [
{
"image_id":"3",
"url":"http://url3"
}
]
},
{
"item_id":"3",
"name":"keyboard",
"images": []
}
]
Do I HAVE to use foreach inside foreach loop to get this result?
No, use a single loop, and a single SQL query.
Firstly the SQL query is
SELECT im.image_id, im.image_url, im.item_id
it.item_name
FROM images im
LEFT JOIN items it ON it.item_id=im.item_id
ORDER BY im.item_id, im.image_id
And your PHP (paraphrased) is:
$lastItemId = -1;
$aJson = array();
while ($row = GetNextRow()) { // Depends on your class
$itemID = $row['item_id'];
if (!isset($aJson[$itemID])) {
$aJson[$itemID] = array('item_id'=$itemID, 'item_name'=$row['item_name'], images=>array());
}
$aJson[$itemID]['images'] = array('image_id'=>$row['image_id'], 'image_url'=>$row['image_url']);
}
echo json_encode($aJson);
You should be able to tweak from there?

Categories