Display specific PHP array from MySQL - php

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));}

Related

php mysql add sub array to main array

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.

Php Build an Array from MySQL

I currently collect data like this :
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['id'].$row['name'].$row['surname'].$row['email'].$row['dob'];
echo "<br />";
}
It outputs all the data in one line, like this
1maxpaynemax#hat.com24/07/1950
I want to build the data into a Array rather so it looks like this :
$fields = array(
'id' => '21890',
'name' => 'nick',
'surname' => 'moppy',
'email' => 'nick#moppy.com',
'dob' => '11-01-1965',
),
You already have your array:
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
var_dump($row);
}
So I'm going to make an assumption here. That is that you only want id, name, surname, email and dob. If you want all the columns returned from the table and in the array, just return the SELECT to what it was.
$query = "SELECT id, name, surname, email, dob FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
// $row is now what your example array looks like
}
So there are 2 differences, first, the specified columns from the table. If you're actually wanting all of the columns returned (back to using * for example), but don't want all of the columns returned in your array, this won't work (but you haven't said either way) but #b0s3 first example will.
Second, the addition of the MYSQL_ASSOC parameter. This tells PHP to return an array with only the column name indicies as opposed to them AND numeric keys which doubles up the number of items in the array.
You should use this way.
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}
echo "<pre>"; print_r($res); echo "</pre>";

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']
}

Populating an HTML Table from SQL DB with PHP

I'm attempting to populate a table in HTML with data from a DB. It's not working properly (a blank white page is displayed), but I can't find the source of the error.
<?php
$sql = "SELECT * FROM Orders";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($result)){
$order_id = $row['orderID'];
$order_due = $row['order_due'];
$order_subject = $row['order_subject'];
$order_level = $row['order_level'];
$order_pages = $row['order_pages'];
$order_cost = $row['order_cost']);
echo "<tr><td>".$order_id."</td><td>".$order_due."</td><td>".$order_subject."</td><td>".$order_level."</td><td>".$order_pages."</td><td>".$order_cost."</td></tr>";
}
echo "</table>";
?>
$order_cost = $row['order_cost']);
you have an extra paranthesis
Also change this
while($row = mysql_fetch_array($result))
to
while($row = mysql_fetch_assoc($result))
I think you need to change third line to:
result = mysql_query($sql) or die(mysql_error());
(space before "or")

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