PHP Warning causing sql timeouts? - php

I got a whole heap of (foreach()) warnings on my server log. Could they be causing sql timeout on my db via a sql connection leak?
I'm not entirely sure how to resolve the problem but i read that i need to add this bit of code:
if (is_array($values) || is_object($values))
{
foreach ($values as $value)
{
...
}
}
...into my .php but dont really know where to add it or why? sorry noob here again.
Here's my code section for: PHP Warning:
Invalid argument supplied for foreach() in
/hermes/bosnacweb03/bosnacweb03aa/b1245/ipg.tinsflowershop/panga/classes/ImageType.php
on line 148
(Line 148 starts - foreach ($results as $result) {)
/**
* Finds image type definition by name and type
* #param string $name
* #param string $type
*/
public static function getByNameNType($name, $type = null, $order = 0)
{
static $is_passed = false;
if (!isset(self::$images_types_name_cache[$name.'_'.$type.'_'.$order]) && !$is_passed) {
$results = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'image_type`');
$types = array('products', 'categories', 'manufacturers', 'suppliers', 'scenes', 'stores');
$total = count($types);
foreach ($results as $result) {
foreach ($result as $value) {
for ($i = 0; $i < $total; ++$i) {
self::$images_types_name_cache[$result['name'].'_'.$types[$i].'_'.$value] = $result;
}
}
}
$is_passed = true;
}
I assume that all the warnings on my log are the same issue? eg:
PHP Warning: Invalid argument supplied for foreach() in
/hermes/bosnacweb03/bosnacweb03aa/b1245/ipg.tinsflowershop/panga/classes/Category.php
on line 594

Related

What is wrong with the lines? (php)

I get these error from nowhere... What is wrong?
Warning: Division by zero in
/home//public_html/wp-content/plugins//includes/classes/controller/SearchPageController.php
on line 60
Warning: Invalid argument supplied for foreach() in
/home/vineanimals/public_html/wp-content/plugins/*/includes/classes/controller/ImportPageController.php
on line 70
Warning: array_merge(): Argument #1 is not an array in
/home//public_html/wp-content/plugins//includes/classes/controller/AImportPageController.php
on line 75
line codes:
line 60: $last = ceil($load_products_result['total'] / $load_products_result['per_page']);
line 70-75:
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}
A) Check your SQL query or Array feed
B) Then change
$last = ceil($load_products_result['total'] / $load_products_result['per_page']);
to:
$last = ( isset($load_products_result['per_page']) ? ceil($load_products_result['total'] / $load_products_result['per_page']) : 0)
C) and change the foreach as below:
if (is_array($product['sku_products']['variations'])){
foreach ($product['sku_products']['variations'] as $var) {
if (isset($var['image'])) {
$product['all_images'][] = $var['image'];
}
}
if (isset($product['description']) && is_array($product['all_images']) && is_array($this->woocommerce_model->get_images_from_description($product['description'])) )
$product['all_images'] = array_merge($product['all_images'], $this->woocommerce_model->get_images_from_description($product['description']));
}

JSON Property of non-object & Invalid argument supplied for foreach

I have a wordpress function that calls an API via Zebra_cURL and then uses json_decode to render the data appropriately. Yet there is erratic behavior and the following two errors are occurring: Trying to get property of non-object in [template file] on line 42 and through lines 50.
The lines in question are below:
//Course display callback function
function display_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$title = $result->body[0]->{'Title'};
$term = $result->body[0]->{'Term'};
$meetings = $result->body[0]->{'Meetings'};
$status = $result->body[0]->{'Status'};
$course_number = $result->body[0]->{'OfferingName'};
$clean_course_number = preg_replace('/[^A-Za-z0-9\-]/', '', $course_number);
$credits = $result->body[0]->{'Credits'};
$instructor = $result->body[0]->{'InstructorsFullName'};
$description = $result->body[0]->{'SectionDetails'}[0]->{'Description'};
}
And then later I get an error Invalid argument supplied for foreach() in [template file] on line 64 which is:
//Call callback function
function parse_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}
What exactly am I doing wrong here to cause these errors?
Here's a link to the full template file. Apologies for the vast code dump but I'm a bit in over my head with this.
You must always verify the output of functions/methods if the result if "as expected"
In your case, you did not check what json_decode() has returned
function parse_courses($result) {
$result = json_decode(html_entity_decode($result->body));
if ((!is_array ($result) && !is_object($result)) ||
(is_array($result) || count($result) == 0) ||
(json_last_error() != JSON_ERROR_NONE)) { // only for PHP >= 5.3.0
// log the error or warning here ...
// $input = $result;
// $output = print_r ($result, TRUE);
// Only for PHP >= 5.3.0
// json_last_error();
// json_last_error_msg();
return -1;
}
$result->body = $result;
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}

Warning: Illegal offset type in

I have some problem with this code. Warning: Illegal offset type in Line 22
$this->word[$kata][]=array($i,$j);
and the full code is below
private $jmldoc = 0; private $word = array();
public function getIndex($D) {
$this->jmldoc = count($D);
for($i=0; $i<$this->jmldoc; $i++) {
$pp = new prePro($D[$i]);
$kata = $pp->tokenize();
$n = count($kata);
for($j=0; $j<$n; $j++) {
$this->word[$kata]=array($i,$j);
}
}
}
Can you help me to fix it?
You are passing an array, not a string/integer index to your $this->word.
//I suppose from the context of your code that $kata is an array also
//so if that's true, it can't be used as an index
$this->word[$kata][]=array($i,$j);
Keep in mind that $this->word is an array. So probably there is something wrong with your program logic. To fix this, use an integer or string to access the elements of an array.

Fatal error when value empty

I have a fatal error claiming that I have a string when I thought I had an array :
Fatal error: [] operator not supported for strings in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 566
and also the following warning :
Warning: in_array() expects parameter 2 to be array, string given in /Applications/MAMP/htdocs/tankards_wordpress/wp-content/themes/tankardsleague/functions.php on line 579
I am guessing that I have either a syntax problem somewhere? I thought I had initailized the variables but maybe I missed one? I would appreciate some more experienced eyes having a look.
function forum_subscribe_member_player()
{
global $user_ID;
$players= get_users();
foreach($players as $player)
{
$user_info = get_userdata($player->ID);
$playeremail = $user_info->user_email;
if(!empty($playeremail) && user_can( $player-> ID, 'contributor'))
{
$list = get_option('mf_forum_subscribers_1', array());
if( is_player_subscribed($player->ID)) //remove player if already exists (user clicked unsubscribe)
{
$key = array_search($playeremail, $list);
unset($list[$key]);
}
else
$list[] = $playeremail;
update_option('mf_forum_subscribers_1', $list);
}
}
}
function is_player_subscribed($user_ID)
{
if($user_ID)
{
$useremail = get_userdata($user_ID, 'user_email');
$list = get_option("mf_forum_subscribers_1", array());
if(!empty($list) && in_array($useremail, $list))
{
return true;
}
return false;
}
}
function call_forum_subscribe_member_player()
{
forum_subscribe_member_player();
}
line 566 is $list[] = $playeremail; line 579 is if(!empty($list) && in_array($useremail, $list))
well, you can type-cast the $list to an array to make sure it's always an array even if the initial value is null/empty/etc (basically any type except array):
$list = (array)get_option('mf_forum_subscribers_1', array());

mysql error msg reading table data (Warning: Invalid argument supplied for foreach() in)

ive this table which is updated and re-ranked every midnight ,
after few days i start getting this msg
Warning: Invalid argument supplied for foreach() in
in last page of pagination to fix it i used to trunk the table and run my code again then its work fine , here is my mysql statement case the issue
mysql_free_result($result);
$i = 0;
$total_amount = count($tweeps);
foreach ($tweeps as $tweep) {
$i++;
if ($total_amount == $i) {
$class = 'divrow divrowlast';
} else {
$class = 'divrow';
}
need to tips to do permanent fix
Without seeing what $tweeps contains its hard to help. You are however missing a closing bracket. For practice you should check if $tweeps is in fact an array though. My assumption is that $tweeps isn't an array.
if (is_array($tweeps)) {
foreach ($tweeps as $tweep) {
$i++;
if ($total_amount == $i) {
$class = 'divrow divrowlast';
} else {
$class = 'divrow';
}
}
}

Categories