Jump to content

complicated string/array/variable issue


Pyro4816

Recommended Posts

For this example, $row2['author'] = "1"

$user = 'user'.$row2['author'];
if (!isset($$user)){
      $result = $connector->query('SELECT * FROM users WHERE ID = '.$row2['author']);
      $$user = $connector->fetchArray($result);
}
echo '<a href="user.php?user='.($$user['ID']).'">'.$$user.'</a></p>';

The problem is it is a weird variable that also has an array, but when i use

echo $$user['ID'];

It errors out saying Notice:

Undefined variable: u

 

So I am wondering how to word it to set the variable up properly, and how to call items in the array. Thanks in advance

Link to comment
Share on other sites

It wouldn't let me edit again, but an update on it is that

echo $user1['ID'];

Works, but since this is in a while loop, and i am trying to prevent too many server queries, im trying to set it so that if i need user 1's info again it wont query the database again, but it will query it if there is a user 2 who has not had his info retrieved.

Link to comment
Share on other sites

are you intending to use a variable variable...?

if (!isset($$user)){

 

echo $$user['ID'];

 

you're using the $$user variable variable 4 times in that script.

 

unless you have a need for a variable variable, just use $user

Link to comment
Share on other sites

I would use an array from the start

i.e.

$user = 'user'.$row2['author'];
$userArray = array();
if (!isset($$user)){
      $result = $connector->query('SELECT * FROM users WHERE ID = '.$row2['author']);
      $userArray[$user] = $connector->fetchArray($result);
}
echo '<a href="user.php?user='.$userArray[$user]['ID'].'">'.$user.'</a></p>';

 

play around with that, otherwise do print_r($$user); to ensure the variable variable array is setup as you wish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.