return link checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

return link checking

Post 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
Last edited by chris_s_22 on Thu Nov 26, 2009 10:31 am, edited 1 time in total.
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: return link checking

Post 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
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: return link checking

Post 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()
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: return link checking

Post 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
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: return link checking

Post 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
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: return link checking

Post by chris_s_22 »

AH YES! thx ian
Post Reply