Putting Mysql results into a multidimensional php array - php

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

Related

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

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 loop not performing as it should

$query = "SELECT * FROM table WHERE data = '$userinput'";
$row = mysql_query($query);
while ($row = mysql_fetch_array($row))
{
echo $row['data'];
}
Ok So my questions are:
What exactly does mysql_fetch_array return?
AND if my WHERE clause finds multiple matches, shouldn't the loop execute numerous times?
I am running a program and I can only get the first result to print
You're overwriting $row. Instead use a different variable for the query result.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo $row['data'];
}
Try using mysql_fetch_assoc($row)
You should not use the same variable as the index and the loop variable.
$row = mysql_query
while( $row = ...
Replace the first $row with $result

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