Less intensive way to pull data from an object?

Toodles, y’all! o/ Back again, with a quick question: Is there a less memory intensive way to pull data from an object than the $object->data way? I’m only echoing this about three times in my script before I get the “Allowed memory size of ###### bytes exhausted (tried to allocate ### bytes)” error. I’ve debugged it and it definitely is due to the pulling of data from the $object. Is there any simpler way to retrieve data, or am I just going to have to increase the memory allowance?

Cheers,

Here’s the code, if that helps.

[php]$i = 0;

	while ($i < $result->num_rows)
	{
		$record = $result->fetch_object();
		$companyName = strtolower($record->company_name);
		
		$searchFor = "/";
		$searchFor .= strtolower($id);
		$searchFor .= "/";
		if(preg_match($searchFor, $companyName))
		{
			echo "<html><body><table><tr>";
			echo "<td>" . $companyName . "</td>";
			echo "<td>" . $record->Address2 . "</td>";
			echo "<td>" . $record->Address3 . "</td>";
			echo  "</table></body></html><button type='button'>See More</button>";
		}
		$i += 1;
	}[/php]

The issue isn’t pulling data from the object, it is that you are fetching data multiple times and not understanding what is occurring.

A foreach loop would work just fine in that snippet and give you what you are after, without running
$record = $result->fetch_object();

multiple times.

I was fooling around with the world database that you can find at php.net and came up with this simple search. This should give you an idea on how to implement your search.

[php]<?php
include ‘lib/includes/connect/connect.php’;

$countryCode = filter_input(INPUT_POST, ‘countryCode’, FILTER_SANITIZE_FULL_SPECIAL_CHARS); // Search by Country Code:

$db_options = array(
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/
throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/
fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
);
$pdo = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=’ . DATABASE_NAME . ‘;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);

$query = ‘SELECT Name, CountryCode, District, Population FROM city WHERE CountryCode=:CountryCode’; // Set the Search Query:

$stmt = $pdo->prepare($query); // Prepare the query:
$stmt->execute([’:CountryCode’ => $countryCode]); // Execute the query with the supplied user’s parameter(s):
?>

Database Search #hor-minimalist-a { font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 12px; background: #fff; margin: 45px; width: 480px; border-collapse: collapse; text-align: left; } #hor-minimalist-a th { font-size: 14px; font-weight: normal; color: #039; padding: 10px 8px; border-bottom: 2px solid #6678b1; } #hor-minimalist-a td { color: #669; padding: 9px 8px 0px 8px; } #hor-minimalist-a tbody tr:hover td { color: #009; } search <?php while($record = $stmt->fetch()) { echo ""; echo '"; echo '"; echo '"; echo '"; echo ""; } ?>
City Country Code District / State Population
' . $record->Name . "' . $record->CountryCode . "' . $record->District . "' . $record->Population . "
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service