如何从数据库获取信息到会话?
我想通过使用数据库中的信息来启动新的 PHP 会话,但我不知道如何操作。帮助?谢谢。
例如,我有 DatabaseA,其中包含 TableB 和 RowC。我想将 RowC 的名字存储到 PHP 会话中。我该怎么做?
I want to start a new PHP session by using information in a database, but I don't know how. Help? Thanks.
For example, I have DatabaseA that has TableB, with RowC. I want to store RowC's firstname into a PHP session. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这两个操作都有很多教程。
There are plenty of tutorials on both operations.
我意识到您不需要参数化像这样的小型受控查询,但由于您处于学习阶段,您应该研究 PDO 和参数化查询并学习正确的方法以避免将来的麻烦。
I realize you don't need to parameterize a small controlled query like this, but since you are in the learning phase you should look into PDO and parameterized querys and learn the right way to avoid future headaches.
伪代码:
阅读此以了解在 php 中使用数据库 (MySql)。
Pseudocode:
Read this to understand working with database (MySql) in php.
SELECT first_name FROM table_name WHERE
field_name
= $value如果您只想要第一行:
ORDER BY DESC LIMIT 1
最后一行:
ORDER BY ASC LIMIT 1
取决于您的 WHERE 子句的内容将取决于您获得的结果数量。
如果 3 个人有相同的名字,并且 WHERE last_name = bob.
将加载 3 个名为 Dave Bob 的人...如果您只想要 dave...
添加
您需要在查询末尾
GROUP BY first_name...这将仅返回一个 Dave编辑,您不需要 WHERE我只是在这个例子中添加了这一点。但如果您想要一个特定的名称(返回单个名称),这是唯一的方法。
SELECT first_name FROM table_name WHERE
field_name
= $valueIf you only want the first row:
ORDER BY DESC LIMIT 1
The last row:
ORDER BY ASC LIMIT 1
Depending on what you WHERE clause has will depend on how many results you get.
If 3 people have the same name and it was WHERE last_name = bob.
3 people with the name Dave Bob will load... if you only want dave...
you need to add
GROUP BY first_name at the end of your query... that will return only one Dave
EDIT you don't need a WHERE's clause i just adding that in for this example. But if you want a specific name (with a single name return) thats the only way to do it.
我假设您要求用户输入以限制您的查询。如果你使用 post 提交用户信息,你会做这样的事情......
当然你也必须连接到数据库,并用一些唯一的列替换“somedata”。确保您也调用了
session_start()
。I will assume that you are asking for user input in order to limit your query. If you use post to submit the users information, you would do something like this...
Of course you will have to connect to the database as well, and replace "somedata" with some unique column. Make sure you call
session_start()
as well.