php - 改变变量

标签 php mysql

这是我闹鬼的代码。

ini_set("display_errors", true);
ini_set("html_errors", false); 
require "conn.php";
echo "debug 1";
$stmt2 = $conn->prepare("SELECT * FROM UserData WHERE username = ?");
$stmt2->bind_param('s',$username);
//$username = $_POST["username"];
$username ="netsgets";
$stmt2->execute();
$stmt2->store_result(); 
if ($stmt2->num_rows == 0){ // username not taken
echo "debug 2.5";

die;
}else{
// prepare query
$stmt=$conn->prepare("SELECT * FROM UserData WHERE username = ?");
// You only need to call bind_param once
$stmt->bind_param('s',$username);
$username = "netsgets";
// execute query
$stmt->execute(); 
$stmt->store_result();  
// bind variables to result
$stmt->bind_result($id,$dbUser,$dbPassword,$Type1,$Type2,$Type3,$Type4,$Type5);
//fetch the first result row, this pumps the result values in the bound variables

if($stmt->fetch()){
    echo 'result is ' . $Type1, $Type2,$Type3,$Type4,$Type5;
}


//var_dump($query2);
echo "hi";

echo "debug 2";



echo "debug 2.7";

    if ($Type1 == "empty"){

        echo "debug 3";
        $sql11 = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?");
        $sql11->bind_param('ss',$TUsername,$Username);
//      $TUsername = $_POST["TUsername"];
//      $Username = $_POST["username"];
        $TUsername = "test";
        $Username = "netsgets";
        $sql11->execute();



    } 
}

这是它返回的内容( echo )。

Connected successfullydebug 1result is empty empty empty empty empty hidebug 2debug 2.7

如您所见,变量 Type1、Type2、Type3、Type4、Type5 都等于“空”。 出于某种原因,如您所见,if 语句不认为它是“空的”,因为它没有回显“debug 3”。什么.....(也没有错误。)

最佳答案

如果这段代码...

echo 'result is ' . $Type1, $Type2,$Type3,$Type4,$Type5;

字面上产生这个输出...

result is empty empty empty empty empty

每个“空”之间有空格,那么显然你数据库中的Type*值都是“empty”或者“empty”或其他一些与前导/尾随空格的组合。

你会想和

做比较
trim($Type1) == 'empty'

此外,如评论中所述,从不SELECT *bind_result 结合使用。您应该始终明确选择的列及其出现的顺序,例如

SELECT id, dbUser, dbPassword, Type1, Type2, Type3, Type4, Type5 FROM ...

关于php - 改变变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604113/

相关文章:

php - 我可以在 PHP 中混合使用 MySQL API 吗?

php - 为什么 PHP 双重转义这个字符串?

php - 获取当月和下个月的记录

php - 2 个准备好的语句,2 个存储过程,1 个 mysqli 连接

mysql - 用户注销后是否应该从记录中清除 session 表?

php - HTML5 切片,结果文件已损坏

php - 用于从用户提交的数据确定值(value)的数据库设计

php - 使用端口号制作 php curl

php - .htaccess 排序和名称冲突

mysql - 在MySQL中,在不同表中的两个字段之间使用或条件时如何充分利用索引?