Hướng dẫn can javascript call mysql? - javascript có thể gọi mysql không?







";
while($row = mysqli_fetch_array($result)) {
  echo "";
  echo "";
  echo "";
  echo "";
  echo "";
  echo "";
  echo "";
}
echo "


AJAX có thể được sử dụng để giao tiếp tương tác với cơ sở dữ liệu.


Ví dụ cơ sở dữ liệu AJAX

Ví dụ sau đây sẽ chứng minh làm thế nào một trang web có thể tìm nạp thông tin từ cơ sở dữ liệu với AJAX:

Thí dụ

Thông tin người sẽ được liệt kê ở đây ...


Ví dụ giải thích - Cơ sở dữ liệu MySQL

Bảng cơ sở dữ liệu chúng tôi sử dụng trong ví dụ trên có vẻ như thế này:

TôiHọHọTuổi tácQuê nhàNghề nghiệp
1 PeterGriffin41 QuahogNhà máy bia
2 LoisGriffin40 QuahogNhà máy bia
3 LoisNewport39 QuahogNhà máy bia
4 LoisNewport41 QuahogNhà máy bia

Lois

Newport

Giáo viên piano

Joseph

Thí dụ

Swanson



Cảnh sát




Person info will be listed here...

GLenn

Quigmire

Phi công

Ví dụ giải thích

  • Trong ví dụ trên, khi người dùng chọn một người trong danh sách thả xuống ở trên, một hàm gọi là "showuser ()" được thực thi.
  • Hàm được kích hoạt bởi sự kiện Onchange.
  • Đây là mã HTML:
  • Hàm showuser (str) {& nbsp; if (str == "") {& nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ("txthint"). & nbsp; } other {& nbsp; & nbsp; & nbsp; & nbsp; var xmlHttp = new xmlHttpRequest (); & nbsp; & nbsp; & nbsp; xmlHttp.onReadyStateChange = function () {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; if (this.ReadyState == 4 && this.status == 200) {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ("txthint"). Internhtml = this.responsetext; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;} thật); & nbsp; & nbsp; & nbsp; xmlhttp.send (); & nbsp; }}


& nbsp; Chọn một người: & nbsp; Peter Griffin & NBSP; Lois Griffin & NBSP; Joseph Swanson & NBSP; Glenn Quagmire & NBSP; Thông tin người sẽ được liệt kê ở đây ...

The page on the server called by the JavaScript above is a PHP file called "getuser.php".

Chạy ví dụ »

Giải thích mã:




GLenn
$q = intval($_GET['q']);

Quigmire
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}

Phi công
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

Ví dụ giải thích

Firstname Lastname Age Hometown Job
" . $row['FirstName'] . "" . $row['LastName'] . "" . $row['Age'] . "" . $row['Hometown'] . "" . $row['Job'] . "
";
mysqli_close($con);
?>

Trong ví dụ trên, khi người dùng chọn một người trong danh sách thả xuống ở trên, một hàm gọi là "showuser ()" được thực thi.

  1. Hàm được kích hoạt bởi sự kiện Onchange.
  2. Đây là mã HTML:
  3. Hàm showuser (str) {& nbsp; if (str == "") {& nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ("txthint"). & nbsp; } other {& nbsp; & nbsp; & nbsp; & nbsp; var xmlHttp = new xmlHttpRequest (); & nbsp; & nbsp; & nbsp; xmlHttp.onReadyStateChange = function () {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; if (this.ReadyState == 4 && this.status == 200) {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ("txthint"). Internhtml = this.responsetext; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;} thật); & nbsp; & nbsp; & nbsp; xmlhttp.send (); & nbsp; }}