Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySQL以行的形式读取数据_Php_Mysql - Fatal编程技术网

PHP MySQL以行的形式读取数据

PHP MySQL以行的形式读取数据,php,mysql,Php,Mysql,这里有点混乱,例如,我有这样的用户表: -------------------------------- | id | name | pass | email | -------------------------------- | 1 | a | a | a@a.a | -------------------------------- | 2 | b | b | b@b.b | --------------------------------

这里有点混乱,例如,我有这样的用户表:

--------------------------------
|  id  | name  | pass  | email |
--------------------------------
|   1  | a     | a     | a@a.a |
--------------------------------
|   2  | b     | b     | b@b.b |
--------------------------------
|   3  | c     | c     | c@c.c |
--------------------------------
我可以使用这个命令

    $query = mysqli_query($con, "SELECT * FROM user");
    $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
    $username = $row['name'];
    $password = $row['pass'];
但当我有这样的选项表时会遇到问题

------------------------------
|  id  | name      | content |
------------------------------
|   1  | username  | a       |
------------------------------
|   2  | password  | a       |
------------------------------
|   3  | email     | a@a.a   |
------------------------------
|   4  | url       | a.com   |
------------------------------
如何查询此表?重点是我想创建选项表,但不想在右边添加数据,而是要在底部添加数据,所以点“name value=content value”

我真的不知道如何解释它,这对我来说是新的,你可以在PHP中进行实验。例如,在您上面的示例中,尝试以下操作:

$username = '';

// query your OPTIONS table here
// assuming you've called it 'user' as described in your question
$query = mysqli_query($con, "SELECT * FROM user");
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    // note the extra $ prefix before $row['name']
    // this creates the variable from the contents of $row['name']
    $$row['name'] = $row['content'];
}

echo $username; // should return 'a'

简而言之,行的“name”列保存变量名,“content”列保存值。

选项表是关系数据库系统中常用的抽象,称为实体属性值。下面是一篇可爱的文章:

以下是如何查询
选项
表:

<?php
$query = mysqli_query($con, "SELECT * FROM options");
?>

以下是如何将信息捕获到PHP数组中:

<?php
$user = array();
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $user[ $row['name'] ] = $row['content'];
}
var_dump( $user );
?> 

以下是您访问数据的方式:

<html>
<p>username: <?= $user['username'] ?></p>
<p>password: <?= $user['password'] ?></p>
</html>

用户名:

密码:


请注意,您可能需要在选项表中添加一个
user\u id
列。这就是您的EAV表支持多个用户的方式。

表2中使用的标识符是什么2实际上我想创建类似于mysql表的wp\u选项,类似于此类似的函数,但不是为了wp它的工作谢谢:)
<html>
<p>username: <?= $user['username'] ?></p>
<p>password: <?= $user['password'] ?></p>
</html>