dqjl0906 2014-06-05 00:41
浏览 29
已采纳

在PHP中显示SQL查询结果出错[关闭]



When i try to retrieve the results from my sql query within' php it doesn't shows me it right.

My result:

Array ( [0] => 0533 [SUBSTRING(cardnumber, 5, 4)] => 0533 )

While i just try to get the result that is

0533 

to be shown.

My php File:

<?php
    $con=mysqli_connect("localhost","root","wickedphat", "bluecard");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con,"SELECT SUBSTRING(cardnumber, 5, 4) FROM users WHERE username='".($_SESSION['username'])."'");

    while($row = mysqli_fetch_array($result))
      {
      print_r($row) . " " ;
      }

    mysqli_close($con);
    ?>

and in my html file ive put this, where it suppose to show the result.

<?php include_once '../../includes/number1.php' ?>
  • 写回答

2条回答 默认 最新

  • douxianglu4370 2014-06-05 00:46
    关注

    Those results are correct and expected with the code you provided. Two things to note:

    1. You should give the result of your MySQL function call an alias to make access it via PHP easier
    2. You need to refer to the identifier of each result in PHP, not the whole row of results

    This should demonstrate this:

    // Gave the result of SUBSTRING(cardnumber, 5, 4) the alias
    // of "cardnum"
    $result = mysqli_query($con,"SELECT SUBSTRING(cardnumber, 5, 4) as cardnum FROM users WHERE username='".($_SESSION['username'])."'");
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        // accessing the value of cardnum by refering to its key in the
        // the array returned by mysqli_fetch_array()
        echo $row['cardnum'] . ' ';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求