简体   繁体   English

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

[英]How to store the value of a SQL box into a PHP variable

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>";
}

$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 ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM