Page 1 of 1

return link checking

Posted: Thu Nov 26, 2009 5:40 am
by chris_s_22
im trying to allow users to add links to my links page themselves. but only display links where i have a rteturn link back to me
ive created the table in database, the form, a page to check form data and then inserts data into database, ive got a page that displays the checked links

but now im trying to do the return link checking, id be grateful if someone could go through and check my code
im only a php beginner so expect silly mistakes
ive tried to be as descriptive as i can to what im doing.

Code: Select all

<?php
include_once 'Connect.php';
// FIRST I WANT TO GET LINKFROM COLUMN DATA FROM DATABASE - THIS CONTAINS URL's TO PAGES WHERE I WOULD EXPECT A RETURN LINK BACK TO MY SITE
$query = ("SELECT * FROM links");
$result= mysql_query ($query) or die ('Could not query.');
// SO I DEFINE MY URL
    $mylink = "http://www.yoururlhere.com";
// I THEN WANT TO CHECK EACH ROW 
while ($row = mysql_fetch_assoc($result))
{
    $linkto = $row["linkto"];
    
    // FROM THE URL STORED IN DATABASE COLULMN GET THE URL PAGE RETURN AS A STRING
    $linkcheckcontents = file_get_contents ($row['linkfrom']);
    if ($linkcheckcontents == FALSE) echo 'Could not open';
    else 
    {   
        //CHECKS THE LINK RETURNED AS A STRING CONTAINS MY $MYLINK
        if( (stristr($linkcheckcontents, '<a href="'.$mylink) === FALSE) && (stristr($linkcheckcontents, "<a href='" .$mylink) === FALSE)) 
        {
// IF $MYLINK DOESNT APPEAR SET checked='0'
        $query = ("UPDATE links SET checked='0' WHERE linkfrom='$linkfrom'");
        $result= mysql_query ($query) or die ('Could not query.');
        }
        else
        {
// IF $MYLINK APPEARS SET checked='1'
        $query = ("UPDATE links SET checked='1' WHERE linkfrom='$linkfrom'");
        $result= mysql_query ($query) or die ('Could not query.');      
        }
    }
// GOES BACK TO START AND REPEATS FOR NEXT ROW  
}
?>
if you do find any mistakes id be very gratful if you could explain why its wrong
thanks in advance

Re: return link checking

Posted: Thu Nov 26, 2009 10:10 am
by chris_s_22
i get the following message when its run
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ... line 8

Re: return link checking

Posted: Thu Nov 26, 2009 10:18 am
by iankent
looks like your query is wrong (or connection is failing). Try making line 3 this:

Code: Select all

$query = "SELECT * FROM links;";
actually if thats the start of the file, then its a lack of connection. you need to use mysql_connect() and mysql_select_db() before mysql_query()

Re: return link checking

Posted: Thu Nov 26, 2009 10:33 am
by chris_s_22
the include_once 'Connect.php'; does all the connecting and does a session start

i must add the script seems to update database checked feild correctly for the first the row
but doesant seem to loop and continue for each row

Re: return link checking

Posted: Thu Nov 26, 2009 10:44 am
by iankent
Ah, I think its because you're overwriting $result on line 23/29 so when it reaches the while() loop again its no longer a valid result.

Because they're update queries you can ignore the result, so just drop the '$result = ' bit from those two lines. If you had needed the result, you could have just changed it to $result2 or whatever

hth

Re: return link checking

Posted: Thu Nov 26, 2009 11:03 am
by chris_s_22
AH YES! thx ian