tags:

views:

48

answers:

2

Hi,

I've got the following function:

public function already_tweeted($tweet){
        return mysql_num_rows(mysql_query("SELECT * FROM `retweeted` WHERE `tweet`='$tweet'"));
    }

Pretty straightforward. It checks whether a tweet already exists in the database.

The table had the following records:

id  user    tweet
3       You should retweet this too
2       Retweet this

(user is empty for now)

This code:

$db_reader = new database_reader;
$already_tweeted = $db_reader->already_tweeted($tweet);
print $tweet . ". Already: ";
var_dump((bool) $already_tweeted);
print "<br>";

Gives the following output:

You should retweet this too. Already: bool(false) 
Retweet this. Already: bool(true) 

I'm pretty much stuck here. When I run SELECT * FROM retweeted WHERE tweet='You should retweet this too' in phpmyadmin I get 1 row back.

+1  A: 

Check what is the value in $tweet

similar problem

Also change this line to be safe

return mysql_num_rows(mysql_query("SELECT * FROM retweeted WHERE tweet ='{$tweet}'"));
JapanPro
The value of $tweet is correct, that was the first thing I checked. The recourse is valid.
Tim van Dalen
its seems every thing is good beside out put, can you put more codes.
JapanPro
can you output using this echo "<pre>"; print_r($already_tweeted);
JapanPro
Why `print_r($already_tweeted)`?? We already know the value of `$already_tweeted`...
captaintokyo
This solved it though. I guess he meant echo "<pre>"; print_r($tweet);$tweet does have double spaces, that for some reason aren't printed/ removed by trim
Tim van Dalen
A: 

return mysql_num_rows(mysql_query("blah-blah")); is a dirtiest way to run a query.
make a helper function instead of this horror.

mysql_num_rows() is a dirtiest way to count rows, SELECT count(*) wuery should be used instead.

//put this function to your function library
function dbgetvar($query){
  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbget: ".mysql_error()." in ".$query);
    return FALSE;
  }
  $row = mysql_fetch_row($res);
  if (!$row) return NULL;
  return $row[0];
}
public function already_tweeted($tweet){
  $tweet = mysql_real_escape_string($tweet);
  return dbgetvar("SELECT count(*) FROM `retweeted` WHERE `tweet`='$tweet'"));
}
Col. Shrapnel
Still no luckYou should retweet this too. Already: string(1) "0" Retweet this. Already: string(1) "1"
Tim van Dalen
@Tim it doesn't matter. My answer not about your problem but about your code. As for your problem - just compare these strings. mysql_num_rows() has nothing to do here
Col. Shrapnel
Yeah, your code is way cleaner, thanks. This is still very weird though
Tim van Dalen