如何从数据库获取信息到会话?

发布于 2025-01-07 21:30:22 字数 123 浏览 1 评论 0原文

我想通过使用数据库中的信息来启动新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梅窗月明清似水 2025-01-14 21:30:22
  1. 从数据库获取信息
  2. 将其存储在会话中

这两个操作都有很多教程。

  1. get information from the database
  2. store it in a session

There are plenty of tutorials on both operations.

懵少女 2025-01-14 21:30:22

我意识到您不需要参数化像这样的小型受控查询,但由于您处于学习阶段,您应该研究 PDO 和参数化查询并学习正确的方法以避免将来的麻烦。

<?php

$host   = "localhost";
$dbname = "mydb";
$user   = "username";
$pass   = "password";

//this is where you set the value you use in your where clause to determine
//what name to get back - I assume you'll use $_GET or $_POST or else to populate this
$name   = "bob";

    try {
        $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    } catch(PDOException $e) {
        echo "Problem connecting to DB, try again; if it presists contact admin.";
        exit();
    }

    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $sth_select = $dbh->prepare("SELECT row FROM table WHERE firstname = :name");

    $sth_select->setFetchMode(PDO::FETCH_ASSOC);

    $sth_select->execute(array('firstname' => $name));

    while ($row = $sth_select->fetch()) {
        $firstname = $row['firstname'];
    }

    session_start();

    $_SESSION['firstname'] = $firstname;

?>

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

$host   = "localhost";
$dbname = "mydb";
$user   = "username";
$pass   = "password";

//this is where you set the value you use in your where clause to determine
//what name to get back - I assume you'll use $_GET or $_POST or else to populate this
$name   = "bob";

    try {
        $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    } catch(PDOException $e) {
        echo "Problem connecting to DB, try again; if it presists contact admin.";
        exit();
    }

    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $sth_select = $dbh->prepare("SELECT row FROM table WHERE firstname = :name");

    $sth_select->setFetchMode(PDO::FETCH_ASSOC);

    $sth_select->execute(array('firstname' => $name));

    while ($row = $sth_select->fetch()) {
        $firstname = $row['firstname'];
    }

    session_start();

    $_SESSION['firstname'] = $firstname;

?>
难以启齿的温柔 2025-01-14 21:30:22

伪代码:

$fn = "";
SELECT RowC FROM TABLE B;
while($row = mysql_fetch_array($result_of_query) {
      $fn = $row['firstname'];
}
session_start();
$_SESSION['firstname'] = $fn;

阅读以了解在 php 中使用数据库 (MySql)。

Pseudocode:

$fn = "";
SELECT RowC FROM TABLE B;
while($row = mysql_fetch_array($result_of_query) {
      $fn = $row['firstname'];
}
session_start();
$_SESSION['firstname'] = $fn;

Read this to understand working with database (MySql) in php.

帅的被狗咬 2025-01-14 21:30:22

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 = $value

If 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.

黄昏下泛黄的笔记 2025-01-14 21:30:22

我假设您要求用户输入以限制您的查询。如果你使用 post 提交用户信息,你会做这样的事情......

session_start();

$userdata = mysql_real_escape_string($_POST['userdata']);
$result = mysql_query("select RowC from TableB where somedata='$userdata'") or die(mysql_error());
$result = mysql_fetch_row($result);
$result = $result[0];
$_SESSION['firstname'] = $result;

当然你也必须连接到数据库,并用一些唯一的列替换“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...

session_start();

$userdata = mysql_real_escape_string($_POST['userdata']);
$result = mysql_query("select RowC from TableB where somedata='$userdata'") or die(mysql_error());
$result = mysql_fetch_row($result);
$result = $result[0];
$_SESSION['firstname'] = $result;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文