dongzhila3786 2014-06-24 03:42
浏览 61
已采纳

为什么我得到一个表使用PHPmyadmin和mysql在PHP中不存在错误?

Setup -> Using host gator , php, phpmyadmin, mysql:

PHP Code (in the file called ajaxTest.php):

<?php

$con=mysqli_connect("localhost","refinedc_dbadmin","password","refinedc_currency");

// Check connection
if (mysqli_connect_errno()) 
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{



$result = mysqli_query($con,"SELECT * FROM ITEMS");

if (!$check1_res) {
    printf("Error: %s
", mysqli_error($con));
    exit();
}

echo '[';

while($row = mysqli_fetch_array($result)) 
{
  echo '{';
  echo '"ID":' . '"' . $row['ID'] . '",';
  echo '"YEAR":' . '"' . $row['YEAR'] . '",';
  echo '"QUANTITY":' . '"' . $row['QUANTITY'] . '",';
  echo '"DENOMINATION":' . '"' . $row['DENOMINATION'] . '",';
  echo '"TYPE":' . '"' . $row['TYPE'] . '",';
  echo '"COUNTRY":' . '"' . $row['COUNTRY'] . '",';
  echo '"COIN_NAME_OR_TITLE":' . '"' . $row['COIN_NAME_OR_TITLE'] . '",';
  echo '"COLLECTIBLE_METAL_ONE":' . '"' . $row['COLLECTIBLE_METAL_ONE'] . '",';
  echo '"COLLECTIBLE_METAL_TWO":' . '"' . $row['COLLECTIBLE_METAL_TWO'];
  echo '},';
}

echo ']';

}
mysqli_close($con);

?>

phpmyadmin table: enter image description here


mysql access levels for user and database: enter image description here


user is added to the database: enter image description here


And when i hit the page this is the issue i get: enter image description here


UPDATE - Changed the SQL to read SELECT * FROM items... Now get this when I try to hit the page (hits the error code and runs exit() but prints out no error: enter image description here


Why am i receiving a table does not exist error? I have already shortened the name of the database and rebuilt a new one, but it still says it doesn't exist!

  • 写回答

2条回答 默认 最新

  • duansanzi5265 2014-06-25 03:31
    关注

    Here is the corrected code. I have added comment in the code itself.

    <?php
    
    $con=mysqli_connect("localhost","refinedc_dbadmin","password","refinedc_currency");
    
    // Check connection
    if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {
    
        $result = mysqli_query($con,"SELECT * FROM `items`");
        // Check if result is ok
        // if there is an error $result will be false and the error details is available in mysqli_error()
        if (!$result) {
            printf("Error: %s
    ", mysqli_error($con));
            exit();
        }
    
        // Use json_encode to convert data to json.
        // It is too tedious and error prone to convert data to json by hand.
        $data = array();
        while($row = mysqli_fetch_array($result)) 
        {
            //Store the result to array
            $data[]=array(
                "ID" => $row['ID'],
                "YEAR" => $row['YEAR'],
                "QUANTITY" => $row['QUANTITY'],
                "DENOMINATION" => $row['DENOMINATION'],
                "TYPE" => $row['TYPE'],
                "COUNTRY" => $row['COUNTRY'],
                "COIN_NAME_OR_TITLE" => $row['COIN_NAME_OR_TITLE'],
                "COLLECTIBLE_METAL_ONE" => $row['COLLECTIBLE_METAL_ONE'],
                "COLLECTIBLE_METAL_TWO" => $row['COLLECTIBLE_METAL_TWO']
            );
        }
        //echo the json
        echo json_encode($data);
    }
    mysqli_close($con);
    

    BTW: don't use W3schools if you really want to learn and do it in the correct way. Lot of the things there are outdated and misleading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置