Jshee
Jshee

Reputation: 2686

Php Error - Not sure where it is coming from or what it means?

<?php
$filename="vast/Vastopolis_Map.png";
$image = imagecreatefrompng($filename);

// get properties of the image as an array $size
// $size[0] is the width of the image in pixel
// $size[1] is the height

$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];

// Set the color (red)
$ink_color = imagecolorallocatealpha ($image, 244, 1, 1, 100);
// Set line thickness if needed
// imagesetthickness($image, 5);

// get GPS locations where text meets selection criteria
// Connect to MySQL, assume the database is called vast
$dsn = 'mysql:host=localhost;dbname=vast';
$username = '###';
$passwd = '###';

$db = new PDO($dsn, $username, $passwd);

// Select text that mentions flu or fever
// Option 1: text like '%flu%' or text like '%fever%'
// Option 2: text regexp 'flu|fever'
$sql = "SELECT Location, Text FROM blogs WHERE Text LIKE '%flu%' '";
$results = $db->query($sql);

// Linear transformation to match the coordinates on the map
// The upper left corner of the map: 42.3017, 93.5673
// The lower right corner of the map: 42.1609, 93.1923
$minX = 42.3017;
$maxX = 42.1609;

$minY = 93.5673;
$maxY = 93.1923;
// Linear transformation. What does it do?
$a = $width/($maxX-$minX);
$b = -$minX*$a;

$c = $height/($maxY-$minY);
$d = -$minY*$c;

// Set the color of the markers - red
$color = imagecolorallocatealpha ($image, 250, 10, 10, 50);

// plot each location returned from the query to the database
foreach ($results as $row) {
$location = $row[0];
$pos = preg_split("/\s+/", $location); // why?
$X = $pos[0];
$Y = $pos[1];
$x = $a*$X + $b;
$y = $c*$Y + $d;
imagefilledellipse($image, $x, $y, 10, 10, $color);
}
// serve the image to the client
header ("Content-type: image/png");
imagepng($image);
imagedestroy($image);

?>

it is throwing this disgusting error, anyone??

Warning: Invalid argument supplied for foreach() on line 51

Warning: Cannot modify header information - headers already sent by  on line 61
‰PNG  IHDR` ]×ý{~÷PLTEôú ûûûèæÙãÒìæÓÔÖçæËäÎÑÐêÒÐÑæï®âæšìέéšÈê³ØâšËÔ¯ËËä¼åæ·ËзæˬÏÃÆ殭㸑ä æŽÏ®¯Ð­‰Ç—¬Æ‘Œºãó»ãÒ·Èí±ÒÆË÷˜Èù䫷⚮̭ªÌ‘›Â±šÄŠ¨Ï­­®­²®‹®°Œ’´§¬Ž’§‹‹ÄÅyä­yæ€}ò‘IÌ¥yõYĘpÃ[ùƒ+Ê8®Â{¬¬m¤¥Z±i®„X‰¬q™¢Y‹“nŽP¨¢;¤‚<‘ >„ˆ;„å}Är¦É}ƒ{ͤ|¤§v¦\¡©VŒ–v¤mX¨…\…‘2‡çppð`^ç\bêPQÔpqÊnVÒ]bÔQRíy%þjë@>ìZÌr'ÒiØA>ÍX ð=CÔ>Bë34ñ!þ"ý   Õ67Å:̧rn¥yQ¢\f«RSnm‘mLŠYo†YL£v;´e«\+±XŽf:ƒm†Y5ŒT®=F†2m„:L‚m­/1®'¾‰2+‚'Ž ƒ rÀúT¤ázª¤|¢{ˆ¢s“‰P¥¥U¨‰O‡¯[ˆƒ“þ-ƒ¸-‘¶……z£ix¡[qllŽT[¡n_ C[ƒrZ…Lxƒ:iƒU…:Sƒ2‡m4„W‚w…N2ƒg`Õsk³qsˆdZ´tU†Jx©[iƒUK±UMŽC;Ùj0‡d‡E9¬H8\ {È&mŸ$á3)®3'—&®&”­‘mmpltPmRnqQNVpmToLSUkOONqn6~jrO0kHNm5Z}NT0NNfde:.f:p#~R4/N0L-C8kf-lG8Kf4PN~~lDJ~GH*g7&~2R-/Md8sU-G63c25M#}5Jtéã°ER¶ÁSÆ3tŸwC×ëx<ñ“ÏɼøÇvæßžpvü0#V>¶[û]oGÿÏÓc{ÚhEõlóï‡1g çFû|‡Úà®–\¿^o×ö~mÏãO?ÿô`Åè^<ë®òe{öØÓ>°ý~ק íøszŠè꾪nz¤>â<äø°^÷:̶óu;ö]…:þtÜöUÍ’«6QvKn|@ÉÖL¸ëoêk8|`ítSû9«°\Ú³Ï'BÆ•¥5ڪ㎨_tû «¬tøíúív½¶;ÿÝow>»À›íÃÃÑÊÓuµýò𪪶Gœ€Õvª?”è”;Ee§`þm{az`Ÿ­åzÄyí1"‹žÝÑÎ÷ÓqTÓXÕvÖKí÷

Not sure if this is a db error, or something with php. Completely lost why its doing this or what its talking about.

Upvotes: 1

Views: 95

Answers (6)

JiminP
JiminP

Reputation: 2142

I know nothing about PDO, but I can tell that $results is PDOStatement, not an array.

I think you may use some method like fetch... not foreach...

Upvotes: 1

Riba
Riba

Reputation: 1118

Try print_r($db->errorInfo()); to get more info about a possible mysql error.

Upvotes: 1

lock
lock

Reputation: 6614

It means your $db->query($sql); returned an empty result set.

Upvotes: 2

shmeeps
shmeeps

Reputation: 7833

You have an stray ' in your query, which makes it invalid

$sql = "SELECT Location, Text FROM blogs WHERE Text LIKE '%flu%' '";

should be

$sql = "SELECT Location, Text FROM blogs WHERE Text LIKE '%flu%'";

A malformed query will make $results an error code instead of an array of results. Since there are no results in $results, you can't use foreach on it without errors, as foreach can only be used on arrays.

Since the foreach causes an error, which prints and send to the user, it will generate the "headers already sent" error. Which the cause, and the effect will be solved too.

Upvotes: 1

Homer6
Homer6

Reputation: 15159

$results is not an array. You can ensure that it's the right type by using var_dump or is_array.

Cheers

Upvotes: 1

Sabeen Malik
Sabeen Malik

Reputation: 10880

When the warning is printed , it sends the header. Stop the foreach warning the error would go away.

Upvotes: 1

Related Questions