Jump to content

[SOLVED] Very wierd mysql query error.


Joesavage

Recommended Posts

Well I guess the error is not so wierd, but I should not be getting it.

 

So my code is shown below. Let me just start off by saying that the scraper down there works fine.

I am getting a lame database error.

 

So all of the code works inside that first primary while loop which pulls every row from the table 'Pending' where Date = '2008/07/18' in this case. Then the scraper goes through and scrapes 10 different pages, searching those pages for a link called '/user/$Channel' where $Channel is a variable pulled from the table called Pending. If the scraper finds the link, it breaks out of the scraper and $Subscriptioncomplete gets set = 1. And then, if $Subscription = 1, it executes some code modifying the database. Here is where my error comes in:

$result = mysql_query("SELECT * FROM Users where Subchannel = '$Subscriber'");
while($row = mysql_fetch_array($result))

 

So I need some basic info from another database called Users, so i go to pull some info from there.

 

and i need some more basic info from Users, so I have another pull.

$result = mysql_query("SELECT * FROM Users where Subchannel = '$Channel'");
while($row = mysql_fetch_array($result))

 

They are essentially the same except they are getting info from two different rows.

 

So the weird part is that SOMETIMES i get the error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blah/public_html/Scraper1.php on line 97

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blah/public_html/Scraper1.php on line 112

 

(Lines 97 and 112 are what i posted above.)

 

Sometimes I DONT get this error...  And i dont mean for some rows or whatever. I mean sometimes i run the whole script and this error will appear everytime, or I run the whole script and it will never appear. Obviously I need more reliability than that.

 

So anyway, I hope its something really easy that I missed somehow. Please help!! The full script is show below. My first thought was that maybe I couldnt run while loops within eachother or something, but I have done it before so that doesnt make sense. And I dont think it is a problem with the databases, because sometimes this script runs flawlessly.... Anyway, help!

<?php
$Date = $_GET["Date"];

$con = mysql_connect("blahblahblah");
mysql_select_db("blahblahblah", $con);

    
$Tea = mysql_query("SELECT * FROM Pending where Date = '2008/07/18'");
while($Leaves = mysql_fetch_array($Tea))
{
$Count = 0;
$Channelcounter = 0;
$Channel = $Leaves[Channel];
$Subscriber = $Leaves[subscriber];
$Subscriptioncomplete = 0;

$Numberofpages = 10;

for ($q = $Numberofpages; $q > 0; $q--)
{
$target_url = "blahblahblah";
        $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
// make the cURL request to $target_url
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
         curl_setopt($ch, CURLOPT_URL,$target_url);
         curl_setopt($ch, CURLOPT_FAILONERROR, true);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         $html= curl_exec($ch);
         if (!$html) 
            {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    echo "<br>Curl Error.";    
    exit;
            }
          
         // parse the html into a DOMDocument
         $dom = new DOMDocument();
         @$dom->loadHTML($html);
         // grab all the on the page
         $xpath = new DOMXPath($dom);
         $hrefs = $xpath->evaluate("/html/body//a");
         for ($i = 0; $i < $hrefs->length; $i++)
            {
    $href = $hrefs->item($i);
    $Url = $href->getAttribute('href');
    if ($Url == "/user/$Channel")
    {
    $Subscriptioncomplete = 1;
    break 2;
    }
    }
}


if ($Subscriptioncomplete == 1)
{
echo "$Subscriber Subscribed to $Channel<br><br><br>";

$result = mysql_query("SELECT * FROM Users where Subchannel = '$Subscriber'");
while($row = mysql_fetch_array($result))
{
$Subscompleted = $row[subscompleted];
$Pendingsubscompleted = $row[Pendingsubscompleted];
$Backlink = $row[backlink];
}
if ($Backlink == 1)
{
$Inc = 1.25;
}
else
{
$Inc = 1;
}
$result = mysql_query("SELECT * FROM Users where Subchannel = '$Channel'");
while($row = mysql_fetch_array($result))
{
$Subsused = $row[subsused];
$Pendingsubsused = $row[Pendingsubsused];
}

$Subscompleted = $Subscompleted + $Inc; 
$Pendingsubscompleted = $Pendingsubscompleted - 1;
$Subsused = $Subsused + 1;
$Pendingsubsused = $Pendingsubsused - 1;
mysql_query("UPDATE Users SET Subscompleted ='$Subscompleted' WHERE Subchannel = '$Subscriber'");
mysql_query("UPDATE Users SET Pendingsubscompleted = '$Pendingsubscompleted' WHERE Subchannel = '$Subscriber'");
mysql_query("UPDATE Users SET Subsused = '$Subsused' WHERE Subchannel = '$Channel'");
mysql_query("UPDATE Users SET Pendingsubsused = '$Pendingsubsused' WHERE Subchannel = '$Channel'");
mysql_query("INSERT INTO Completedsubs (Subscriber, Channel) VALUES ('$Subscriber', '$Channel')");
mysql_query("DELETE FROM Pending WHERE Channel = '$Channel' && Subscriber = '$Subscriber'");
}

}

?>

Link to comment
Share on other sites

Those errors normally mean that mysql_query did not return a valid result resource due to an error in your query. use mysql_error to retrieve the error from mysql:

$result = mysql_query("SELECT * FROM Users where Subchannel = '$Channel'") or die(mysql_error());

Link to comment
Share on other sites

Ok well i just called my host to ask what it might be and apparently they have been working on the server for the last couple of days and have been disconnecting any particularly cpu consuming scripts. Thanks for telling me about the die thingy thats a good thing to have in there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.