Gossamer Forum
Home : General : Perl Programming :

SQL Limitation or coding?

Quote Reply
SQL Limitation or coding?
Ok, I got some PHP code that is supposed to track the impressions of a link via Links SQL (its gonna be a free plugin). I'm having a bit of a problem with it though as it doesn't seem to be tracking it correctly. The site I am testing it on reveives about 500k hits a day. Each page has 20 links on it. So, the approx number of calls for it is 10,000,000.

Now, for some reason, we tested a link, and it said that it was viewed 900 times. However, we put an image on the page at the bottom to see how impressions that link received and it got 39k (we tracked this with the system log). Could it be down to the fact that I'm using several queries and then re-submitting the information into the database? If so, is there a query that would do it all in one go? The code is below;

Code:
if (!$id) { error("You didnt specify an id number"); }

// set up the connection
$connection = mysql_connect("localhost", "", "");
if (!$connection) { error("Unable to connect with your login info for MySQL", $connection); }

// connect the database
$db = mysql_select_db("', $connection);
if (!$db) { error("Unable to connect to database", $connection); }

// set up the query
$query = "SELECT * FROM Links_Links WHERE ID = $id";

// now do a while to print out the HTML code to show the categories..
$result = mysql_query($query);
if (!$result) { error("Unable to execute query to mySQL Database.", $connection); }

//start putting into the HTML
while ($cat = mysql_fetch_array($result)) {

$count = $cat[view];

} // end of the while

$count++;

mysql_close($connection);

// set up the connection
$connection = mysql_connect("localhost", "", "");
if (!$connection) { error("Unable to connect with your login info for MySQL", $connection); }
// connect the database
$db = mysql_select_db("", $connection);
if (!$db) { error("Unable to execute command", $connection); }
// now create ther query and use it to update the field number...
$query = "UPDATE Links_Links SET view = $count WHERE ID = $id";
$result = mysql_query($query);
$error = mysql_error();
if (!$result) { error("Unable to execute query to mySQL Database! Reason: $error", $connection); }
mysql_close($connection);


echo "<img src=\"http://www.site.com/pixel.gif\" width=1 height=1>";


// erorr sub incase we get probs!
function error($error, $connection) {

echo $error;
mysql_close($connection);
exit;

}

Thanks for any help you guys can provide Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
Don't know for sure... but maybe you have the length of the views column set to 3. once views hits 1000, the value gets truncated and becomes 100.

Yes, you could update the views without using multiple queries..
Code:
UPDATE Links_Links SET views = views + 1 WHERE ID = $id

--Philip
Links 2.0 moderator
Quote Reply
Re: [King Junko II] SQL Limitation or coding? In reply to
>>
UPDATE Links_Links SET views = views + 1 WHERE ID = $id
<<

Wouldn't that need to be:

UPDATE Links_Links SET views = views + 1 WHERE ID = '$id'
Quote Reply
Re: [RedRum] SQL Limitation or coding? In reply to
No because you don't *have* to quote numbers.

Adrian
Quote Reply
Re: [brewt] SQL Limitation or coding? In reply to
....in the past not quoting numbers as scalars has caused syntax errors for me.

Last edited by:

RedRum: Feb 4, 2002, 2:53 AM
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
Any reason why you're connecting, running one query, disconnecting, and connecting again to run another query? Why not do it in one connection?

What's with the while loop? You're only going to get a maximum of one Link for the select (since ID is unique).

As the King Junko said, you might want to do an update with one query, but you also would probably want to check that the link exists, so you'd want to do something like:
1) connect and stuff
2) SELECT COUNT(*) FROM ${PREFIX}Links WHERE ID="$id" <= remember to support prefixes!
3) if the count = 1, then UPDATE ...
4) disconnect and stuff

BTW, isn't this handled by jump.cgi (or page.php equivalent)? Or is this doing something different?

Adrian
Quote Reply
Re: [RedRum] SQL Limitation or coding? In reply to
Code:
mysql> SELECT COUNT(*) FROM lsql210_Links WHERE ID = 123456;
+----------+
| COUNT(*) |
+----------+
| 1 |
+----------+
1 row in set (0.04 sec)
?

Adrian
Quote Reply
Re: [brewt] SQL Limitation or coding? In reply to
>>as scalars<<

Not sure why it happened. Perhaps I'm mis-remembering as there was a letter in there or something.

Last edited by:

RedRum: Feb 4, 2002, 3:07 AM
Quote Reply
Re: [brewt] SQL Limitation or coding? In reply to
Hi everyone. Thanks for the great replys.

Quote:
BTW, isn't this handled by jump.cgi (or page.php equivalent)? Or is this doing something different?
No, this is to count the impressions and not hits. i.e how many times the link has been loaded on the page.

Thanks, and I'll give your ideas a go Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
>>No, this is to count the impressions and not hits.<<

On results pages?

Surely doing it the way you are doing it won't work. You'd have to increment a count for every single link?
Quote Reply
Re: [RedRum] SQL Limitation or coding? In reply to
Yeah, on results, top links, category and other pages. The person that asked me to do it wants to be able to track impressions no matter where in L SQL they are shown. This means that I had to write this script to update the impressions table every time a link is viewed. Very CPU consuming, but they do have their own 1Ghz machine, so hopefully it can handle it ok Tongue

The way I am doing it is by calling a 1x1 image after the above code, and then using <img src='impressions.php?id=1234'>. It looks like its not doing to bad, but it just didn't seem to be coping very well incrementing it. I think its just the fact they get so many page impressions.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
>>The way I am doing it is by calling a 1x1 image after the above code, and then using <img src='impressions.php?id=1234'>. It looks like its not doing to bad, but it just didn't seem to be coping very well incrementing it. I think its just the fact they get so many page impressions.<<

I still don't see how that would work.

Are you putting the image into link.html or the actual category/search pages?

If the latter, how are you determining which links are present on those pages?
Quote Reply
Re: [RedRum] SQL Limitation or coding? In reply to
Its in link.html. Every time impression.php?id=1234 is called, it will send $id to the script. The image is really just there to give something back to the browser, and also it can be used on php/html/cgi pages, so it makes everything much easier.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
Hmm ok.

Last edited by:

RedRum: Feb 4, 2002, 7:35 AM
Quote Reply
Re: [AndyNewby] SQL Limitation or coding? In reply to
Wouldn't it be easier to create a global which does this? Create a global which prints out nothing, but executes the single SQL query (don't have to check if it exists, since the record already passed in). Something like this:
Code:
'link_views' => 'sub {
my $vars = shift;
$DB->table("Links")->update({ views => \"views + 1" }, { ID => $vars->{ID} });
return;
}'
Then just add <%link_views%> to some place in link.html

Adrian
Quote Reply
Re: [brewt] SQL Limitation or coding? In reply to
Interesting idea. I'll try it Smile

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [brewt] SQL Limitation or coding? In reply to
Darn. Just realised. That sub is only gonna work in dynamic mod, right?

If so, I think I will have to go back to my script Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!