0

So, I am trying to do a simple query which works fine when I put in the URL manually as a string. But when I put it as a variable it doesn't work. I even checked the variable and the variable has the URl in it. Never put a variable in a WHERE statement so not sure if it is an echo thing or what, just that nothing seems to work.

$external_result = $facebook->api(
    array(
        'method' => 'fql.query',
        'query' => 'SELECT share_count, 
                           like_count, 
                           comment_count, 
                           total_count 
                      FROM link_stat 
                     WHERE url="{$urlf}";'
    )
);

echo $external_result[0]['total_count'];
0

4 Answers 4

1

There's a difference between single and double quoted strings in PHP syntax. A single quoted string does not offer variable interpolation. Whereas double quoted strings do allow you to interpolate variables inside string literals. See the manual on PHP strings for more details.

Here's what you need to do.

$external_result = $facebook->api(
    array(
        'method' => 'fql.query',
        'query' => "SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url=\"{$urlf}\";"
    )
);

By replacing your single quotes around that string to double quotes you now get PHP to replace $urlf with it's actual value.

Sign up to request clarification or add additional context in comments.

Comments

1

Single-quoted strings do not allow for variable interpolation:

$foo = 5;
echo("This is $foo"); # "This is 5"
echo('This is ' . $foo); #ditto
echo('This is $foo'); # "This is $foo"
echo("This is \$foo"); # ditto

I'd recommend looking through some PHP tutorials or picking up a book.

Comments

0

Change your code to:

$external_result = $facebook->api(
    array(
        'method' => 'fql.query',
        'query' => 'SELECT share_count, like_count, comment_count, 
                    total_count FROM link_stat WHERE url="' . $urlf . '";'
    )
);

3 Comments

I tried this but it came up with no value, but the double quotes issue just above fixed it. Hmmmm.
Its your prerogative to mark any answer as accepted however just to let you know that there is no difference between your accepted answer and mine other than I am using much preferred way of concatenating variable with string in PHP.
@Neal: Thanks, yes I see, removed curly braces.
-1

Use double quotes if you are going to do something like that.

'query' => "
            SELECT share_count, like_count, comment_count, total_count 
            FROM link_stat WHERE url='{$urlf}';
           "

2 Comments

This totally worked but you said don't! Crap. $external_result = $facebook->api(array( 'method' => 'fql.query', 'query' => "SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url='{$urlf}';" )); echo $external_result[0]['total_count'];
Not sure why this got downvoted either. It's a perfectly acceptable answer, which indeed solves the problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.