溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP Ajax如何請求MySQL數(shù)據(jù)庫

發(fā)布時間:2021-10-25 16:52:43 來源:億速云 閱讀:240 作者:柒染 欄目:數(shù)據(jù)庫

PHP Ajax如何請求MySQL數(shù)據(jù)庫,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

數(shù)據(jù)庫:PHP Ajax如何請求MySQL數(shù)據(jù)庫

前臺頁面:choseForm.php

<html>

<head>

<script type="text/javascript">

function showUser(str){

var xmlhttp;

if (str=="")

 {

 document.getElementById("txtHint").innerHTML="";

 return;

 }

if (window.XMLHttpRequest)

 {// code for IE7+, Firefox, Chrome, Opera, Safari

 xmlhttp=new XMLHttpRequest();

 }

else

 {// code for IE6, IE5

 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

 }

xmlhttp.onreadystatechange=function()

 {

 if (xmlhttp.readyState==4 && xmlhttp.status==200)

   {

   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

   }

 }

xmlhttp.open("GET","getuser.php?q="+str,true);

xmlhttp.send();

}

</script>

</head>

<body>

<form> 

Select a User:

<select name="users" onchange="showUser(this.value)">

<option value="0">Please Choose</option>

<option value="1">Peter Griffin</option>

<option value="2">Lois Griffin</option>

<option value="3">Glenn Quagmire</option>

<option value="4">Joseph Swanson</option>

</select>

</form>

<div id="txtHint">客戶信息將在此處列出 ...</div>

</body>

</html>

后臺文件:getuser.php

<?php

$q=$_GET["q"];

    $mysql_server_name="localhost"; //數(shù)據(jù)庫服務器名稱

    $mysql_username="root"; // 連接數(shù)據(jù)庫用戶名

    $mysql_password="cxst789"; // 連接數(shù)據(jù)庫密碼

    $mysql_database="user"; // 數(shù)據(jù)庫的名字

    // 連接到數(shù)據(jù)庫

    $con=mysql_connect($mysql_server_name, $mysql_username,

                        $mysql_password);

if (!$con)

 {

 die('Could not connect: ' . mysql_error());

 }

mysql_select_db("user", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>

<tr>

<th>Firstname</th>

<th>Lastname</th>

<th>Age</th>

<th>Hometown</th>

<th>Job</th>

</tr>";

while($row = mysql_fetch_array($result))

 {

 echo "<tr>";

 echo "<td>" . $row['FirstName'] . "</td>";

 echo "<td>" . $row['LastName'] . "</td>";

 echo "<td>" . $row['Age'] . "</td>";

 echo "<td>" . $row['Hometown'] . "</td>";

 echo "<td>" . $row['Job'] . "</td>";

 echo "</tr>";

 }

echo "</table>";

mysql_close($con);

?>

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI