pontypete Posted September 6, 2012 Share Posted September 6, 2012 $backlinkSources = array(); $result = mysql_query("SELECT website FROM `free_listings`"); while($row = mysql_fetch_assoc($result)) { $backlinkSources[] = $row; } print_r($backlinkSources); Returns these errors: Array ( [0] => Array ( [website] => http://www.safehandsplans.co.uk ) [1] => Array ( [website] => http://www.google.com ) ) Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39 Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45 Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39 Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45 Array ( ) Thanks in advance! Quote Link to comment Share on other sites More sharing options...
pontypete Posted September 6, 2012 Author Share Posted September 6, 2012 And this works... (but I need it feeding in from SQL) $backlinkSources = array ('http://www.safehandsplans.co.uk','http://www.google.com'); Quote Link to comment Share on other sites More sharing options...
spiderwell Posted September 6, 2012 Share Posted September 6, 2012 the errors seem to be occuring AFTER your print_r() and you havent shown us that part of the code, please post that and it will help us help you Quote Link to comment Share on other sites More sharing options...
MMDE Posted September 6, 2012 Share Posted September 6, 2012 $backlinkSources = array(); $result = mysql_query("SELECT website FROM `free_listings`"); while($row = mysql_fetch_assoc($result)) { $backlinkSources[] = $row; } print_r($backlinkSources); Returns these errors: Array ( [0] => Array ( [website] => http://www.safehandsplans.co.uk ) [1] => Array ( [website] => http://www.google.com ) ) Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39 Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45 Warning: file_get_contents() expects parameter 1 to be string, array given in /home/content/95/9771895/html/checktest.php on line 39 Warning: Illegal offset type in /home/content/95/9771895/html/checktest.php on line 45 Array ( ) Thanks in advance! I don't see 45 lines, neither the lines that gives the errors. What the code does is looping through the mysql_query's result. What it loops through is arrays of all the columns you've selected in your mysql_query. You are assigning these to a new array. This new array will therefor be an array of arrays. do this after the loop to see the content of the array $backlinkSources: echo '<pre>'; print_r($backlinkSources); echo '</pre>'; You will probably need to do this: foreach($backlinkSources AS $backlinkSource){ file_get_contents($backlinkSource['website']); } Or just do this while you loop through the results: while($row = mysql_fetch_assoc($result)) { $backlinkSources[] = $row['website']; } The latter is probably the best. Another little thing to note is what would happen if the mysql_query resulted in no rows? You would never create the array $backlinkSources, and later would try to use it. Do this before the loop to make sure it exists: $backlinkSources = array(); Or you could just check if the mysql_query resulted in 0 rows and terminate the execution of the rest of the related code: if(!mysql_num_rows($result)){ return; //should terminate the function if this is a function } Quote Link to comment Share on other sites More sharing options...
pontypete Posted September 6, 2012 Author Share Posted September 6, 2012 /* This is the part I need, but is corrupting everything else... $backlinkSources = array(); $result = mysql_query("SELECT website FROM `free_listings`"); while($row = mysql_fetch_assoc($result)) { $backlinkSources[] = $row; } print_r($backlinkSources);*/ $backlinkSources = array ('http://www.safehandsplans.co.uk','http://www.google.com'); $reportArray = array(); $falseFound = false; foreach ($backlinkSources as $source) { $html = file_get_contents ( $source ); $links = ExtractHrefLinks($html); if (CheckForTargetUrl($links, $url) === false) { $falseFound = true; $reportArray[$source] = 0; } else { $reportArray[$source] = 1; } } print_r($reportArray); if ($falseFound === true) { GenerateReportEmail($email, $fromEmail, $subject, $body, $url, $reportArray); } function GenerateReportEmail($email, $fromEmail, $subject, $body, $url, $reportArray) { $header = "From: " . $fromEmail . "\r\n"; foreach ($reportArray as $key => $report) { if ($report == false) { $body .= "Backlink to $url not found on: $key\n"; //update as link 0 $sqlUpdate = "UPDATE free_listings SET backlinkactive='0' WHERE website='$key'"; mysql_query($sqlUpdate) or die (mysql_error()); } } mail($email, $subject, $body, $header); } function CheckForTargetUrl($links, $target) { foreach ($links as $link) { if (strpos($link, $target) !== false) { return true; } } return false; } function ExtractHrefLinks($html) { $dom = new DOMDocument; $linkUrls = array(); @$dom->loadHTML($html); $links = $dom->getElementsByTagName('a'); foreach ($links as $link){ $linkUrls[] = $link->getAttribute('href'); } return $linkUrls; } Quote Link to comment Share on other sites More sharing options...
MMDE Posted September 6, 2012 Share Posted September 6, 2012 As I said in my earlier post: $backlinkSources[] = $row['website']; Oh and sorry, I didn't see you created the $backlinkSources array before the mysql_query. If it is a function, you may still want to terminate the execution of the rest of the code when there is no results from the mysql_query. Quote Link to comment Share on other sites More sharing options...
pontypete Posted September 6, 2012 Author Share Posted September 6, 2012 Thank you, much appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.