简体   繁体   English

从数据库中获取和显示数据(JavaScript XML HTTP请求)

[英]Fetching & displaying data from a database (Javascript XML HTTP Request)

I'm really struggling with this task for my course and hope someone doesn't mind helping out or just offering guidance here. 在我的课程中,我真的很努力地完成这项任务,希望有人不介意提供帮助或在此处提供指导。 Basically I'm trying to create a simple Javascript XML Http Request to display basic information (the country_name & country_capital fields) from the database just in the html page. 基本上,我试图创建一个简单的Javascript XML Http Request,以仅在html页面中显示来自数据库的基本信息(country_name和country_capital字段)。 Below I just describe the apparent stages from the guide, and what I have done. 下面,我仅介绍指南中的明显步骤以及我所做的事情。

Firstly the 'database.html' page contains javascript XHR code which I think is mostly correct, but may have an error. 首先,“ database.html”页面包含我认为大部分正确的javascript XHR代码,但可能有错误。 To be honest I'm not 100% sure what else it does other than somehow refer to the getcountries.php file. 老实说,我不是100%知道除了以某种方式引用getcountries.php文件外,它还能做什么。

Secondly the getcountries.php file is where I'm really struggling as I've never coded in PHP. 其次,由于我从未用PHP编写过代码,因此我在getcountries.php文件中确实遇到了困难。 I think it's supposed to fetch the data from the local server (I'm running XAMPP) and echo the results on the web page. 我认为应该从本地服务器(我正在运行XAMPP)中获取数据并在网页上回显结果。

The database on phpMyAdmin is simple with just a table of countries including a primary key ID number, the country name, capital and currency, with the details below: Database name = countries_db Table name = countries_table Table fields: country_ID (primary key) country_name country_capital country_currency An example entry: 2, USA, Washington DC, US Dollar phpMyAdmin上的数据库很简单,只有一个国家/地区表,包括一个主键ID号,国家/地区名称,资本和货币,其详细信息如下:数据库名称= countrys_db表名称= country_table表字段:country_ID(主键)country_name country_capital country_currency示例条目:2,美国,华盛顿特区,美元

To summarise, my question is this: how can I edit what I've done to correctly fetch the data from the database and display it on the page? 总而言之,我的问题是:如何编辑所做的操作以正确地从数据库中获取数据并将其显示在页面上?

Really appreciate any help or advice here, thanks a lot. 非常感谢您在这里提供的任何帮助或建议,非常感谢。

 <!-- 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); ?> 

Assuming that this is the structure of your data base: 假设这是数据库的结构:

Database name = countries_db 
Table name = countries_table 
Table fields: 
    country_ID (primary key) 
    country_name 
    country_capital 
    country_currency 

The problem is that you have some syntax error in your code change this lines: 问题是您的代码中有一些语法错误,请更改以下行:

mysqli_select-db($con,"countries_db");
$sql="SELECT country_name AND country_capital FROM records";

with: 与:

mysqli_select_db($con,"countries_db");
$sql="SELECT country_name, country_capital FROM countries_table";

Alternative : using PDO: 替代方法 :使用PDO:

Try this instead of your getcountries.php implementation 试试这个,而不是您的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>";
?>

在您的getcountries.php中使用mysqli_select_db代替mysqli_select-db

mysqli_select_db($con,"countries_db");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM