views:

229

answers:

3

I'm trying to print an ID from MySQL, the field loads into an array and is visible via print_r but I can't echo it or transfer it to another variable ... what am I missing?

if ( $_POST['section'] == "freelance" ) {
    $field_name = "promoter";
} else {
    $field_name = "connector";
}
echo $row[$field_name.'_login_ID']

As requested the results of var_dump($row)

array(13) {
   ["connector_login_id"] =>  string(2) "14"
   ["connector_type"] =>  string(10) "non-profit"
   ["unique_code"] =>  string(9) "test-t001"
   ["update_code"] =>  string(1) "N"
   ["md5ID"] =>  string(0) ""
   ["username"] =>  string(6) "bugger"
   ["connectorEmail"] =>  string(17) "[email protected]"
   ["password"] =>  string(32) "098f6bcd4621d373cade4e832627b4f6"
   ["connectorPass"] =>  string(4) "test"
   ["active"] =>  string(1) "Y"
   ["modified"] =>  string(19) "2009-08-21 15:37:22"
   ["lastlogin"] =>  string(19) "0000-00-00 00:00:00"
   ["md5email" ]=>  string(32) "051cba58da33fac6b2d18af5182079f4"
}
A: 

This is purely speculation without your code, but it's probable that the field you are trying to echo contains a hyphen, e.g. "mytable-id", considering that it does indeed show when you use print_r() to print out the entire array. If this is the case you would need to use {'mytable-id'} to get/echo it's value:

echo($dataArray->MyTable->{'mytable-id'});

*Edit: I don't know if your code is copy and pasted, but the value you are trying to print is:

echo $row[$field_name.'_login_ID'];

instead of:

echo $row[$field_name.'_login_id'];

PHP is case-sensitive. You could also try this:

$field_name = $field_name.'_login_id';
echo $row[$field_name];

or

$field_name = $field_name.'_login_id';
echo $row['$field_name'];
Mr. Smith
Thanks for the suggestion but I'm not using hyphens and the value still doesn't print even if I use echo $row[0].
No problem, I was merely guessing what the most obvious reason would be according to my experience. Can you please post the results of var_dump($row) as per Deceze comment on your question?
Mr. Smith
Updated possible solutions in my answer Belinda.
Mr. Smith
The last one won't work, there's no key named `'$field_name'`. :)
deceze
@deceze: AFAIK you could do echo('$field_name is having a great time'); and that would work just fine. Doesn't the same thing apply when passing it to an array?
Mr. Smith
Thanks :) It was the uppercase "ID" that was the problem.
@Belinda: Please mark one of the posts as accepted answer. They are all right.
Mr. Smith
@Boekwurm: Yes, in *double* quotes. But `$var` and `"$var"` are identical in this case anyway, so why bother? :)
deceze
@deceze: I forgot the double quotes part, thanks.
Mr. Smith
+2  A: 
$row[$field_name.'_login_ID']   <-- "ID"

array(13) {
    ["connector_login_id"]      <-- "id"

Seems like a simple typo to me.

Alternatively, are you sure $field_name gets set to 'connector', since 'promoter_login_id' doesn't exist in this array.

deceze
damn but that's embarrassing. Thanks everyone who helped.
The accepted standard response seems to be *"Argh, my coffee hasn't kicked in yet."*, but *"I shouldn't have coded for 24 hours straight"* works as well. ;o)
deceze
A: 

Looking at what you've pasted above, its the bleeding obvious:

$row[$field_name.'_login_ID']
-------------------------^^

array(13) { ["connector_login_id"]=>
------------------------------^^

PHP is case sensitive.

Salman A