I'm am trying to query all the photo albums of a page and get each albums cover photo. Below is the code I have that returns an empty result.
I am able to make both the queries separately without the problem, so it is not a permissions issue.
$album_fql = "SELECT aid, owner, name, object_id, cover_pid, cover_object_id
FROM album
WHERE owner = 'xxxxxxx'";
$albumpic_fql = "SELECT pid, src_small
FROM photo
WHERE pid IN (SELECT cover_pid FROM #query1)";
$album_param = array(
'method' => 'fql.multiquery',
'queries' => '{"query1":"' . $album_fql . '", "query2":"' . $albumpic_fql . '"}');
$album_fqlResult = $facebook->api($album_param);
print_r($album_fqlResult);
Any ideas?
you can do that in a nested query rather than as a multi query.
SELECT pid, src_small
FROM photo
WHERE pid in (SELECT cover_pid FROM album where owner={page id here})
for example here's the list of album covers I got from CocaCola's Facebook page
SELECT pid, src_small
FROM photo
WHERE pid in (SELECT cover_pid FROM album where owner=40796308305)
LIMIT 5
returned:
{
"data": [
{
"pid": "40796308305_2143765",
"object_id": 110516463305,
"src_small": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-snc1/6570_110516463305_40796308305_2143765_6253354_t.jpg"
},
{
"pid": "40796308305_8753877",
"object_id": "10150500888203306",
"src_small": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/398389_10150500888203306_40796308305_8753877_1814220329_t.jpg"
},
{
"pid": "40796308305_8751674",
"object_id": "10150500434253306",
"src_small": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/407475_10150500434253306_40796308305_8751674_1040781388_t.jpg"
},
{
"pid": "40796308305_8742570",
"object_id": "10150498588158306",
"src_small": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/396541_10150498588158306_40796308305_8742570_1378170213_t.jpg"
},
{
"pid": "40796308305_8742259",
"object_id": "10150498546173306",
"src_small": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-snc7/402667_10150498546173306_40796308305_8742259_287537096_t.jpg"
}
]
}
If you want to try your multiquery, then try to name the queries starting at index 0 instead of 1. e.g. query0 and query1 This is the only way the C# SDK allows it, maybe the php SDK requires that too.
Finally figured it out. I had to merge the queries into 1 variable and escape the single quotes.
$queries = '{
"query1" : "SELECT aid, name, cover_pid FROM album WHERE owner = \'xxxxxxxxxxx\'",
"query2" : "SELECT aid, images FROM photo WHERE pid IN (SELECT cover_pid FROM #query1)"
}';
Related
I am trying to get data from 2 different tables: users and comments.
I want to return comment data and user data depending on the commentsenderid .
<?php
$commentpostid = $_POST['commentpostid'];
$sql = "SELECT * FROM comments where commentpostid = '{$commentpostid}'";
$sqll = mysqli_query($db, $sql); // $db->query() is also accepted.
$result = mysqli_fetch_all($sqll, MYSQLI_ASSOC);
echo json_encode($result);
Result:
[
{
"commentid": "93",
"comment": "naber kankam Benin",
"commentpostid": "1006",
"commentsenderid": "12"
},
{
"commentid": "95",
"comment": "kankam selam!",
"commentpostid": "1006",
"commentsenderid": "3135"
}
]
Should be:
[
{
"commentid": "93",
"comment": "naber kankam Benin",
"commentpostid": "1006",
"commentsenderid": "12",
"username": "denemeuser",
"userbolum": "denemebolum",
"useryas": "useryasdeneme"
},
{
"commentid": "95",
"comment": "kankam selam!",
"commentpostid": "1006",
"commentsenderid": "3135",
"username": "denemeuser",
"userbolum": "denemebolum",
"useryas": "useryasdeneme"
}
]
[]
[]
You select all comments, join them with users whose id matches the id of the authors of comments, you select only those whose post matches the value you are looking for.
SELECT c.*, u.username, u.userbolum, u.useryas
FROM comments c
JOIN users u ON c.commentsenderid = u.userid
WHERE commentpostid = ?
I am trying to insert data to 2 different tables in my database.
First table is named business which I have done it to insert data to from the json file to the database.
But when I am trying to insert data into table named business_phone it does not do anything.
Here is my code for inserting the data:
$query = '';
$query_phones='';
$table_data = '';
$filename = "businesses.json";
$businesses = file_get_contents($filename);
$business = json_decode($businesses, true);
foreach($business as $row)
{
$query .= "INSERT INTO business(title, address, website, page) VALUES ('".$row["title"]."', '".$row["address"]."', '".$row["website"]."', '".$row["page"]."'); ";
//data that i will show on page
$table_data .= '
<tr>
<td>'.$row["title"].'</td>
<td>'.$row["address"].'</td>
<td>'.$row["website"].'</td>
<td>'.$row["page"].'</td>
</tr>
';
}
foreach($business as $row)
{
$query_phones .="INSERT INTO business_phones(business_title, phone_number, phone_name) VALUES ('".$row["title"]."', '".$row["number"]."', '".$row["name"]."');";
}
Here is some code from the json file
[
{
"title": "CONSERVE IT LTD",
"address": "12 Truman Ave (10) ",
"phones": [
{
"name": "telephone_1",
"number": "876-754-0220"
},
{
"name": "telephone_2",
"number": "876-754-0221"
}
],
"website": "www.conserveitja.com",
"page": 1
},
{
"title": "Consie Walters Cancer Hospital",
"address": "22 Deanery Rd (3) ",
"phones": [
{
"name": "telephone_1",
"number": "876-930-5016"
}
],
"page": 1
},
...
]
I don't know how you to handle within php but you can create an auxiliary table to be populated from the file, and then use JSON_TABLE function for the key values to be inserted into that table, provided you're using MySQL DB ver. 8+ :
INSERT INTO business(title, address, website, page)
SELECT t.*
FROM tab
CROSS JOIN
JSON_TABLE(jsdata, '$[*]' COLUMNS (
title VARCHAR(100) PATH '$.title',
address VARCHAR(100) PATH '$.address',
website VARCHAR(100) PATH '$.website',
page VARCHAR(100) PATH '$.page')
) t
and
INSERT INTO business_phones(business_title, phone_number, phone_name)
SELECT t.*
FROM tab
CROSS JOIN
JSON_TABLE(jsdata, '$[*]' COLUMNS (
business_title VARCHAR(100) PATH '$.title',
NESTED PATH '$.phones[*]' COLUMNS (
phone_number VARCHAR(100) PATH '$.number',
phone_name VARCHAR(100) PATH '$.name')
)
) t
a side Note concatenations for sql statements are vulnerable to Injection for most of the programming languages as #Magnuss Eriksson mentioned.
Demo
$row["number"] is not valid. It will be $row["phones"][0] or $row["phones"][1]. Because according to your data "number" and "name" inside of "phones" which is array.
You can nested loop through "phones"
foreach($row["phones"] as $contact){
$query .="INSERT INTO business_phones(business_title, phone_number,
phone_name) VALUES ('".$row["title"]."', '".contact."',
'".$row["name"]."');"
}
How would I make the $query_ppimage results a sub array from the first users query? At the moment, the output is outputting a user, then a profile image, under the user, rather than inside the users array. Therefore they aren't linked.
How would I do such a thing?
Here is my code:
$query_user = "SELECT *,'user' AS type FROM users WHERE username LIKE '".$query."' OR firstname LIKE '".$query."' OR lastname LIKE '".$query."'";
$quser = $conn->query($query_user);
$rows = array();
while($user = mysqli_fetch_assoc($quser)) {
$query_ppimage = "SELECT id, post_id, relation, userID, file_format FROM media WHERE userID = '".$user['id']."' AND relation = 'profile_picture' UNION ALL SELECT -1 id, '55529055162cf' post_id, 'profile_picture' relation, '0' userID, 'jpg' file_format ORDER BY id DESC";
$qppimg = $conn->query($query_ppimage);
while($ppimg = mysqli_fetch_assoc($qppimg)) {
$rows[] = $ppimg;
$rows[] = $user;
}
}
Here is how the array is returned:
[
{
"id": "117",
"post_id": "1",
"relation": "profile_picture",
"userID": "3",
"file_format": "jpg"
},
{
"id": "3",
"email": "casper#socialnetwk.com",
"type": "user"
},
]
How it should look, or something similar. I don't think I named the sub array correctly, but it needs a name ppimage
[
{
"id": "3",
"email": "casper#socialnetwk.com",
"type": "user"
ppimage: {
"id": "117",
"post_id": "1",
"relation": "profile_picture",
"userID": "3",
"file_format": "jpg"
}
},
]
You are adding to $rows twice and appending two separate elements. Try building your array element first and then adding it into the $rows array. Something like this:
$newrow = $user;
$newrow['ppimage'] = $ppimg;
$rows[] = $newrow;
Using JOIN, you could limit this to one single query. Having a query inside a loop is never a good idea, and you should avoid it if you can. Furthermore, you should use prepared statements in order to protect against SQL injection.
The code below uses a JOIN, so that you just get one query - and structures the array as shown in the example. This is hard-coded, as its easier to control what goes where, since we now use a JOIN, the data is all fetched at once (and not in separate variables).
$row = array();
$stmt = $conn->prepare("SELECT m.id, m.post_id, m.relation, m.file_format, u.id, u.email, 'user' as type
FROM media m
JOIN users u ON u.id=m.userID
WHERE m.relation = 'profile_picture'
AND (username LIKE ?
OR firstname LIKE ?
OR lastname LIKE ?)
ORDER BY m.id DESC");
$stmt->bind_param("sss", $query, $query, $query);
$stmt->execute();
$stmt->bind_result($mediaID, $postID, $relation, $file_format, $userID, $user_email, $type);
while ($stmt->fetch()) { // If you just expect one row, use LIMIT in the query, and remove the loop here
$row[] = array('id' => $userID,
'email' => $user_email,
'type' => $type,
'ppimage' => array('id' => $mediaID,
'post_id' => $postID,
'relation' => $relation,
'userID' => $userID,
'file_format' => $file_format)
);
}
$stmt->close();
How can I prevent SQL injection in PHP?
MySQL JOIN
I have 2 tables; the first one is user_in_event table
which here role can be regular, waiting for ack or moderator
and the second one is user table
Here the role can be regular or admin
I want to take the role from user_in_event table, based on the event_id and output all users info (beside user role) and output in a single JSON
I have tried to use LEFT JOIN
$query = "SELECT user_in_event.role, user_in_event.user_id, user.name, user.email, user.birthday, user.phone_number, user.address, user.image
FROM user LEFT JOIN user_in_event
ON user_in_event.event_id = '".$event_id."'
LIMIT $num_of_rows";
$result = mysqli_query($con,$query)
or die(mysqli_error($con));
// print_r($query);
while ($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($response);
but I got messed up data
[
{
"role": "moderator",
"user_id": "2",
"name": "ofir",
"email": "ofir#ofr.com",
"birthday": "08/12/2016",
"phone_number": "123",
"address": "yoqneam",
"image": "http://imgur.com/a/KslOW"
},
{
"role": "waiting for ack",
"user_id": "21",
"name": "ofir",
"email": "ofir#ofr.com",
"birthday": "08/12/2016",
"phone_number": "123",
"address": "yoqneam",
"image": "http://imgur.com/a/KslOW"
}
]
PS: I also tried to make 2 different queries and combine the JSON and user array_merge_recursive but I got 2 subs arrays instead of a single one
Please try :
SELECT user_in_event.role, user_in_event.user_id, user.name, user.email
FROM user
LEFT JOIN user_in_event ON user_in_event.user_id = user.user_id
WHERE user_in_event.event_id =25
LIMIT 0 , 30
Here event_id is static. run this query to your database and let me know.
looks /me/likes contains a summary of the FQL ones 'activities,books,interests,movies,music,tv'.
But if you compare both outputs then /me/likes shows much more!
For example just LIKE this Tetris page:
http://www.facebook.com/pages/Tetris/107667482589691
It will appear at /me/likes but it will NOT appear in 'activities,books,interests,movies,music,tv'.
So how can i retrieve this add. data through FQL?
It DOES appear
These "likes" means that you are a "fan" of that (activity,movie,game)'s page!
Here's the FQL:
To get the page_id only:
SELECT page_id
FROM page_fan
WHERE uid=me()
And to get the page info:
SELECT page_id,name
FROM page
WHERE page_id IN (
SELECT page_id
FROM page_fan
WHERE uid=me()
)
Results:
[
{
"page_id": 107667482589691,
"name": "Tetris"
},
{
"page_id": 375266451894,
"name": "I Play Soccer"
},
{
"page_id": 17475686963,
"name": "Hanine Y son cubano"
},
{
"page_id": 91290503700,
"name": "Inception"
},
...etc