PHP - Dynamic Array ID

Status
Not open for further replies.

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Hey fellas,
So I'm trying to create a class for calling user's motto but I need to use like
PHP:
    $stmt = $db->prepare("SELECT * FROM users WHERE username = :u");
    $stmt->bindParam(':u', $_SESSION['user']['username']);
    $stmt->execute();
    $fetch = $stmt->fetchAll();
   //  print_r($fetch);
    $this->motto = $fetch[0][12];
as you can see, I have to use [0] before the [12] (motto column). I want it to be dynamic, and I don't know how to search this in google, what would be your opinions/suggestions?
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Hey fellas,
So I'm trying to create a class for calling user's motto but I need to use like
PHP:
    $stmt = $db->prepare("SELECT * FROM users WHERE username = :u");
    $stmt->bindParam(':u', $_SESSION['user']['username']);
    $stmt->execute();
    $fetch = $stmt->fetchAll();
   //  print_r($fetch);
    $this->motto = $fetch[0][12];
as you can see, I have to use [0] before the [12] (motto column). I want it to be dynamic, and I don't know how to search this in google, what would be your opinions/suggestions?
I don't know how you couldn't find This, because a simply search came up with a Stack Overflow answer, also you could look up all the arguments available for fetch and fetchAll

But you should do
PHP:
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$this->motto = $user->motto;

And you can't bind a param to an array value, you gotta define it as an variable first and then pass that.
PHP:
$username = $_SESSION['user']['username']
// ...
$stmt->bindParam(':u', $username, PDO::PARAM_STR);
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
I don't know how you couldn't find This, because a simply search came up with a Stack Overflow answer, also you could look up all the arguments available for fetch and fetchAll

But you should do
PHP:
$user = $stmt->fetch(PDO::FETCH_ASSOC)
$this->motto = $user->motto
Notice: Trying to get property of non-object. I guess, I need to do it manually or what.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Not quite sure what you mean? Do you mean where you can pass a user ID and a column name through to a function and it returns the value of the column related to that user?

If so, I think it'd be something like this (bear in mind I've not coded PHP in a long while)

PHP:
function getUserInfo($column, $user='') {
  if ($user == '') $user = $_SESSION['user']['username'];
  $stmt = $db->prepare("SELECT $column FROM users WHERE username = :u LIMIT 1");
  $stmt->bindParam(':u', $user);
  $stmt->execute();
  $usr = $stmt->fetch(PDO::FETCH_OBJ);
  return $usr->{$column};
}

Then you can either use it like:
PHP:
echo getUserInfo('motto');
...to get the motto of the current logged in user

PHP:
echo getUserInfo('motto', 'Markshall');
...to get the motto of the user Markshall
 
Last edited:

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Not quite sure what you mean? Do you mean where you can pass a user ID and a column name through to a function and it returns the value of the column related to that user?

If so, I think it'd be something like this (bear in mind I've not coded PHP in a long while)

PHP:
function getUserInfo($column, $user='') {
  if ($user == '') $user = $_SESSION['user']['username'];
  $stmt = $db->prepare("SELECT :column FROM users WHERE username = :u LIMIT 1");
  $stmt->bindParam([':column' => $column, ':u' => $user]);
  $stmt->execute();
  $usr = $stmt->fetch(PDO::FETCH_OBJ);
  return $usr->{$column};
}

Then you can either use it like:
PHP:
echo getUserInfo('motto');
...to get the motto of the current logged in user

PHP:
echo getUserInfo('motto', 'Markshall');
...to get the motto of the user Markshall
stdClass Object ( [?] => motto )
Notice: Undefined property: stdClass::$motto
I've changed code abit and encounter this issue, as your code's bindParam returns error.
code:
PHP:
if ($user == '') $user = $_SESSION['user']['username'];
  $stmt = $db->prepare("SELECT :column FROM users WHERE username = :u LIMIT 1");
  $stmt->bindParam(':column',$column);
  $stmt->bindParam(':u', $user);
  $stmt->execute();
  $usr = $stmt->fetch(PDO::FETCH_OBJ);
  print_r($usr);
 
  return $usr->{$column};
I couldn't think of anything, maybe you'd know? @Markshall
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
stdClass Object ( [?] => motto )
Notice: Undefined property: stdClass::$motto
I've changed code abit and encounter this issue, as your code's bindParam returns error.
code:
PHP:
if ($user == '') $user = $_SESSION['user']['username'];
  $stmt = $db->prepare("SELECT :column FROM users WHERE username = :u LIMIT 1");
  $stmt->bindParam(':column',$column);
  $stmt->bindParam(':u', $user);
  $stmt->execute();
  $usr = $stmt->fetch(PDO::FETCH_OBJ);
  print_r($usr);
 
  return $usr->{$column};
I couldn't think of anything, maybe you'd know? @Markshall
You can't bind table or column names.

Hold on and I'll throw something together.

Edit:

Try something along the lines of this - haven't ran it so may need adjusting to your needs but gives an idea.
PHP:
function getInfo($data, $user) {

    $allowed = array('username', 'motto', 'etc');

    if (in_array($data, $allowed)) {
        $col = $data;
    } else {
        $col = 'null';
    }

    $stmt = $db->prepare("SELECT $col FROM users WHERE username = :u LIMIT 1");
    $stmt->bindParam(':u', $user);
    $stmt->execute();
    
    return $stmt->fetch(PDO::FETCH_OBJ)->{$col};

}
print getInfo('motto', 'john');
 
Last edited:

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
You can't bind table or column names.

Hold on and I'll throw something together.

Edit:

Try something along the lines of this - haven't ran it so may need adjusting to your needs but gives an idea.
PHP:
function getInfo($data, $user) {

    $allowed = array('username', 'motto', 'etc');

    if (in_array($data, $allowed)) {
        $col = $data;
    } else {
        $col = 'null';
    }

    $stmt = $db->prepare("SELECT $col FROM users WHERE username = :u LIMIT 1");
    $stmt->bindParam(':u', $user);
    $stmt->execute();
   
    return $stmt->fetch(PDO::FETCH_OBJ)->{$col};

}
print getInfo('motto', 'john');
Thank you, this one worked perfectly.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
You can't bind table or column names.

Hold on and I'll throw something together.

Edit:

Try something along the lines of this - haven't ran it so may need adjusting to your needs but gives an idea.
PHP:
function getInfo($data, $user) {

    $allowed = array('username', 'motto', 'etc');

    if (in_array($data, $allowed)) {
        $col = $data;
    } else {
        $col = 'null';
    }

    $stmt = $db->prepare("SELECT $col FROM users WHERE username = :u LIMIT 1");
    $stmt->bindParam(':u', $user);
    $stmt->execute();
   
    return $stmt->fetch(PDO::FETCH_OBJ)->{$col};

}
print getInfo('motto', 'john');

Please don't execute a query with SELECT NULL if it's not allowed. Just replace $col = 'null'; with return false; for the sake of proper engineering.
 
Status
Not open for further replies.

Users who are viewing this thread

Top