H. Jason
H. Jason

Reputation: 13

"Object of class stdClass could not be converted to String" php

for some reason $Value throws this error (Yes, I am sure about this, if I remove it from the query, the error is not thrown -- obviously this makes the code useless though, also if I try to assign $value to another variable before it also throws the error).

This is the var_dump() of $PLChecked if needed.

array(2) { [0]=> object(stdClass)#1 (1) { ["Plugin ID"]=> string(1) "2" } [1]=> object(stdClass)#2 (1) { ["Plugin ID"]=> string(1) "3" } }


$PLChecked = [];
foreach ($a as $b){

$checkNum = $;

$result = mysql_query("Select `cc` FROM `aa` WHERE `Checksum` LIKE '$checkNum'");

if (mysql_num_rows($result) == 1){

    $result = mysql_query("SELECT `ID` FROM `aa` WHERE `cc` LIKE '$checkNum' LIMIT 1");
    $value = mysql_fetch_object($result); //here i would like to get the selected ID
    $PLChecked[] = $value;
}
}


 foreach ($PLChecked as $Value)
{   

    //$a;
    $sqlano = "SELECT `Link` FROM `aa` WHERE `ID`LIKE '$Value'"; // This throws the error -- or at least that's what php says.
    //$resultawqe = mysql_query($sqlano);
    //$value = mysql_fetch_row(mysql_query($sqlano));

    //$PLLinks[] = $resultawqe;
}

Now, I have this exact code in another part of the code and it runs fine (actually the query is slightly different but it's fundamentally the same). I don't know why it does this.

By the way if it's something easy to fix please post a hint as the answer, later, when selected as correct, you can add the actual fix.

Thanks

Upvotes: 0

Views: 829

Answers (2)

Abid Nawaz
Abid Nawaz

Reputation: 956

May be you are looking for this.

if (mysql_num_rows($result) == 1){

    $result = mysql_query("SELECT `ID` FROM `aa` WHERE `cc` LIKE '$checkNum' LIMIT 1");
    $value = mysql_fetch_object($result); //here i would like to get the selected ID

     // if $value is object or array then you will loop through it like
    foreach($value as $val){
      $PLChecked[] = $val->ID; in case of object 
      $PLChecked[] = $val['ID']; in case of array
    }

}

 foreach ($PLChecked as $Value)
 {   

     // now variable $value will contain ID and the below query will work. 
     $sqlano = "SELECT `Link` FROM `aa` WHERE `ID`LIKE '$Value'"; 

 }

Upvotes: 0

girish
girish

Reputation: 301

The values which you add to the PLChecked array using foreach loop should be of type "string". Adding to PLChecked should be like $PLChecked[] = $value->ID instead of $PLChecked[] = $value; Because $value is an object and it contains the ID you selected from the query.

Upvotes: 0

Related Questions