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"];
..............
.........
}
Related
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
I want to convert my query result to two dimensional array the query is
SELECT c.name,c.mobile_number,t.service_time
FROM CUSTOMER AS c
JOIN TRANSACTION AS t ON c.id = t.customer_id
WHERE t.service_date = '$date'
AND employee_id = '$employee_id
I get name(varchar),mobile num(int) and time which is time format. I wrote some code but it is not working:
$results = array();
echo $num_fields=mysqli_num_fields($array1);
echo $num_rows=mysqli_num_rows($array1);
while($line = mysqli_fetch_array($array1))
{
for($i=0;$i<$num_rows;$i++)
{
for($j=0;$j<$num_fields;$j++)
{
$results[$i][$j]=$line[$i][$j];
}
}
}
The result I am getting is:
[["k ","a ","r "],["9 ","8 ","7 "],["0 ","2 ",": "]]
The output contains only first characters that to one. I want each row of two dimensional array to have row of my query result.
You can just add the result to a new array and change mysqli_fetch_array to mysqli_fetch_row:
$results = array();
while ($line = mysqli_fetch_row($array1))
{
$results[] = $line;
}
Alternatively you can use mysqli_fetch_assoc() to make for an associative array (e.g. $results[0]['name'] will be the name column of the first row).
Edit
If there are duplicate column names you can use aliases:
SELECT one.name AS one_name, two.name AS two_name FROM one INNER JOIN two USING ...
The associative array will then have $result[0]['one_name'] and $result[0]['two_name'].
try this:
while($line = mysqli_fetch_row($array1))
{
array_push($result, $line);
}
This should do it
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con, "SELECT * FROM .....");
$rows = array();
if($result){
while($row = mysqli_fetch_assoc($con, $result)){
array_push($rows, $row);
}
mysqli_free_result($result);
}
var_dump($rows);
$sql = "SELECT name FROM students";
$result = $conn->query($sql);
According to code above the $result is an object which contains the values from database.
suppose there are two names in the database under the column "name" like name1 and name2
now what i want is to convert the object $result into an array which will contain name1 and name2 as array element like
$name_array = array('name1' , 'name2')
how can i do that ??
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// acess the each colum elements using $row["<col name>"]
//this will loop through all the rows
}
} else {
// no rows where fetched
}
That is $row = $result->fetch(); will store the values of a row
in the array $row with key as column names
Answering my own question
got a solution for my problem and realized that it's pretty basic and shouldn't have posted this question. this type of problem should be solved by oneself.
$query = "SELECT name FROM students";
if($query_run = mysql_query($query)) {
$name_array = array();
while($query_row = mysql_fetch_assoc($query_run)) {
array_push($name_array, $query_row['name']);
}
the array_push function will do the trick.
thank you.
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']
}
I have this code:
public function getRecurringEventsByGrouped($grouped){
$query = "SELECT * FROM `event` AS e
WHERE e.`grouped` = " . $grouped . " ORDER BY EventId DESC";
$result = mysql_query($query);
while ($ids[] = mysql_fetch_array($result, MYSQL_NUM)) ;
return $ids;
}
mysql_fetch_array() doesn't return the first row. mysql_num_rows() returns correct row count.
I also tried this query in HeidiSQL and it gave the same rows number as mysql_num_rows().
you need to iterate through the data in while loop.
$ids = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
//check for proper indexing of table rows and specify $row[value] accordingly.
$ids[] = $row[0];
}
return $ids;
this should fetch you the correct content.