这里有点混乱,例如,我有这样的用户表:
--------------------------------
| 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 |
------------------------------如何查询此表?关键是我想要创建options表,但不是向右添加数据,而是将其底部设置为"name value = content value“。
username = a;
password = a;
email = a@a.a;
url = a.com;我真的不知道该怎么解释,这对我来说是新的
发布于 2014-09-18 02:49:38
您可以在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”列保存值。
发布于 2014-09-18 03:40:06
options表是关系数据库系统中常用的抽象,称为实体-属性值。下面是一篇很可爱的文章:
http://mikesmithers.wordpress.com/2013/12/22/the-anti-pattern-eavil-database-design
下面是如何查询您的options表:
<?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>注意,您可能需要向options表中添加一个user_id列。这就是您的EAV表能够支持多个用户的方式。
https://stackoverflow.com/questions/25903301
复制相似问题