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;
Related
I am using the following code to show all tables, but how do I store the tables into a sorted array:
$sql = "SHOW TABLES FROM ".$counter_database." ORDER BY TABLES DESC";
$result = $conn->query($sql);
$outArray = array();
while($table = $result->fetch_assoc()) {
$outArrayx[] = $table;
}
The following code spits out the data:
foreach ($outArrayx as $key => $value) {
foreach ($value as $keys => $values) {
$sql = "SELECT * FROM ".$values." WHERE user_id='0'";
$result = $conn->query($sql);
if (strpos($values, "ckia") === 0) {
while($table = $result->fetch_assoc()) {
$outArray[] = $table;
}
}
}
}
Put the table names in a PHP array, and then sort the array. And if you don't nest it in the row, you don't need nested loops when you process it.
You can't use ORDER BY with SHOW TABLES, but you can use LIKE to select only tables that fit a pattern. Then you don't need the if statement in your loop.
$sql = "SHOW TABLES FROM ".$counter_database." LIKE 'ckia%'";
$result = $conn->query($sql);
$outArray = array();
while($table = $result->fetch_row()) {
$outArrayx[] = $table[0];
}
sort ($outArrayx);
foreach ($outArrayx as $table) {
$sql = "SELECT * FROM ".$table." WHERE user_id='0'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$outArray[] = $row;
}
}
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 need an SQL query that displays all the records if its duplicated also. For instance say
select * from table where true and p_id in(1,2,1,1)
displays only records from 1 and 2 but i need it to be repeated when given in while loop.
Update with code:
$cook = unserialize($_COOKIE["pro_cook"]);
foreach ($cook as $something) {
$merc[] = $something;
}
foreach ($size as $new_size) {
$size_array[] = $new_size;
}
$items = count($merc);
$mer = rtrim(implode(',', array_reverse($merc)), ',');
$fulclr = "and p_id in (".$mer.")";
$asd = "(p_id,".$mer.")";
$result = mysql_query("select * from product_details where true ".$fulclr." order by field".$asd."");
Hope this will help
$ids = "1,2,1,1";
$sql = "select * from table where true and p_id in (".$ids.")";
$rec = mysql_query($sql);
$dbData = array();
while($res = mysql_fetch_assoc($rec)) {
$dbData[$res['p_id']] = $res;
}
$ids = explode(',', $ids);
$newArray = array();
foreach ($ids as $id) {
if (!empty($dbData[$id])) {
$newArray[] = $dbData[$id];
}
}
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
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)