I am trying to find some beans by using "IN" as well as "=". I am currently using this code:
$ids = array(1,2,3,4);
$user = 1;
$things = R::find(
'thing',
'id IN ('.R::genSlots($ids).') AND user = ?',
array(
$ids,
$user
)
);
This gives me some errors:
PHP Notice: Array to string conversion in rb.php on line 217
Fatal error: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens thrown in rb.php on line 267
If I run this code with one item in $id I only get the notice but either way I get no results.
I'm assuming it's trying to treat $id as a single variable. What am I missing?
don't add the $ids to the array, merge the two into one array, otherwise this will end up as a nested array.
Related
I have the following value which is returned via API :
[{"message":"No transactions found"}]
this information comes back as a string and I would like to convert it to a JSON or an array.
I have the following two options :
$decoded_msg = json_decode($mpesa_offline_status,TRUE);
echo $decoded_msg['message'];
which return a warning message : Message: Illegal string offset 'message' and this option also:
$decoded_msg = json_decode($mpesa_offline_status,TRUE);
echo $decoded_msg->message;
and I get the following error :
Message: Trying to get property 'message' of non-object
how can I approach this?
You have an array of messages, so you cannot access 'message' at the top level.
$mpesa_offline_status = '[{"message":"No transactions found"}]';
$decoded_msg = json_decode($mpesa_offline_status, true);
var_dump($decoded_msg[0]['message']);
you have to access a valid index. Same with that:
$mpesa_offline_status = '[{"message":"No transactions found"}]';
$decoded_msg = json_decode($mpesa_offline_status, false);
var_dump($decoded_msg[0]->message);
Having [] with this API response means, when you use json_decode() this will generate a multi dimensional array, here [] will be converted into 0 index and message will be associated with 0 index something like:
Array
(
[0] => Array
(
[message] => No transactions found
)
)
For this if you want to access message index which inside the 0 index, you can access as like:
<?
$string = '[{"message":"No transactions found"}]';
$decode = json_decode($string,true);
echo $decode[0]['message']; // No transactions found
?>
Side Note:
Message: Illegal string offset 'message' error doesn't mean, to switch on to property instead of array, this message means, you are trying to access a string value which is an array.
I use this code for Virtuemart:
$product_id_to_remove = 3;
$cart = json_decode($_SESSION['__vm']['vmcart']);
foreach($cart->cartProductsData as $k => $v){
if($v->virtuemart_product_id == $product_id_to_remove) unset($cart->cartProductsData[$k]);
}
$_SESSION['__vm']['vmcart'] = json_encode($cart);
but I get the fatal error: Cannot use object of type stdClass as array in ... line 4. If I add true at json_decode($_SESSION['__vm']['vmcart']) I get the warning: Invalid argument supplied for foreach().
How to resolve the problem?
p.s. I'm beginner in php and don't know json_ at all. The code is suggested by the link: stackoverflow.com/questions/28691203/how-to-remove-a-single-product-from-mod-virtuemart-cart
$cart->cartProductsData behaves like an array but it's actually an object
try this:
change
unset($cart->cartProductsData[$k])
to
unset($cart->cartProductsData->$k)
Iam newbie to php and codeigniter.
I have error in php,
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/search.php
Line Number: 44
Line 44 is: foreach ( $data as $key => $value )
There is some syntax error here,
inside this if statement, if (!empty($user['search'])), $data is printed correctly,whereas in this if statement,if(!empty($data)), $data is not printed. Can anyone help me resolve this issue.
The problem is you don't have any value inside your looping variable( $data ).
You can print their values for check whether the variable contains values.
Replace :
$data = json_decode($user['search'])->result;
By :
$data = json_decode($user['search'], true);
json_decode works that way :
var_dump(json_decode($json)); // returns object(stdClass)
var_dump(json_decode($json, true)); returns array
Your $data was an object, while foreach strictly wants an array.
Original SQL query is this;
SELECT id,post_title,post_date FROM wp_posts where id='1'
When I retrieve the record, I am finding it but when it comes to returning the results, I am puzzled. Here is the where I got stuck.
while ($row = mysql_fetch_assoc($RS)) :
print_r ($row);
list($id,$post_title,$post_date) = $row;
endwhile;
print_r ($row) outputs this;
Array ( [ID] => 1 [post_title] => Hello world! [post_date] => 2012-03-27 03:28:27 )
And when I run the list function in there ( for debug purposes obviously ), I get this;
Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147
What's causing this?
Replace:
mysql_fetch_assoc($RS)
with:
mysql_fetch_array($RS, MYSQL_NUM)
then it should work, because the list function trys to access the array using numeric keys.
I guess the answer lies somewhere within this;
list() only works on numerical arrays and assumes the numerical indices start at 0.
:(
You might be able to use extract() here instead, as well; (documentation here.)
You used mysql_fetch_assoc, so the resulting array per row has data under a key by column name, whereas "list" tries to match variables to values using numerical array indexes. You can use mysql_fetch_array instead.
$categ = val1 | val2
list($one,$two,$three)=#split('[|]',$categ);
If you try to list the value which is not available it will return the error Undefined Offset.
Here the error will be Undefined Offset 2.
Because while spliting $categ, it will have only two values, if you try to access third value then it will return error.
I am Creating A facebook that Retrieves 10 Random friends.But I need some code to Retrieve Top friends using comment and like ativity.I used Following code but i get below error
Invalid argument supplied for foreach()
below is the code i tried so far.
$statuses = $facebook->api('/me/statuses');
foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase.
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$frname = $likesData['name'];
$friendArray[$frid] = $frname;
}
foreach($status['comments']['data'] as $comArray){
// processing comments array for calculating fanbase
$frid = $comArray['from']['id'];
$frname = $comArray['from']['name'];
}
That error message generally happens when your array variable is not set. If you could add line numbers to your code and give the full error message (including line number) it might help.
Can you show a print_r of $statuses['data']?
I've been trying to get this to work myself. Found the solution. $facebook->api() is going to return a json array. This is not a valid element for a foreach() statement. You need to use json_decode($statuses) in order to loop through the array in the foreach() statement.
Foreach through JSONArray in PHP