Getting a "Cannot use object of type stdClass" error

The following script gives me the error: Cannot use object of type stdClass as array in /Users/barryhjames/Sites/stwilliamtheabbot/genesis/admin/general/organization.php on line 41. Line 41 starts with:

$organization = $row->value;

Here is the code that I am using:

$mysqli = new mysqli('localhost', 'stwilliamtheabbot', '*******************', 'genesis');
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='organization'")) {
                while ($row = $result->fetch_object()) {
                    $organization = $row->value;
                } 
            } else {
                echo $mysqli->error;
            } // end else

            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='address_1'")) {
                while ($row = $result->fetch_object()) {
                    $address_1 = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='address_2'")) {
                while ($row = $result->fetch_object()) {
                    $address_2 = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='city'")) {
                while ($row = $result->fetch_object()) {
                    $city = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='state_region'")) {
                while ($row = $result->fetch_object()) {
                    $state_region = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='zip_code'")) {
                while ($row = $result->fetch_object()) {
                    $zip_code = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            if ($result = $mysqli->query("SELECT value FROM general WHERE setting='country'")) {
                while ($row = $result->fetch_object()) {
                    $country = $row->value;
                }
            } else {
                echo $mysqli->error;
            } // end else
            
            $mysqli->close();

Any ideas? I’m not even sure what this error message means?

Does your table have a column named ‘value’? (Very close to hitting a reserved word there. be careful.)’

Other than that… I dont see an array reference there. are you sure that’s line 41?

I think the stdClass error is the least of your problems, your code is horribly inefficient, if $result->fetch_object() returns more than one row, you’ll over write the variable on each iteration, if it only returns 1 row, why are you using a while loop?
Also your schema looks very strange for storing address details.

StarLion:

Yes, the column name is “value.” Not sure if you think I should change that or not. I realize it might be close to a reserved word.

blain:

I haven’t gotten to the part yet where I validate all of my variables yet. Just trying to get the function to work. I’m new to using mysqli, and I am just trying to figure it all out. Any advice that you might give me would be appreciated.

The table that I created is a general settings table, so there are three columns: “id,” “setting” (which is the name of the setting) and “value” (which is the value of the setting). I shouldn’t use a loop, you are right, because there will only ever be one row in the table. What should I use to just call on one row?

As for the way I am storing addresses, I am looking for the addresses to represented as follows:

organization
address_1
address_2
city, state_region zip_code country

Do you think I should have any additional fields?

Ok, I have changed my code to the following:


$query = "SELECT value FROM general WHERE setting='organization';";
$query .= "SELECT value FROM general WHERE setting='address_1';";
$query .= "SELECT value FROM general WHERE setting='address_2';";
$query .= "SELECT value FROM general WHERE setting='city';";
$query .= "SELECT value FROM general WHERE setting='state_region';";
$query .= "SELECT value FROM general WHERE setting='zip_code';";
$query .= "SELECT value FROM general WHERE setting='country'";
		
if ($mysqli->multi_query($query)) {
	do {
		// Store first result set 
        	if ($result = $mysqli->store_result()) {
			while ($row = $result->fetch_row()) {
				printf("%s\
", $row[0]);
			}
			$result->free();
		}
    	} while ($mysqli->next_result());
}

… but I can’t seem to get it working. What I want to do is save each one of the results that come from each of the SELECT queries into a variable, but I can’t figure out how to do this. It successfully prints the array to the screen, but how do I split this row into individual values I can store into variables? For example, I would like to be able to create the following variables from the results…

$organization
$address_1
$address_2
$city
$state_region
$zip_code
$country

Try:


$query = "SELECT setting,value FROM general"; 
$res = $mysqli->query($query);
while($row = $res->fetch_row()) {
  $$row[0] = $row[1];
}

(and yes, the $$ is intentional.)

StarLion:

How do I go about using the code above to save those values into individual variables, though? And could you explain to the new guy about the “$$” and why you need two of them?

An update:

I ended up getting the section above working like so:


$query = "SELECT setting, value FROM general";
$result = $mysqli->query($query); 	
while($row = $result->fetch_all()) { 
  	$organization = $row[0][1];
  	$address_1 = $row[1][1];
  	$address_2 = $row[2][1];
  	$city = $row[3][1];
  	$state_region = $row[4][1];
  	$zip_code = $row[5][1];
  	$country = $row[6][1];
  }
  $result->free();

Thanks for all of your help!