php mysql add sub array to main array - php

I need to append result of mysql query with existing result of another query, and both queries performed on different table, actually what I really want to do is take value from main table and get some data from another table using first value, and combine all together using json encode.
$query = "select ID,TIMESTAMP,UUID from TABLE1 where USERNAME='$user'";
$result = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($result);
if($numrows>0)
{
$res=$a;
$myArray = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$eachuuid = $row[UUID];
$query1 = "select ID,IMAGEURL from TABLE_IMAGES where IMG_UUID='$eachuuid'";
$result1 = mysqli_query($conn, $query1);
$numrows1 = mysqli_num_rows($result);
if($numrows1>0){
$res1;
$myArray1 = array();
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1[] = $row1;
}
$row['IMG_URL']=$myArray1;
}
$myArray[] = $row;
}
$res['Data']=$myArray;
echo json_encode($res);
Result:
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":[{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}]}]}
And I need to get the output like,
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}}]}
Means need to remove [] from IMG_URL section so that I can parse the data easily.

You can do this
$row['IMG_URL']=$myArray1[0];
Or
$myArray1 = null;
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1 = $row1;
}
$row['IMG_URL']=$myArray1;
I hope this helps.

Related

Add one result from one query and then second from another one

I have Two MySQLi Queries and two result variables as below
$result = mysqli_query($con, $query);
$result1 = mysqli_query($con, $query1);
what I want to do is make an array of all results , but the condition is it must contain one result from $result then the next one should be from $result1 and next one from $result and so on until both variables have values.
while($row = mysqli_fetch_assoc($result))
{
$links[] = $row["url"];
while($row1 = mysqli_fetch_assoc($result1))
{
$links[] = $row1["ads_id"];
}
}
Currently I have this this works like this , adds One value from $results next adds all values from $results1 and next one by one adds all values from $result .
Can anybody help me out with this logic, I can not work out the logic for this thing.
You could try something like this. It loops through $result and $result1 at the same time, finishing up with any leftover rows from $result1:
while ($row = mysqli_fetch_assoc($result)) {
$links[] = $row["url"];
if ($row1 = mysqli_fetch_assoc($result1))
$links[] = $row1["ads_id"];
}
while ($row1 = mysqli_fetch_assoc($result1)) {
$links[] = $row1["ads_id"];
}

Display specific PHP array from MySQL

Right now all the columns from the MySQL result are converted to JSON and printed out on the screen. I would like to print only two column $row['name'] and $row['gender'] to the screen. Any ideas guys
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail")
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row));}
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")
Here is my solution for getting all from the table and display only two values....
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail") //getting all the fields
while($row = mysql_fetch_assoc( $result)) {
$data = array('name' => $row['name'] , 'gender' => $row['gender']); //get the two values into one array
print_r(json_encode($data)); //printing the name and gender
}
If you want to get only that two fields here is another query...
include('connect-db.php');
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail") //getting two the fields
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row)); //printing the name and gender
}
If you want all data but you have to print only name & gender then ..Create one separate array & allocate name & gender...
$data_array = array();
while($row = mysql_fetch_assoc( $result)) {
array_push($data_array,array("name"=>$row['name'],"gender"=>$row['gender']);
}
print_r(json_encode($data_array));
include('connect-db.php');
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row));}

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

Putting Mysql results into a multidimensional php array

I have a mysql table which I want to have it as a PHP array. Suppose we have a field call id and another field called name. As there may be not one result in the table I want something like $result[0]['id'] to point the first result's id.
I thought of this:
$result = mysql_query("SELECT * FROM db_name
WHERE dependence = 0");
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows = $row;
}
echo $rows[0]['name'];
But it doesn't work!!! Would you please help me?
$result = mysql_query("SELECT * FROM db_name
WHERE dependence = 0");
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
try this
$i=0;
while($row = mysql_fetch_assoc($result)){
$rows[$i] = $row['id'];
$rows[$i] = $row['name'];
$i++;
}
You forgot to give column name
while($row = mysql_fetch_array($result)){
echo $row['colname'];//fill inside which column you need $row['colname']
}

How to I retrieve data from a mysql table cell and put it into a variable in php?

I am creating a function that will retrieve an id number from a table with multiple columns, put it in a variable which i want to use to send as a "claims number" in an email to a claimant and also echo on a confirmation screen. Here's what I have so far.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$claim_id = $result;
$result = mysql_fetch_array(mysql_query($sql));
$claim_id = $result['id'];
Supposing you are sure that you will receive 1 row.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$c = mysql_fetch_assoc($result);
$claim_id = $c['lname'];
NB*: And get ready for a lecture. Someone is going to tell you that mysql is depracated.
Start using MySQLi or PDO (recommended).
Try :
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$claim_id = $row[0];
or
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$claim_id = $row['id'];
}
$row = mysql_fetch_array($result);
$claim_id = $row['id'];
please note that you should also check for errors (e.g. if($result === FALSE)). Also, there are other ways to fetch data - I recommend looking at mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_object
Also, don't rely on the statement always returning exactly one row - make sure that the result is as expected: mysql_num_rows
Give this a try:
$sql = "SELECT id FROM warranty_claim where lname='".$lname."'";
$result = mysql_query($sql);
$objResult = mysql_fetch_array($result);
$claim_id = $objResult['id'];

Categories