简体   繁体   English

如何在 web 页面中存储 MySQL 值以供以后使用

[英]How to store a MySQL value in a web page for later use

I am fetching just one cell value from a MySQL table with php and storing it in a div with an id.我从带有 php 的 MySQL 表中仅获取一个单元格值,并将其存储在带有 id 的 div 中。 I am then trying to use that div later on in JavaScript code.然后我稍后在 JavaScript 代码中尝试使用该 div。

I think the php/MySQL code is all correct but there is a problem using the div value in the later JavaScript code.我认为 php/MySQL 代码都是正确的,但是在后面的 JavaScript 代码中使用 div 值存在问题。

What is the best way to store a MySQL value in a web page for later use?在 web 页面中存储 MySQL 值以供以后使用的最佳方法是什么?

Currently I am storing the returned result as:目前我将返回的结果存储为:

<div id="IsShift04Special"></div>

Using:使用:

xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        document.getElementById("IsShift04Special").innerHTML=xhttp.responseText;
    }
};

It all seemingly works well.这一切似乎都运作良好。

If I double click the 1 on the page in a browser and paste it into notepad it pastes just the 1. There are no visible carriage returns or spaces anywhere near it.如果我在浏览器中双击页面上的 1 并将其粘贴到记事本中,它只会粘贴 1。它附近没有可见的回车或空格。

But when I try to use the 1 value later on with:但是当我稍后尝试使用 1 值时:

var IsItASpecial = document.getElementById("IsShift04Special").innerHTML;

Then use that in an if statement, it is not working.然后在 if 语句中使用它,它不起作用。

When I check the value of IsItASpecial (where it is declared and in the if statement), in the Chrome debugger it returns as:当我检查 IsItASpecial 的值(声明它的位置和 if 语句)时,在 Chrome 调试器中它返回为:

IsItASpecial = "↵↵↵↵↵     1 ↵↵↵"

The table currently has just one row.该表当前只有一行。

The value is tinyint(1) with a value of 1.值为 tinyint(1),值为 1。

This is the php query:这是 php 查询:

$result=mysqli_query($conn, "SELECT shift04_special FROM `shifts` WHERE `users_username`= '$users_username' AND `companies_short_name` = '$companies_short_name' AND `job_role_short_name` = '$job_role_short_name'");  

$special_shift04 = mysqli_fetch_assoc($result);
echo $special_shift04['shift04_special'];

I get the same problem with:我遇到了同样的问题:

while ($row = mysqli_fetch_array($result)) {
    echo 'shift04_special';
}

And:和:

while ($row = mysqli_fetch_assoc($result)) {
    echo $row["shift04_special"];
}

When I do a var dump as:当我将 var 转储为:

var_dump($special_shift04);

This part is returned:这部分被退回:

["shift04_special"]=> string(1) "1" }

So it seems the problem is with my code for returning the div value and then storing it in a var.所以看来问题出在我返回 div 值然后将其存储在 var 中的代码上。

You can save it directly in a JavaScript variable.您可以将其直接保存在 JavaScript 变量中。

var id=<?php echo $row['shift04_special'] ?>

It's a typical typecast problem.这是一个典型的类型转换问题。 MySQL storing data separately from PHP. MySQL 与 PHP 分开存储数据。 So types in MySQL.= types in PHP.所以输入 MySQL.= 输入 PHP。 Because of it you did string(1) "1" in your PHP var_dump() function.因此,您在 PHP var_dump() 函数中执行了 string(1) "1" 。

So in your case, when you getting a value in Javascript environment you should cast this value to type that you need.因此,在您的情况下,当您在 Javascript 环境中获得一个值时,您应该将此值转换为您需要的类型。 In your case like this:在你这样的情况下:

var IsItASpecial = parseInt(document.getElementById("IsShift04Special").innerHTML);

But.但。 I don't recommend use JS like that in production.我不建议在生产中使用这样的 JS。 But for learning it's OK!但是学习没问题!

Have fun玩得开心

You can try to store in localstorage in a JSON format,您可以尝试以 JSON 格式存储在本地存储中,

// Fetch from server localStorage.setItem("mysqlResponse", JSON.stringify(response));

Now whenever you need the value,现在,每当您需要价值时,

const data = JSON.parse(localStorage.getItem("mysqlResponse"));

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

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