shohamh
shohamh

Reputation: 317

SELECT data from a 'messages' table, no error but it isn't working. MYSQL, PHP

I am trying to have an array of data that I can pull out from my table like that:

echo $message_data['content'];

so I declare $message_data like that:

     $message_data = message_data($_GET['id'],'id' ,'content', 'sender', 'addressee','support','date');

And the message_data() function is written like that:

function message_data($message_id) {
$data = array();
$message_id = (int)$message_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if($func_num_args > 1)
{
    unset($func_get_args[0]);

    $fields = '`'  .  implode('`, `' , $func_get_args) . '`';
    $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `messages` WHERE `id` = $message_id") or die(mysql_error()));
    return $data;
} }

This function does work similarly when I try to pull data about the user, I just copied and pasted it and tweaked it slightly. The problem is, there are no errors, and yes, I do input the browser with a GET variable. Where is the error? Or if there is a better way to do this, I'd be glad to hear.

Thanks a lot.

Upvotes: 2

Views: 118

Answers (1)

agou
agou

Reputation: 738

$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `messages` WHERE `id` = $message_id") or die(mysql_error()));

This line is the issue, inside the brackets it seems foo() or bar() equals to foo() || bar(), it's not what you want, so put it out side like this:

$res = mysql_query("SELECT $fields FROM `messages` WHERE `id` = $message_id") or die(mysql_error());
$data = mysql_fetch_assoc($res);

edit:

Just don't use or like this, or at least don't use the return value of the or like that.

Normally,

A or B

if A is false, then PHP will check B; if A is true, then there's no need to check B.

but I guess

$c = A or B

is actually

($c = A) or B

Upvotes: 2

Related Questions