0

I am trying to get single value from DB with single query. i mean that i already featched all values in one query from that i need to get only one column value.

$connection = mysqli_connect($servername, $username, $password, $dbname);
            $query = mysqli_query( $connection,"select * from register where password='$log_in_password' AND username='$log_in_username'");
            var_dump($query);
            $table = mysqli_fetch_all($query,MYSQLI_ASSOC);
            var_dump($table);

assume columns are ' userid useremail username password Name '

from php var_dump($query) gives this..

object(mysqli_result)[2]
  public 'current_field' => null
  public 'field_count' => null
  public 'lengths' => null
  public 'num_rows' => null
  public 'type' => null

var_dump($table) gives this

 array (size=1)
  0 => 
    array (size=5)
      'userid' => string '7' (length=1)
      'useremail' => string '[email protected]' (length=16)
      'username' => string 'demousername' (length=7)
      'password' => string 'demopassword' (length=5)
      'Name' => string 'demoname' (length=6)

so help me out with fetching only one column value's record (example insSelect * to select userid)

Vicky
  • 469
  • 3
  • 9
  • 22
  • plz clarify your question a little bit more – Rohitashv Singhal Jun 15 '15 at 11:58
  • from`select *` i am getting one entire row as a result and i got this in a variable called `$query` since it is having all value how to get a single value (where `select userid` suppose to give). how to get? – Vicky Jun 15 '15 at 12:01

2 Answers2

0

Try this

$connection = mysqli_connect($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "select * from register where password='$log_in_password' AND username='$log_in_username'");
$table = null;
while ($row = mysqli_fetch_array($query)) {
    $table = $row;       
}
print_r($table);
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20
0

Your variable $table is an array, so if you want only the userid, you just have to use

$table[0]['userid']

If you want to query only the userid change you query like this :

$query = mysqli_query( $connection,"select userid from register where password='$log_in_password' AND username='$log_in_username'");
$table = mysqli_fetch_all($query,MYSQLI_ASSOC);

And the result of $table will be :

array (size=1)
  0 => 
    array (size=1)
      'userid' => string '7' (length=1)

$table will also be an array, and to acces userid you have to use again

$table[0]['userid']
pchmn
  • 524
  • 1
  • 5
  • 16