doushen1026 2016-04-18 21:12 采纳率: 100%
浏览 23
已采纳

如何获取单个mysql值并将其输出到ajax调用?

I'm trying to get a number from a mysql line then outputting it to ajax. the number can't be a string because I will multiply it in ajax. This is what i have so far. I'm not sure what to do from here.

ajax:

$(document).ready(function()
{
    $("#btnCalc").click(function()
    {
        var user     = $("#txtUser").val();
        var amount   = $("#txtAmount").val();
        var category = $("txtCat").val();
        var number   = $("txtNum").val();
        var result = '';

        $.get("code/value.php",
        {
            ID:user,
            amount:amount,
            result:result

        },function(query)
        {
            if ( user > 0 and user < 30 ){
                alert(result);
            }
            else{
                alert( 'invalid user ID');
            }
        });
    });

});

php:

<?php

    $userID = $_GET["ID"];
    $amount = $_GET["amount"];
    $category = $_GET["category"];
    $num      = $_GET["number"];

    require "../code/connection.php";

    $SQL = "select userAmount from user where userID= '$userID'";

    $reply = $mysqli->query($SQL);

    while($row = $reply->fetch_array() )
    {


    }

    if($mysqli->affected_rows > 0){
        $msg= "query successful";
    }
    else{
        $msg= "error " . $mysqli->error;
    }

    $mysqli->close();

    echo $msg;
?>
  • 写回答

3条回答 默认 最新

  • douying6206 2016-04-18 21:18
    关注

    Pretty straightforward - you just grab the value from the row and cast it as a float.

    while($row = $result->fetch_array() )
    {
        $msg = floatval($row['userAmount']);
    }
    if($msg > 0) {
        echo $msg;
    } else {
        echo "error" . $mysqli->error;
    }
    
    $mysqli->close();
    

    And one small change in your ajax call:

        $.get("code/value.php",
        {
            ID:user,
            amount:amount,
            result:result
    
        },function(query)
        {
            alert(query);
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器