如何将SQL框的值存储到PHP变量中

时间:2021-02-11 14:02:10

I want to select the data and then store it's information inside of a variable. I feel this is very simple but I can't seem to get it to do it. When I run the below code the result is blank.

我想选择数据,然后将其信息存储在变量中。我觉得这很简单,但我似乎无法做到这一点。当我运行以下代码时,结果为空。

Thanks for any help.

谢谢你的帮助。

$sql = "SELECT `$dateName` FROM `$user` WHERE hour=1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $x = $row["`$dateName`"];
        echo $x;
    }
}

Here is my Create Table code:

这是我的创建表代码:

/* CREATE TABLE FOR NEW USER */
$sql = "CREATE TABLE `$newUser` (
            hour INT(6) NOT NULL
        )";
/* CHECK CREATION */
if ($conn->query($sql) === TRUE) {
} else {
    echo "Error creating table: " . $conn->error . ".<br><br>";
}

/* ADD TABLE FOR TODAYS DATE */
$sql = "ALTER TABLE `$user`
            ADD `$dateName` text";

if ($conn->query($sql) === TRUE) {
    echo "Todays Date entered!";
} else {
    echo "Date already Exists" . "<br><br>";
}

1 个解决方案

#1


1  

$x will always be empty because of the ` you're adding to the key when you access $row:

$ x将始终为空,因为当您访问$ row时,您正在添加密钥:

$x = $row["`$dateName`"];
           ^         ^ remove ticks (You can also remove ")

#1


1  

$x will always be empty because of the ` you're adding to the key when you access $row:

$ x将始终为空,因为当您访问$ row时,您正在添加密钥:

$x = $row["`$dateName`"];
           ^         ^ remove ticks (You can also remove ")