如何在第二个SQL语句中使用SQL语句的结果? |的PHP

时间:2019-11-13 16:58:54

标签: php mysql

,向所有阅读此书的人,
我试图从MySql数据库中读取 UserId
我想在下一个应该使用的SQL语句中使用结果
Id添加到另一个表中。问题是我不知道该怎么做。

在这里,我尝试从 accounts 表中选择行“ UserId”

代码如下:

//first statement
$SqlLookStm = ("SELECT UserId FROM accounts WHERE UserName = ?");
$initLStm = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($initLStm, $SqlLookStm)) {
    header('Location: ../signup.php?error=SqlError');
    exit();

} else {
    mysqli_stmt_bind_param($initLStm, "s", $UserName);
    mysqli_stmt_execute($initLStm);
}

$ UserName 是表单中的值。

现在我要使用用户ID,它应该是第一条语句的结果,
在我的下一个SQL语句中,该语句应在
中添加来自用户的id “ account_id” 行下的“ account_session” 表。

代码如下:

//second statement
$SqlInsertIdStm = ("INSERT INTO account_session (account_id) VALUES (?)");
$initIIStm = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($initIIStm, $SqlInsertIdStm)) {
   header('Location: ../signup.php?error=SqlError');
   exit();

} else {
    mysqli_stmt_bind_param($initIIStm, "s", $id);
    mysqli_stmt_execute($initIIStm);
}

但是如何使用语句一的结果,以便可以将其插入为
第二条语句中的 $ id 字符串?

我已经尝试了四天...(╯°□°)╯︵ ┻━┻
感谢所有可以帮助我的人。

2 个答案:

答案 0 :(得分:0)

尝试将id绑定为这样的整数

mysqli_stmt_bind_param($initIIStm, "d", $UserId);

然后执行该语句 (我建议您将小写字符用作字符串的第一个字符)

编辑:当我写评论时,我在考虑需要%d打印十进制值的printf()函数,而不是'd'使用'i'

答案 1 :(得分:0)

MySQL单一查询答案:

INSERT INTO `account_session` (account_id) SELECT `UserId` FROM `accounts` WHERE `UserName` = ?

要使用PHP和MySQLi做到这一点,我建议使用面向对象的方法,因此:

$myOwnSql = new mysqli('localhost', 'my_user', 'my_password', 'my_database');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}
$sql = "INSERT INTO `account_session` (account_id) SELECT `UserId` FROM `accounts` WHERE `UserName` = ?";

/***
 * Prepare the SQL statement
 ***/
$stmt = $myOwnSql->prepare($sql);
$stmt->bind_param('s', $UserName);

/***
 * Execute the prepared statement 
 ***/
$stmt->execute();
if($stmt->affected_rows < 1){
    /***
     * Nothing effected, something went wrong.
     ***/
    error_log("SQL Error: %s.\n", $stmt->error);
    header('Location: ../signup.php?error=SqlError');
    exit;
}
/***
 * close statement and connection 
 ***/
$stmt->close();

如何使用MySQLi面向对象(OOi)从数据库中提取数据:

$myOwnSqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

/***
 * Assume we have this table and it is suitably populated  
 ***/
$query = "SELECT UserId, RealName FROM accounts WHERE UserName = ?";

$stmt = $myOwnSqli->prepare($query);
$stmt->bind_param('s', $UserName);
$stmt->execute();

/***
 * Bind result variables 
 ***/
$stmt->bind_result($userId, $humanName);

/***
 * fetch each found row of bound values 
 ***/
 while ($stmt->fetch()) {
        print "Name: ".$humanName."<BR>";
 }

 /***
  * close statement 
  ***/
 $stmt->close();


/***
 * close DB connection 
 ***/
$myOwnSqli->close();

Read The Manual


要解决您所拥有代码的特定问题(即不要重做当前代码),请查看并向我们展示您的PHP / MySQL生成的error_log文件。

谢谢。

相关问题