suppose i have a query
$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
$data = $row['data'];
}
$array = $data;
Now how can i get that each $data values out of while in an array() i.e $array
$array = array();
$sql = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($sql)) {
$array[] = $row['data'];
}
by adding each $row to array $data
for reference: http://php.net/types.array
Note that there is no such a thing as "while() array". There are just arrays only.
Your $data variable is being overwritten during each iteration. So you need to turn it into $data[] and $data should be declared at the top, or you can just use $array[] as the other answer suggests, not sure why you put in the $data variable in their in the first place.
You need to save each iteration object into a "new" object, so your code will look just like:
$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
$array[] = $row['data'];
}
please note the line:
$array[] = $row['data'];
References:
PHP Arrays
$array = array();
$sql = mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($sql)){
$array[] = $row['data'];
}
further u can use
$array['data']
and u can also use mysql_fetch_assoc($sql) instead mysql_fetch_array($sql)
Related
So, using the Latte engine I can't seem to loop through every data row in my table. I can only get the first one and I've run out of ideas!
$query = $this->db->query("SELECT id,title FROM table");
$array = array();
while($fetch = $query->fetch_assoc()) {
$array = $fetch;
}
$Template["listing"] = $array;
Template:
{foreach $listing as $item}
{$item["title"]}}
{/foreach}
Simpliest solution is (if you don't need $array variable elsewhere):
$query = $this->db->query("SELECT id,title FROM table");
while($fetch = $query->fetch_assoc()) {
$Template["listing"][] = $fetch;
}
Instead of $array = $fetch, run $array[] = $fetch, in order to add more items.
What you're doing is overwrite the array in every loop.
$query = $this->db->query("SELECT id,title FROM table");
$array = array();
while($fetch = $query->fetch_assoc()) {
$array[] = $fetch;
}
$Template["listing"] = $array;
$query = $this->db->query("SELECT id,title FROM table");
$array = array();
while($fetch = $query->fetch_assoc()) {
$array[] = $fetch;
}
$Template = $array;
require_once('db.php');
$sql = "SELECT * FROM tbl";
$rs = mysql_query($sql);
$rows = array();
while($r = mysql_fetch_assoc($rs)) {
$rows['Record'][] = $r;
}
print json_encode($rows);
mysql_close($conn);
// it works perfect above
while($r = mysql_fetch_assoc($rs)) {
$r = utf8_decode(urldecode($r));
$rows['Record'][] = $r;
}
// it doesn't work above, may be $r is an object
how can I make it works?
The problem is that mysql_fetch_assoc returns an array, and you want to pass it to urldecode, which waits for a string as a parameter.
To achieve what you want, you should call urldecode to all the columns in the returned $r.
You can do it like this, for instance:
$r = array_map(function($col){
return utf8_decode(urldecode($col));
}, $r);
http://php.net/manual/en/function.mysql-fetch-assoc.php
http://php.net/manual/en/function.urldecode.php
My code is
$sql = 'select * from table;' $res = $connection->query($sql);
while($row = mysqli_fetch_row($res){'tackle output'};
to get an object
while($row = $res->fetch_object()){'tackle output'};
Question: what do I have to do to get an array of objects, so my output is thus
$data[$row[0]]= fetch_object();
try this one:
$data = array();
while($row = $res->fetch_object()){
$data[] = $row;
}
Use it like
$data = array();
while($row = $res->fetch_object()){
$data[$row->id] = $row;
}
may you want to fetch all
you can do this by
http://php.net/manual/en/mysqli-result.fetch-all.php
I have this code that fetches some fields from MYSQL database. The problem is that the php appears plain white. But if I delete elements of while and leave only one , the array prints fine.
$sql = "SELECT rest.img,rest.rest_name,horario.desc_hor, rest.descp, rest.rest_id FROM rest, type, horario WHERE rest.type_id = type.type_id AND rest.id_hor = horario.id_hor AND rest.type_id=1";
$result = $conn->query($sql);
$cnt = $result->num_rows;
$json = array();
if($cnt>0){
while($row = $result->fetch_assoc()){
$json['']=$row['img'];
$json['']=$row['rest_name'];
$json['']=$row['desc_hor'];
$json['']=$row['descp'];
$json['']=$row['rest_id'];
}
}
print( json_encode($json));
The problem with your code is that you are not assigning indexes automatically. You are always assigning the index [''] for all columns and rows in the array.
Try this...
$json = array();
while($row = $result->fetch_assoc()){
array_push($json, $row);
}
echo json_encode($json);
or this...
$json = array();
while($row = $result->fetch_assoc()){
$json[] = $row;
}
echo json_encode($json);
Of either ways, will generate the automátic indexes in the array.
I get an array of values returned from the following function:
function get_subscribitions($user)
{
$user = mysql_real_escape_string ($user);
$sql = "SELECT 'user_id' FROM `subscribe` WHERE subscriber = '$user'";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
mysql_free_result($result);
return $rows;
I now want to use these values in a new function, where each "user_id" is used to collect text from the database through this function:
function get_text($writer) {
$writer = mysql_real_escape_string ($writer);
$sql = "SELECT * FROM `text` WHERE user_id='$writer' ORDER BY timestamp desc";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
mysql_free_result($result);
return $rows;
However the returned value from the first function is an array, and as I've learnt the hard way, arrays cannot be treated by "mysql_real_escape_string".
How can I make the second function handle the values that I got from the first function?
Any responses appreciated.
Thank you in advance.
Your first mistake is to use mysql_fetch_assoc when only selecting one column. You should use mysql_fetch_row for this. This is likely going to fix your primary problem.
Could look like this:
$subs = get_subscribitions($whateverId);
$texts = get_text($subs);
function get_subscribitions($user)
{
$user = mysql_real_escape_string ($user);
$sql = "SELECT 'user_id' FROM `subscribe` WHERE subscriber = '$user'";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_row($result)) {
$user_id = $row['user_id'];
$rows[$user_id] = $user_id;
}
mysql_free_result($result);
return $rows;
}
function get_text($writer) {
$writers = implode(",", $writer);
$sql = "SELECT * FROM `text` WHERE user_id IN ({$writers}) ORDER BY timestamp DESC";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
mysql_free_result($result);
return $rows;
}
This will save you a lot of time, because you can get all data from 'text' in one statement.
The solution is to avoid placing arrays in your $rows array in the first function. Instead of:
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
try:
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row['user_id'];
}
This will place only the value from column 'user_id' in the $rows array.
In order to use the second function you must iterate over the array returned from the first one. Something like this could work for you:
$user_subscriptions = get_subscribitions($user);
foreach($user_subscriptions as $subscription) {
$texts = get_text($subscription['user_id']);
foreach($texts as $text) {
// do something with the fetched text
}
}
As George Cummins says,
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row['user_id'];
}
and, to speed up the second function:
function get_text($writer)
{
$sql = "SELECT * FROM `text` WHERE user_id in (".implode(',',$writer).") ORDER BY timestamp desc";
$rows = array();
if ($result = mysql_query($sql))
{
while ($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
}
mysql_free_result($result);
}
return $rows;
}
The change to the query means that you only do one in total rather than one for each ID thus removing the time taken to send the query to the server and get a response multiple times. Also, if the query fails, the function returns an empty array
Use :
string implode ( string $glue , array $pieces )
// example, elements separated by a comma :
$arrayasstring = impode(",", $myarray);
function get_subscribitions($user)
{
$user = mysql_real_escape_string ($user);
$sql = "SELECT 'user_id' FROM `subscribe` WHERE subscriber = '$user'";
$result = mysql_query($sql);
$row = mysql_fetch_row($results);
mysql_free_result($result);
return intval($row['user_id']);
}
it return only the user id you can used it in the 2nd function