duanchouyi6730 2014-01-27 02:11
浏览 17
已采纳

我的SQL没有返回预期的值[重复]

This question already has an answer here:

I am new to phpMySQL and php. and I am trying to get a value from database and put it in a textbox. Currently, I only have 1 value in my table to test the code:

This is my code:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "dbtest";
$tbl_name="tbltest";

$con = mysql_connect("$mysql_hostname","$mysql_user","$mysql_password");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("$mysql_database", $con);
$all = mysql_query("SELECT Balance FROM tbltest");
?>

And then in my HTML side, I have this:

<input type="text" name="Balance" value="<?php echo $all; ?>" />

But when I tried to run the code the value that appears in the textbox is:

Resource Id #4

What did I miss? Thank you for the help

</div>
  • 写回答

2条回答 默认 最新

  • dongsisui7562 2014-01-27 02:22
    关注

    mysql_query does not return the result itself, but returns the resource that holds the result.

    You can use mysql_fetch_assoc or other relevant to fetch one row from that resource.

    The code should be like: $con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);

    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db($mysql_database, $con);
    $res = mysql_query("SELECT Balance FROM tbltest");
    
    $all = mysql_fetch_assoc($res); //actually $all is not all the result, it's first row.
                                    // $all might be like array('Balance' => '100');
    

    and HTML part, as arrays could not be echoed directly('Array' is shown instead), you have to specify the index of $all:

    <input type="text" name="Balance" value="<?php echo $all['Balance']; ?>" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?