php: unable to iterate for each loop - php

I am facing the follow error from the below code:
Error:
Warning: Invalid argument supplied for foreach()
Code:
//--------This is the code for jquery UI autocompleter-------------
include "inc/connect.php";
$skeyword = $_GET["term"];
$req = "SELECT p.prm_title ,a.am_name, c.cm_name ,l.lm_name ,b.bm_name,r.pm_name "
. "FROM product_master p,author_master a,category_master c,language_master l,binding_master b ,publisher_master r "
. "WHERE p.prm_title LIKE '%$skeyword%' OR a.am_name LIKE '%$skeyword%'
OR c.cm_name LIKE '%$skeyword%' OR l.lm_name LIKE '%$skeyword%' OR b.bm_name LIKE '%$skeyword%' OR r.pm_name LIKE '%$skeyword%'
GROUP BY p.prm_id LIMIT 10";
$res = mysql_query($req);
$ret = array();
foreach (mysql_fetch_array($res) as $row) {
foreach ($row as $val) //--Error: from this line, Why?
{
if (false !== stripos($val, $skeyword)) {
$ret[] = $val;
break;
}
}
}
$testme = json_encode($ret);
echo $testme;
The above code is written for jquery auto-completer to search in many column field, but it will return only the matched column.
Please help me to solve this issue..

why:
If a variable:
is not array
does not implements the interface Iterator
and it is used to iterator by foreach, this error will raise.
How to fix
mysql_fetch_array returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows, so use a while loop to fetch result:
while ($row = mysql_fetch_array($res))
{
// if $row is false, the code will not hit here.
foreach ($row as $val)
{
if (FALSE !== stripos($val, $skeyword))
{
$ret[] = $val;
break;
}
}
}
update:
while ($row = mysql_fetch_array($res))
{
// if $row is false, the code will not hit here.
foreach ($row as $val)
{
if (FALSE !== stripos($val, $skeyword) && !in_array($val, $ret))
{
$ret[] = $val;
break; // what this break for?
}
}
}

Please try this.
Use while instead of foreach
//--------This is the code for jquery UI autocompleter-------------
include "inc/connect.php";
$skeyword = $_GET["term"];
$req = "SELECT p.prm_title ,a.am_name, c.cm_name ,l.lm_name ,b.bm_name,r.pm_name "
. "FROM product_master p,author_master a,category_master c,language_master l,binding_master b ,publisher_master r "
. "WHERE p.prm_title LIKE '%$skeyword%' OR a.am_name LIKE '%$skeyword%'
OR c.cm_name LIKE '%$skeyword%' OR l.lm_name LIKE '%$skeyword%' OR b.bm_name LIKE '%$skeyword%' OR r.pm_name LIKE '%$skeyword%'
GROUP BY p.prm_id LIMIT 10";
$res = mysql_query($req);
$ret = array();
while ($row = mysql_fetch_array($res))
{
foreach ($row as $val) //--Error: from this line, Why?
{
if (FALSE !== stripos($val, $skeyword))
{
$ret[] = $val;
break;
}
}
}
$testme = json_encode($ret);
echo $testme;

while ($row = mysql_fetch_array($res)) {

Again, but now as an answer:
Change your first foreach into a while loop. See the mysql_fetch_array manual

Please always verify that the parameter for the "foreach" is an array.
Your SQL request may return FALSE if there is no row matching your conditions, and foreach on FALSE does not work :)
So after your code
foreach (mysql_fetch_array($res) as $row)
You have to verify that $row is an array and not false before trying to iterate over it.
if (is_array($row))
Or you may be more explicit and use mysql_num_rows, which tells you if the query returns some rows or not :
if (mysql_num_rows($res) > 0)
{
$ret = array();
foreach ( mysql_fetch_array($res) as $row)
// ...
}
But the right solution, as exposed above by djot and Amol, is to use the
while ($row = mysql_fetch_array($res))
which ensure that if $row is FALSE, then you won't enter the block and you won't try to iterate on FALSE.

Related

Making an Array from Database MySQL

I have database connection. I want to make an array but something is problem i think. Here is my array code:
$var = "SELECT SUBSTRING(KayitTarihi,1,4) AS year,SUBSTRING(KayitTarihi,6,2) AS month,SUBSTRING(KayitTarihi,9,2) AS day,SUBSTRING(KayitTarihi,12,2) AS saat,SUBSTRING(KayitTarihi,15,2) AS dakika,Guc FROM Urun WHERE Date(KayitTarihi)=\"".$link_m."\"";
$result = $mysqli->query($var);
$data = array();
foreach ($result as $row) {$data[] = $row;}
print_r($data);
$no=1;$total_deger=count($data);
echo $total_deger;
for($i=0;$i<$total_deger);$i++){
$xxx[i]="[Date.UTC(".$data[i]['year'].",".$data[i]['month'].",".$data[i]['day'].",".$data[i]['saat'].",".$data[i]['dakika'].",00),".$data[i]['Guc']."]";if($no < $total_deger){echo ",";}
echo $xxx[i];
}
When I run this code, nothing happens in page. When I write to for example 0 for i. I can see my array value. Where do I mistake?
Code with correction and suggestion (both are commented):-
<?php
error_reporting(E_ALL); // check all errors
ini_set('display_errors',1);// display those errors
$var = "SELECT SUBSTRING(KayitTarihi,1,4) AS year,SUBSTRING(KayitTarihi,6,2) AS month,SUBSTRING(KayitTarihi,9,2) AS day,SUBSTRING(KayitTarihi,12,2) AS saat,SUBSTRING(KayitTarihi,15,2) AS dakika,Guc FROM Urun WHERE Date(KayitTarihi)=\"".$link_m."\"";
$result = $mysqli->query($var);
$data = array();
if ($result->num_rows > 0) { // check you got results or not
while($row = $result->fetch_assoc()) {
$data[] = $row; // assign them
}
}
//foreach ($result as $row) {$data[] = $row;} // not needed
print_r($data);
$no=1;
$total_deger= count($data);
echo $total_deger;
for($i=0;$i<$total_deger;$i++){ // remove )
$xxx[$i]="[Date.UTC(".$data[$i]['year'].",".$data[$i]['month'].",".$data[$i]['day'].",".$data[$i]['saat'].",".$data[$i]['dakika'].",00),".$data[$i]['Guc']."]"; // use $i instead of i
if($no < $total_deger)
{
echo ",";
}
echo $xxx[$i];
}
Note:- Always add some error reporting code. So that all errors will populated and you can rectify those
Also better would be to use foreach() instead of for loop (as it will take care of indexes itself):-
//use foreach
foreach ($data as $dat){
echo "[Date.UTC(".$data['year'].",".$data['month'].",".$data['day'].",".$data['saat'].",".$data['dakika'].",00),".$data['Guc']."]";
}
Firstly you want to check your SQL query returns rows. Then I think you need to modify your foreach to a while as below. Also checking that rows are returned before looping.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
As per example given here: http://www.w3schools.com/php/php_mysql_select.asp

Using urldecode on a mysql_fetch_array Array

I have been trying to convert the fields from a mysql_fetch_array (that are urlencoded) to urldecode before converting to JSON (json_encode)
Here's what I'm working with that doesn't work:The output is still urlencoded
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_fetch_array(mysql_query($query));
foreach($result as $value) {
$value = urldecode($value);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
Any ideas?
yeah....! you're not updating $result with the value returned by the function. $value needs to be passed by reference.
foreach($result as &$value) {
$value = urldecode($value);
}
or
foreach($result as $i => $value) {
$result[$i] = urldecode($value);
}
when you do this...
foreach($result as $value) {
$value = urldecode($value);
}
The result of the function is lost at at iteration of the foreach. You're trying to update each value stored in $result but that's not happening.
Also take note that the code only fetches one row from your query. I'm not sure if that's by design or not.
Try:
$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_query($query);
$value = array();
while($row = mysql_fetch_array($result))
$value[] = urldecode($row);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));

If a row is not unique - ignore it - php

I'm working on the following code:
function form_Subcat_Picker() {
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('There was a problem connecting to the database.');
}
$catPicker = "SELECT Subcatid, Subcatname, Parentid
FROM ProductSubCats
ORDER BY Subcatid";
if ($Result = $mysqli->query($catPicker)){
if (!$Result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while ($row = $Result->fetch_assoc()) {
echo '<div class="parentid'.$row['Parentid'].'">';
echo '<select name="Subcatid">';
echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
echo '</select>';
echo '</div>';
}
}
$mysqli->close();
}
What I want to do, is in the line:
while ($row = $Result->fetch_assoc()) {
echo '<div class="parentid'.$row['Parentid'].'">';
If the $row['Parentid'] part is the same as the previous iteration, I want to ignore that particular line (adding the div class)
So that if for example in the first run $row['Parentid'] is 1, and in the next loop it is 1 again, I want to not create a new div, just echo everything else and thus keep it in the same div.
Is this possible? Alternatively, how could I make multiple sub category id's and names appear in the one div, if they share a common parentid (there are multiple parent ids)
For the line:
echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
Maybe this would work:
$last_id = 0;
while ($row = $Result->fetch_assoc()) {
if ($last_id != $row['Parentid']) {
echo '<div class="parentid'.$row['Parentid'].'">';
echo '<select name="Subcatid">';
echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
echo '</select>';
echo '</div>';
$last_id = $row['Parentid'];
}
}
However, I think the best solution is to filter them out in the SQL statement, maybe the GROUP BY clause, but I'm not 100% sure how to do it :).
Regards,
That is just something basic for looping. Let's see your current loop:
while ($row = $Result->fetch_assoc()) {
...
}
As you want to skip with a specific condition, let's just introduce this skipping (not taking much care about the condition first):
while ($row = $Result->fetch_assoc()) {
if ($condition) continue;
...
}
Now let's formulate the condition. As we want to look into the last $row we need to keep a copy:
$last = null;
while ($row = $Result->fetch_assoc()) {
if ($condition) continue;
...
$last = $row;
}
Now we've got the data we need to have to create the condition, $last can contain the last row (if there was one) and therefore the comparison can be done:
$last = null;
while ($row = $Result->fetch_assoc()) {
$condition = $last && $row['Parentid'] === $last['Parentid'];
if ($condition) continue;
...
$last = $row;
}
And that is it basically. Depending on the logic, you might want to switch to a for loop:
for ($last = null; $row = $Result->fetch_assoc(); $last = $row) {
$condition = $last && $row['Parentid'] === $last['Parentid'];
if ($condition) continue;
...
}
This for example does ensure that for each iteration (even the skipped ones), the $last is set to the $row at the end of the loop.
Instead of continue you can naturally do different things, like not outputting the <div> or similar.
Hope this helps.
This is the way I'd write it.
// add a variable to hold the previous value
$previous_parent_id = "";
while ($row = $Result->fetch_assoc()) {
// then add an if statement to see if it's the previous statement
if ($row['parent_id'] != $previous_parent_id){
echo '<div class="parent_id'.$row['parent_id'].'">';
$previous_parent_id = $row['parent_id'];
}
}
So going in a loop on these records
ID ParentID
1 0
2 0
3 1
4 1
4 2
4 2
the output would be:
<div class="parent_id0">
<div class="parent_id1">
<div class="parent_id2">

looping in associative array with column name

I created associative array as below
$query = "SELECT word,meaning1,meaning2 FROM dictionary";
$results = $db->exeQuery($query);
$arrDictionary = array();
while($line = mysql_fetch_array($results, MYSQL_ASSOC))
{
$arrDictionary[] = $results;
}
Then i am iterative through another list of word array
foreach ($file_array as $value)
{
}
within this loop, for each word, i need to find meaning1 and meaning2. Tried many ways, but no success.
Could somebody help me?
I think
while($line = mysql_fetch_array($results, MYSQL_ASSOC))
{
$arrDictionary[] = $results;
}
should be replaced with
while($line = mysql_fetch_assoc($results))
{
$arrDictionary[] = $line;
}
and
foreach ($arrDictionary as $value)
{
echo $value['field_name'];
}

How to index the result of a mySql query as an array of array?

If I need to select and use information of every element of a table in a database the procedure would be this:
$query = "...mySql query...";
$query_result = mysql_query($query) or die (mysql_error());
Then if I wished to access the fields of the result I would use the function mysql_fetch_array() and access them like this:
$query_result_array = mysql_fetch_array($query_result);
echo $query_result_array['field_1'];
....
echo $query_result_array['field_i'];
....
But since more elements could be returned by the query I would like to access every single of them with an array indexed from 0 to mysql_num_rows($query_result).
As an example:
echo $query_result_array['field_i'][0];
....
echo $query_result_array['field_i'][mysql_num_rows($query_result)];
should print for every selected element of the table the value of field i.
Is there a function that will do the job for me?
If not, any suggestions on how to do it?
Thanks in advance for help.
This may be an alternative
$res = mysql_query("..SQL...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$arr[] = $row;
}
var_dump($arr);
Or
$res = mysql_query("..SQL...");
for
(
$arr = array();
$row = mysql_fetch_assoc($res);
$arr[] = $row
);
var_dump($arr);
I don't think there is such a method; you have to do it yourself.
try with something like:
$res = mysql_query("..mySql query...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$query_result_array[] = $row;
}
then you access your data like:
echo $query_result_array[0]['field_i'];
based on 2 previous answers, those authors assuming that usual SO author is familiar with such a thing as creating a function
function sqlArr($sql) { return an array consists of
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
if ($res) {
while ($row = mysql_fetch_assoc($res)) {
$ret[] = $row;
}
}
return $ret;
}
$array = sqlArr("SELECT * FROM table");
foreach ($array as $row) {
echo $row['name'],$row['sex'];
}
this resulting array have different structure from what you asked, but it is way more convenient too.
if you still need yours unusual one, you have to tell how you gonna use it

Categories