When I do this I am only getting one result back Fantasy0.1429 when I should be getting 7 different ones any one know what I am doing wrong.
$userid = $_SESSION['sess_id'];
$genreQuery = $con ->query ("select distinct(genre) from movies");
$movieGenre = array();
while($row = $genreQuery->fetch_object()) {
$movieGenre[] = $row;
}
foreach($movieGenre as $MGenre){
$query = $con ->query
(" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
from user_movie_ratings umr,
movies m
where umr.user_id = '$userid'
and umr.movie_id = m.id
and m.genre = '$MGenre->genre'; ");
$movieTitle = array();
while($row = $query->fetch_object()) {
$movieTitle[] = $row;
}
}
Then I echo it out later
<?php foreach($movieTitle as $movie): echo $movie->genre; echo $movie->rating; endforeach; ?>
Try to move away the instanciation of your array before the foreach like:
// Some code ...
$movieTitle = array();
foreach($movieGenre as $MGenre){
$query = $con ->query
(" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
from user_movie_ratings umr,
movies m
where umr.user_id = '$userid'
and umr.movie_id = m.id
and m.genre = '$MGenre->genre'; ");
while($row = $query->fetch_object()) {
$movieTitle[] = $row;
}
}
Related
So we recently set up a mySQL database for a project in our university and are currently working on the data access files written in php. In order to use the data on our front end we need it formatted as JSON. While putting an array of objects in the result of a certain object is working it won't work with 2 arrays of objects.
Working code:
<?php
include_once 'db.php';
$email = "email#online.com";
$conn = connect();
$conn->set_charset('utf8');
$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);
$sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$resultArr['customer'][$row['email']]['shops'][] = $row2;
}
}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);
?>
And here is the not working code:
<?php
include_once 'db.php';
$email = "email#online.com";
$conn = connect();
$conn->set_charset('utf8');
$resultArr = array();
$sql = "SELECT * FROM `customer` WHERE email = '$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$resultArr['customer'][$row['email']] = array('email' => $row['email'], 'address' => $row['address']);
$sql2 = "SELECT id, name, address FROM shop INNER JOIN rel_shop_customer WHERE customer_email='".$row['email']."' AND rel_shop_customer.shop_id = shop.id";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$resultArr['customer'][$row['email']]['shops'][] = $row2;
}
$sql3 = "SELECT img, name FROM ad INNER JOIN rel_customer_ad WHERE customer_email='".$row['email']."' AND rel_customer_ad.ad_name = ad.name";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc()) {
$resultArr['customer'][$row['email']]['ads'][] = $row3;
}
}
$resultArr['customer'] = array_values($resultArr['customer']);
} else {
echo "failure";
}
echo json_encode($resultArr, JSON_UNESCAPED_UNICODE);
?>
Both SQL queries provide the expected result when run directly on the database and I have no idea why the second one won't work in the PHP script.
<?php
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 1";
$query1 = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 2";
$comments = mysql_query($query);
$comments1 = mysql_query($query1);
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
while($row = mysql_fetch_array($comments1, MYSQL_ASSOC)) {
$bobot1 = $row['bobot'];
$bobot1 = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
?>
I want to make this code can looping until 10. I hope that there aren't many variable, ex: $query, $query1, $query2, ..., $query10, $comments, $comments1, $comments2, ..., $comments10, $bobot, $bobot1, $bobot2, ..., $bobot10. Someone help me, please ...
You're almost there. But I have to mention that you should start using parameterized queries with prepared statements instead of constructing your queries manually.
$id = 1;
while($id <= 10) {
// construct your query
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = $id";
// execute and get results
$comments = mysql_query($query);
// iterate over records in result
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
// increment the id for next cycle through the loop
$id = $id + 1;
}
How can I concatenate two columns in where clause
this is my normal query:
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
I want the stud_fname and stud_lname to be concatenated.
here is my full code:
<?php
error_reporting(0);
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("dbmobile_class_record") or die("Could not select database");
// array for JSON response
$response = array();
$instructor_id=$_GET['instructor_id'];
$description=$_GET['description'];
$stud_fname=$_GET['stud_fname'];
$stud_lname=$_GET['stud_lname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["student"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$student = array();
$student["stud_id"] = $row["stud_id"];
// push ordered items into response array
array_push($response["student"], $student);
}
// success
$response["success"] = 1;
}
else {
// order is empty
$response["success"] = 0;
$response["message"] = "No Records Found";
}
// echoing JSON response
echo json_encode($response);
?>
$instructor_id = $_GET['instructor_id'];
$description = $_GET['description'];
$fullname = $_GET['fullname'];
// get all items from myorder table
$result = mysql_query("SELECT stud_id FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND CONCAT(stud_fname, ' ', stud_lname) = '$fullname'") or die(mysql_error());
Use this one
$result = mysql_query("SELECT stud_id, CONCAT_WS('', stud_fname, stud_lname) AS stud_fullname FROM tb_student WHERE instructor_id = '$instructor_id' AND description = '$description' AND stud_fname = '$stud_fname' AND stud_lname = '$stud_lname'") or die(mysql_error());
stud_fullname should contain what you want.
Following is my PHP code which is only giving i =0 though in a loop I am incrementing the $i but it always return i as 0 and while loop is only working one time, though my query SELECT * FROM events WHERE DATE(event_date) < CURDATE() is returning 7 records when exectuing in phpmyadmin. Let me know what i am doing wrong here ?
Code -
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/app/'."config.php";
error_reporting(E_ALL);
if( $_POST['number'] == 'all' ) {
$eventArr = array();
$myarray = array();
$query = "SELECT * FROM events WHERE DATE(`event_date`) < CURDATE()";
$result = mysql_query($query);
$i =0;
while($row = mysql_fetch_assoc($result)) {
$eventArr[$i] = array('event_data'=> $row);
// Get image For an event
$event_id = $row['id'];
$query = "SELECT * FROM event_images WHERE event_id = $event_id ORDER BY `uploaded_date` DESC LIMIT 0,1";
$result = mysql_query($query);
$eventImgArr = array();
while($row = mysql_fetch_assoc($result)) {
$eventImgArr[] = $row;
}
$eventArr[$i]['event_image'] = $eventImgArr;
// Get venue details for the event
$venue_id = $row['venue_id'];
$eventVenArr = array();
$query = "SELECT * FROM `venues` WHERE id = $venue_id";
while($row = mysql_fetch_assoc($result)) {
$eventVenArr[] = $row;
}
$eventArr[$i]['venue_detail'] = $eventVenArr;
echo $i, " -- ";
$i++;
}
$myarray = array('response'=>'1','message'=>'Event data', 'data'=>$eventArr);
echo json_encode($myarray);
return;
}
You are re-using the $result variable for the other queries, which is destroying its value needed for the main loop.
P.S. Also, you're not actually executing the query for the venue details.
I am passing the company information with Json, and i want also pass the areas with the same method, but something is not well, can anyone help me?
if($_GET['method']=="get_all_companies"){
$query = "SELECT company.id as c_id, company.c_name as c_name, company.c_desc as c_desc,company.c_address as c_address FROM company";
$result = mysql_query($query);
if($result){
$query_areas = "SELECT company.id as c_id, bussiness_area.* FROM company
LEFT JOIN company_area
ON company_area.id_company = company.id
LEFT JOIN bussiness_area
ON bussiness_area.id = company_area.id_area
WHERE company_id = c_id";
$result_areas = mysql_query($query_areas);
$return_arr = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$areas_arr = array();
while ($row2 = mysql_fetch_array($result_areas, MYSQL_ASSOC)) {
$row_array['c_id'] = $row['c_id'];
$row_array['c_name'] = $row['c_name'];
$row_array['c_desc'] = $row['c_desc'];
$row_array['c_address'] = $row['c_address'];
$row_array['bu_title'] = $row2['bu_title'];
array_push($return_arr,$areas_arr,$row_array);
}
}
echo json_encode($return_arr, $areas_arr);
}
else
{
echo json_encode(array('error' => 'true'));
}
}
From the first json_encode() the $areas_arr variable is not necessary.
json_encode($return_arr);