设置 - >使用host gator,php,phpmyadmin,mysql:
PHP代码(在名为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\n", 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表:
用户和数据库的mysql访问级别:
用户已添加到数据库中
当我点击页面时,这是我得到的问题:
UPDATE - 更改SQL以读取SELECT * FROM项目...现在,当我尝试点击页面时获取此信息(点击错误代码并运行exit()但打印出没有错误:
为什么我收到一个表不存在错误?我已经缩短了数据库的名称并重建了一个新的数据库,但它仍然说它不存在!
答案 0 :(得分:4)
在Windows表名称不区分大小写,而在* nix系统表名称区分大小写。
主机gator可能在unix上运行,因此如果你的表名为
items
你需要这样称呼它
$result = mysqli_query($con,"SELECT * FROM items");
答案 1 :(得分:1)
这是更正后的代码。我在代码中添加了注释。
<?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\n", 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);
顺便说一句:如果你真的想学习并以正确的方式去做,那就不要使用W3schools。很多东西已经过时且具有误导性。