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 'demo@gmail.com' (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
  • so from this i am getting `Array ( [0] => Array ( [userid] => 7 [useremail] => prabhu@gmail.com [username] => vignesh [password] => apple [Name] => prabhu ) )` so how to get second array value.. i am new to php please help – Vicky Jun 15 '15 at 12:09
  • 1
    print_r($table[0]) to get second array value –  Jun 15 '15 at 12:12
  • @RajagopalPG i have updated the code now you will get all the values – Faiz Rasool Jun 15 '15 at 12:12
  • @vishalshah no it would not because it had break to run only once and also limit one in sql but now it would. Please vote up the answer and mark correct that would be appreciated – Faiz Rasool Jun 15 '15 at 12:13
  • 1
    @FaizRasool For $table = Array ( [0] => Array ( [userid] => 7 [useremail] => prabhu@gmail.com [username] => vignesh [password] => apple [Name] => prabhu ) ) this array I said use $table[0] to get second array –  Jun 15 '15 at 12:15
  • @vishalshah oh he mean second value a inner array value? – Faiz Rasool Jun 15 '15 at 12:16
  • 1
    @RajagopalPG clarify what you want? like what result you want – Faiz Rasool Jun 15 '15 at 12:17
  • @FaizRasool that is what i thought –  Jun 15 '15 at 12:17
  • @vishalshah i think he is looking to get the all of the matching result which for me does not give a sense but let see what he say – Faiz Rasool Jun 15 '15 at 12:18
  • `$table[0]['userid']` this is what i want and answer is already given .. thank you for helping me out. – Vicky Jun 15 '15 at 12:42
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