我在本课程中确实很努力地完成这项任务,希望有人不介意提供帮助或只是在此处提供指导。基本上,我试图创建一个简单的Javascript XML Http Request,以仅在html页面中显示数据库中的基本信息(country_name和country_capital字段)。下面,我仅描述指南中的明显步骤以及我所做的事情。
首先,“ database.html”页面包含我认为大部分正确的javascript XHR代码,但可能会有错误。老实说,我不是100%知道除了以某种方式引用getcountries.php文件外,它还能做什么。
其次,由于我从未使用过PHP编码,因此我在getcountries.php文件中确实很费劲。我认为应该从本地服务器(我正在运行XAMPP)中获取数据并在网页上回显结果。
phpMyAdmin上的数据库很简单,只有一个国家/地区表,包括一个主键ID号,国家/地区名称,资本和货币,其详细信息如下:
数据库名称= country_db
表名称= country_table
表格栏位:
country_ID(主键)
国家的名字
country_capital
country_currency
条目示例:2,美国,华盛顿特区,美元
总而言之,我的问题是:如何编辑所做的操作以正确地从数据库中获取数据并将其显示在页面上?
非常感谢您在这里提供的任何帮助或建议,非常感谢。
<!-- Code on Page 1 (database.html) -->
<p id="txtHint"></p>
<p id="hint"></p>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) { // detects whether the browser has XMLHttpRequest functionality
// code for modern browsers
xmlhttp=new XMLHttpRequest(); // creates an XMLHttpRequest object
} else { // code for old browsers
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() { // onreadystatechange defines the function to be called when the readyState property changes
if (this.readyState==4 && this.status==200) {
document.getElementById("hint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getcountries.php?q="+str,true);
xmlhttp.send();
}
</script>
<!-- Code on Page 2 (getcountries.php) -->
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','');
if (!$con) {
die('Could not connect: ' .mysqli_error($con));
}
mysqli_select-db($con,"countries_db");
$sql="SELECT country_name AND country_capital FROM records";
$result = mysqli_query($con,$sql);
echo "Results:"
error_reporting(E_ERROR | E_PARSE);
\
while($row = mysqli_fetch_array($result)) {
echo $row['country_name'] . "<br>";
echo $row['country_capital'] . "<br>";
}
mysqli_close($con);
?>
最佳答案
假设这是数据库的结构:
Database name = countries_db
Table name = countries_table
Table fields:
country_ID (primary key)
country_name
country_capital
country_currency
问题是您的代码中有一些语法错误,请更改以下行:
mysqli_select-db($con,"countries_db");
$sql="SELECT country_name AND country_capital FROM records";
与:
mysqli_select_db($con,"countries_db");
$sql="SELECT country_name, country_capital FROM countries_table";
备选:使用PDO:
试试这个代替您的getcountries.php实现
<?php
$driver = 'mysql';
$database = "dbname=countries_db";
$dsn = "$driver:host=localhost;unix_socket=/home/cg/mysql/mysql.sock;$database";
$username = 'root';
$password = 'root';
try {
$conn = new PDO($dsn, $username, $password);
echo "<h2>Database countries_db Connected<h2>";
}catch(PDOException $e){
echo "<h1>" . $e->getMessage() . "</h1>";
}
$sql = 'SELECT country_name, country_capital FROM countries_table';
$stmt = $conn->prepare($sql);
$stmt->execute();
echo "Results:";
echo "<table style='width:100%'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
foreach($row as $value)
{
echo sprintf("<td>%s</td>", $value);
}
echo "</tr>";
}
echo "</table>";
?>
关于javascript - 从数据库中获取和显示数据(JavaScript XML HTTP请求),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42940671/