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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化