Php 如果在mysql表中使用num_行(面向对象的过程)找到空字符串,如何显示消息?
好的,我有一个MySQL表,有3列(城市、颜色、投票)。 我有一个带有颜色的HTML表,其中一些颜色在MySQL表中不存在。如果不匹配,我尝试显示“0”。 当颜色不存在时,我在控制台中收到“”,但我想在HTML表中显示这些颜色的“0”。 有什么想法吗 到目前为止,我有这个php:Php 如果在mysql表中使用num_行(面向对象的过程)找到空字符串,如何显示消息?,php,mysql,Php,Mysql,好的,我有一个MySQL表,有3列(城市、颜色、投票)。 我有一个带有颜色的HTML表,其中一些颜色在MySQL表中不存在。如果不匹配,我尝试显示“0”。 当颜色不存在时,我在控制台中收到“”,但我想在HTML表中显示这些颜色的“0”。 有什么想法吗 到目前为止,我有这个php: $co = $_REQUEST['color']; $sql = "SELECT SUM(votes) AS sum FROM Votes WHERE color = '$co'"; $result = $conn-
$co = $_REQUEST['color'];
$sql = "SELECT SUM(votes) AS sum FROM Votes WHERE color = '$co'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["sum"];
}
} else {
echo 0;
}
这是ajax调用:
<script>
$(document).ready(function(){
$('#red').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#redr").html(response);
console.log(response);
}
});
});
$('#orange').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#oranger").html(response);
console.log(response);
}
});
});
$('#green').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#greenr").html(response);
console.log(response);
}
});
});
$('#indigo').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#indigor").html(response);
console.log(response);
}
});
});
$('#yellow').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#yellowr").html(response);
console.log(response);
}
});
});
$('#blue').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({
"url": "tables.php",
"data": {"color" : colorId},
success: function(response) {
$("#bluer").html(response);
console.log(response);
}
});
});
$('#total').on('click', function() {
var sumofval = 0;
$(".val").each(function () {
if ($(this).text() !== '') {
sumofval = sumofval + parseInt($(this).text());
}
$('#totalr').text(sumofval);
console.log(sumofval);
});
});
});
</script>
$(文档).ready(函数(){
$('#红色')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#redr”).html(回复);
控制台日志(响应);
}
});
});
$('#橙色')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#橙子”).html(回复);
控制台日志(响应);
}
});
});
$('#绿色')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#greenr”).html(回应);
控制台日志(响应);
}
});
});
$('#靛蓝')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#靛蓝”).html(回应);
控制台日志(响应);
}
});
});
$('#黄色')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#yellowr”).html(回复);
控制台日志(响应);
}
});
});
$('#蓝色')。在('单击',函数()上){
var colorId=$(this.attr(“id”);
$.ajax({
“url”:“tables.php”,
“数据”:{“颜色”:colorId},
成功:功能(响应){
$(“#bluer”).html(回复);
控制台日志(响应);
}
});
});
$('#total')。在('click',function()上{
var sumofval=0;
$(“.val”)。每个(函数(){
如果($(this).text()!=''){
sumofval=sumofval+parseInt($(this.text());
}
$('#totalr').text(sumofval);
console.log(sumofval);
});
});
});
尝试使用此选项
$('#red').on('click', function() {
var colorId = $(this).attr("id");
$.ajax({"url": "tables.php", "data": {"color" : colorId}, success: function(response) {
response = Number(response)
response = response.toFixed(0)
$("#redr").text(response);
console.log(response);
});
});
它使用.text()
而不是.html()
。它将来自php web端点的可能空白响应转换为实际数字
我使用了两行代码来实现这一点,因为雷蒙德中学的科学项目称为Internet Explorer。下面是这些行的作用:
在ajax回调中,response
可能是一个实际的数字,或者是一个包含数字的文本字符串,或者是一个空文本字符串,或者是一个空值<代码>响应=数字(响应)强制将其设置为数字。在空或空的情况下,数字为零
第二行response=response.toFixed(0)
将数字转换回文本字符串,小数点后为零位
然后将该文本字符串赋给.text(response)
,将其放在网页的适当位置
如果没有Internet Explorer,你可以这样做
... .text(Number(response).toFixed(0))
全部在一行中。但是IE除了在实际声明的变量上使用.toFixed()
方法外,无法处理任何事情。另外,额外的行更容易解释
Pro提示始终缩进代码。在传回值之前,只需检查查询结果即可 SUM()查询将始终返回一行且仅返回一行,因此您必须检查结果,然后如果结果为null(SUM没有值),则返回零,否则返回SUM()中的数字 注意:我还将您的代码更改为使用带绑定值的准备好的查询。这比您用来避免的串联值机制安全得多
好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您为自己的利益调试代码。您可能会被要求在几周/几个月内修改此代码,最终您会感谢我。那么,这里到底有什么问题?该查询将只返回一行。因此,您不需要while循环来获取它,只需要
fetch\u assoc()
。然后,查看代码的人会知道您只处理一个结果行。Self-DocumentationRiggsFolly,kk,我在一个html表中有两列(颜色、投票)。“颜色”列是html表中的链接。当您单击每种颜色时,该颜色的投票将通过ajax显示,但其中一些颜色不在mysql表中。发生这种情况时,我希望在表中显示“0”。现在,我只在浏览器控制台中收到颜色不存在时的“”。这对你有意义吗?谢谢你的回答,O.琼斯,它成功了。你能给我解释一下这两行是如何得到我想要的结果的吗?我想更清楚地理解它。琼斯,好的,但是为什么我需要把它转回到一个文本字符串?如果它已经是一个非空的文本字符串,你就可以了。但你必须发现这种情况并有条件地加以处理。我建议的代码不需要任何ifs或TERNARY就可以满足您的需要。谢谢您的帮助。RiggsFolly,这个过程是如何调用的?从代码段的int行4开始的代码。直到现在,我才学会
$sql = "SELECT SUM(votes) AS sum FROM Votes WHERE color = ?";
$stmt = $conn->prepare($sql);
// assumed `color` was a string column,
// if its integer is `'i'` instead of `'s'`
$stmt->bind_param('s', $_REQUEST['color']);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
echo $row['sum'] == '' ? 0 : $row['sum'];