简体   繁体   English

如何从PHP的查询结果中获取单个值

[英]How to get a single value from a query result in php

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 ' 假定列为“ userid useremail用户名密码名称”

from php var_dump($query) gives this.. 从PHP var_dump($ query)给出了这个。

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 var_dump($ table)给出了

 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) 因此,请帮我获取仅一列值的记录(例如insSelect *以选择用户ID)

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);

Your variable $table is an array, so if you want only the userid, you just have to use 您的变量$ table是一个数组,因此,如果只需要userid,则只需使用

$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 : $ table的结果将是:

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也将是一个数组,要访问userid,您必须再次使用

$table[0]['userid']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM