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 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染