show data with reading column name for json - php

I always show my data look like bellow for printing JSON .
Current Code:
if (mysql_num_rows($data) > 0) {
while($row = mysql_fetch_array($data)) {
$info[] = array(
'ID' =>"".$row['id'],
'game_id' =>"".$row['game_id'],
'appliance_name'=>"".$row['appliance_name'],
'area'=>"".$row['area'],
'type' =>"".$row['type'],
'description'=>"".$row['description'],
'code'=>"".$row['code']
);
}
}
Here i want to show my array key name as database table column name as a example
when my query face data and store it in $info array that time automatically name of key make will be column name code'=> then the value.
So I have no tension how many column exist in database and what will be every array key name

while($row = mysql_fetch_assoc($result)) {
$info[] =$row;
}
working code.

Related

php get from mysql all values by column to json_encode

I must put to json_encode all values of specifed mysql table column
$fromdate = $_GET['fromdate'];
$getrezhiredh = safe_query("
SELECT rezhour FROM rezhiredhours
WHERE rezdate = '".$fromdate."' ORDER BY rezhour
");
$rows = array();
while($r = mysql_fetch_assoc($getrezhiredh)) {
$rows[] = $r;
}
print json_encode($rows);
With code above i have one problem. This code return result only when in table we have one row with selected data. In this case json_encode() result is
[{"rezhour":"1"}]
But when table have more than one row with selected data result don't return anything but
[]
How put to json_encode() all values selected from table?
EDIT:
I just wonder why in case when we have more rows in table with selected data, result don't give as example below
[{"rezhour": { [0] => "1",[1] => "4" }]
Instead in result we have "[]"
Thank You in advance.
try changing
mysql_fetch_assoc($getrezhiredh)
to
mysql_fetch_object($getrezhiredh)
Results from mysql_fetch_assoc are different than you think. Each row is more or less like this:
array(1)
"rezhour" => "1"
So you access the data it like this:
while($r = mysql_fetch_assoc($getrezhiredh)) {
$rows[] = $r["rezhour"];
}

PHP get one row from an array based on a variables value

If i create an array from results returned in a mysqli query is there a way to select and use only one specific row from the array?
$info= array();
while($row = mysqli_fetch_assoc($query)) {
$info[] = array(
'id' => $row['id'],
'location' => $row['location']
);
}
How would i go about displaying only a single row from this array where the id equals a variable like $id?
In your loop you could just do something like:
if ($id == $row['id']) {
$info[] = $row;
}
However it would make more sense to me to just update your query.
SELECT cols FROM t1 WHERE id = :id
Using $id as a parameter.

column counterpart to mysql_fetch_array() in PHP

I am new to PHP and looking for some help.
I read a column of data from a table in a database and send the data to a dropdown menu in the form of an array.
Now, the function mysql_fetch_array() converts a row of data into an array. I tried using the same function on a column with improper results.
Is there a function that similarly converts a column of data into an array?
You could simple iterate over the result(-rows) and get your column array if you like:
$columnArrays = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
foreach ($row AS $key=>$value){
if (isset($columnArrays[$key]))
$columnArrays[$key][] = $value; //add to existing array
else
$columnArrays[$key] = array($value); //create new array
}
}
Now you have all ids (depending on the column name) in $columnArrays["id"] - all names in $columnArrays["name"] and so on.

Check $row for value and amend

I am using flexigrid for displaying data and need help to check a row for a value and change it. The data is being returned to flexigrid via json and works well and without ant errors. however, what I am trying to do is check the row; $row['status'] for a value = 1 and if that row is that value then change it to value = "In".
I have tried various ways to code using an if statement, but it does not error, just dosen't display the data in the grid. What is the correct way to code this row? Thanks
$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
$data['rows'][] = array(
'id' => $row['Id'],
'cell' => array($row['Id'], $row['rack'].$row['column'].'-'.$row['row'].'-'.$row['bay'], $row['customer'], $dropdown, $service, $row['department'], if ($row['status']==1){$row['status']="In"}, $row['custref'], $row['size'], $row['authorisation'], date('d/m/Y H:m:s',strtotime($row['intake_date'])), date('d/m/Y',strtotime($row['destroy_date']))));
}
echo $json->encode($data);

PHP, SQL, in_array with csv and while loop only retrieving integer

I have searched for similar questions but cannot find the right answer to my specific one. I have a column (data) in my table (table) which contains comma separated values which are id's e.g.
Row 1= 4,5,45
Row 2= 5,8,9
Row 3= 5
I use an in_array function to retrieve the number of occurances for the $data value within the while loop. So I use an sql function to retrieve the number of times a certain value such as '5' occurs in all rows within the while loop.
The issue is that I can only retrieve the $data value if it is by itself (i.e. no commas just the integer by itself) so based on my example in the list, I can only retrieve 5 once (row 3). I would like to retrieve the value '5' three times as it appears in all the rows. Here is my code below and any help would be appreciated. The $selectiontext variable is what the user enters from the form.
$sql_frnd_arry_mem1 = mysql_query("SELECT data, id FROM table WHERE data='$selectiontext'");
while($row=mysql_fetch_array($sql_frnd_arry_mem1)) {
$datacheck = $row["data"];
$id = $row["id"];
}
$frndArryMem1 = explode(",", $frnd_arry_mem1);
if (($frnd_arry_mem1 !=="") && (!in_array($id, $frndArryMem1))) {echo $id;}
Thank you.
you keep overwriting the variables $datacheck and $id, leaving you with only the last version of them. move that curly brace down two lines, like this:
$sql_frnd_arry_mem1 = mysql_query("SELECT data, id FROM table WHERE data='$selectiontext'");
while($row=mysql_fetch_array($sql_frnd_arry_mem1)) {
$datacheck = $row["data"];
$id = $row["id"];
$frndArryMem1 = explode(",", $frnd_arry_mem1);
if (($frnd_arry_mem1 !=="") && (!in_array($id, $frndArryMem1))) {echo $id;}
}
I am not totally sure of your requirements, is not very clear what you intend to do as part of the code is missing.
I assume you want to check each row of a comma separated of a CSV file ($frnd_arry_mem1 being the row) against your data in column data, if any of those comma separated data (not) occurs.
Assuming you are already looping through your CSV, this would be the code (non tested):
NOTE: might be inefficient retrieving data within a loop, if you can do otherwise - but I cant tell as I dont kow the full specs of your script.
If I got the specs wrong please clarify, I will try to help further.
$sql = "SELECT data, id
FROM table
WHERE data='$selectiontext'";
$sql_frnd_arry_mem1 = mysql_query($sql);
$results = array();
// get values to check
while ($row = mysql_fetch_array($sql_frnd_arry_mem1))
{
$results[] = array(
'datacheck' => $row["data"],
'id' => $row["id"],
);
}
foreach ($results as $result)
{
$frndArryMem1 = explode(",", $frnd_arry_mem1);
$data = explode(',', $result['datacheck']);
foreach ($data as $d)
{
if (($frnd_arry_mem1 !=="") && (!in_array($d, $frndArryMem1)))
{
echo 'DEBUG: Record #' . $id . ' not found, value:' . $d . '</br>';
}
}
}

Categories