1

I've got this and I dont know why its not working. It's not returning the ID from the first query so I can use it in the 2nd query. Nevermind the REFERER, its an external link it will refer from.

require("/home/username/public_html/wp-load.php");

global $wpdb;

$sql = $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_url LIKE '%%s'%", $_SERVER['HTTP_REFERER'] );
$row = $wpdb->get_row($sql);
$lid = $row->link_id;

$wpdb->query("UPDATE $wpdb->wptstats SET impressions = impressions+1 WHERE link_id = ".$lid);

Can anyone please help me in the right directions ?

error log file

[03-Mar-2015 17:13:26 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET impressions = impressions+1 WHERE link_id =' at line 1 for query UPDATE SET impressions = impressions+1 WHERE link_id =

3
  • 2
    What does "Not Working" mean? Does it not return anything? Does it return the wrong information? Does it kill the page ( white screen )? Have you tried turning on debugging? What are you expecting this SQL to return?
    – Howdy_McGee
    Commented Mar 2, 2015 at 22:42
  • did you var_dump the sql that you pass to get_row? Commented Mar 3, 2015 at 12:36
  • No, the code you see is all the code I have for this file at the moment. Commented Mar 3, 2015 at 12:38

1 Answer 1

0

I think the problem may be with your wildcards. Because you're trying to use Wildcards in the prepare, which takes %s, the WPDP doesn't know the difference between the wildcard % and the placeholder %. Using this answer as reference: How to use wildcards in $wpdb queries using $wpdb->get_results & $wpdb->prepare? You'll have to double-escape the wildcards:

$sql = $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_url LIKE '%%%s%%'", $_SERVER['HTTP_REFERER'] );

There was also an issue with a wildcard being outside the single quote '. Hopefully this will work for you.

As for the error, try this, I was able to update link_rating using the same method though I am not familiar with wpstats:

$wpdb->query( "UPDATE $wpdb->wptstats SET impressions = impressions+1 WHERE link_id = $lid" );

Using update function instead of query:

$wpdb->update(
    $wpdb->wptstats ,
    array( 'impressions' => 'impressions' + 1 ),
    array( 'link_id' => $lid ),
    array( '%d' )
);
6
  • my error log file: [03-Mar-2015 17:13:26 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET impressions = impressions+1 WHERE link_id =' at line 1 for query UPDATE SET impressions = impressions+1 WHERE link_id = Commented Mar 3, 2015 at 17:14
  • @RogerAndersen Maybe try my updated line at the end. If that doesn't work maybe try $wpdb->update instead. I was able to get the same code to work to increment link_rating in the links table.
    – Howdy_McGee
    Commented Mar 3, 2015 at 18:20
  • I tried both and I don't seem to be able to get the link_id from the first query at all. I try to echo it, but it never seems to appear. I also tried your suggestions with the new query and the wpdb update query, but it seems like its failing in the first query. The wptstats table is custom and the impressions are INT(11) Commented Mar 3, 2015 at 20:41
  • @RogerAndersen As Pieter mentioned earlier, this could be a problem with $_SERVER['HTTP_REFERER'] and not the query, I got the query and update to work on my installation. Try to remove $_SERVER['HTTP_REFERER'] and replace it with a static string that you know is in the links table.
    – Howdy_McGee
    Commented Mar 3, 2015 at 20:42
  • That does it. Thats weird, but what other options do I have to get the refering url ? Commented Mar 3, 2015 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.