How to replace text with appropriate variable

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
guitars4god7
Forum Newbie
Posts: 6
Joined: Thu Oct 14, 2010 5:49 pm

How to replace text with appropriate variable

Post by guitars4god7 »

I am working on my site and adding the ability for users to like different pages and I would like to be able to keep track of the number of likes each website url has and insert them into my database. So far it works fine for if I want to submit one url to check but I would like to set it up as a cron job to check multiple url's using a function. The problem that has me stumped is how do I take the "http://www.test.com" out of 'query' => 'select total_count from link_stat where url = "http://www.test.com";' and replace it with a variable. Every method I have tried has failed and i'm not sure if it has something to do with facebooks php sdk or not. Any help would be very much appreciated. Chance

Code: Select all

<?php

require './facebook.php';
include 'config.php';

$facebook = new Facebook(array(
  'appId'  => 'my app id',
  'secret' => 'my app secret',
  'cookie' => true,
));

$website = "http://www.test.com";

$result = $facebook->api(array(
	'method' => 'fql.query',
	'query' => 'select total_count from link_stat where url = "http://www.test.com";'
));

$fblikes = $result[0]['total_count'];
mysql_query("UPDATE likes SET count = '$fblikes' WHERE website = '$website'");

?>		
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: How to replace text with appropriate variable

Post by josh »

Code: Select all

$string = 'foo';
$otherString = 'bar';

echo $string . ' ' . $otherString;
echo "$string $otherString";
Both of those echo statements do the same. Its called concatenation in the PHP manual. Be sure to use mysql_real_escape_string() to escape the values.
guitars4god7
Forum Newbie
Posts: 6
Joined: Thu Oct 14, 2010 5:49 pm

Re: How to replace text with appropriate variable

Post by guitars4god7 »

No that's not it I'm afraid. I understand string concatenation but if that were the case I could just do something like this (which isn't concatenation but it wouldn't need to be) I think its something with fb's php sdk cause if I do this I get the following error.
Fatal error: Uncaught Exception: 601: Parser error: unexpected '$' at position 51. thrown in C:\Documents and Settings\Chance\Desktop\New Site\xampplite\htdocs\facebook.php on line 515
I can post the facebook.php file if you want to look. Is there some way I can make a variable inside the array or something? Can array statements have variables in them?

Code: Select all

<?php

require './facebook.php';
include 'config.php';

$facebook = new Facebook(array(
  'appId'  => 'my app id',
  'secret' => 'my app secret',
  'cookie' => true,
));

$website = '"http://www.test.com"';

$result = $facebook->api(array(
	'method' => 'fql.query',
	'query' => 'select total_count from link_stat where url = echo $website;'
));

$fblikes = $result[0]['total_count'];
mysql_query("UPDATE likes SET count = '$fblikes' WHERE website = '$website'");

?>		

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to replace text with appropriate variable

Post by requinix »

guitars4god7 wrote:No that's not it I'm afraid. I understand string concatenation but if that were the case I could just do something like this
No, actually, you don't understand it. Because if that were the case you could just do something like this.

Code: Select all

'query' => 'select total_count from link_stat where url = "' . $website . '"'
Post Reply