jQuery自动填充以填充表单如何连接到mysql
此代码显示数组结果,我想连接mysql数据库如何连接到mysql请帮我解决这个问题
autocomplete.htm
<link rel="stylesheet" href="http://static.jquery.com/ui/css/base2.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
source: "/ajax/demo_cities.php",
select: function(event, ui){
$("#city").val(ui.item.city);
$("#state").val(ui.item.state);
$("#zip").val(ui.item.zip);
},
minLength:1
};
$("#city").autocomplete(ac_config);
});
</script>
<form action="#" method="post">
<p><label for="city">City</label><br />
<input type="text" name="city" id="city" value="" /></p>
<p><label for="state">State</label><br />
<input type="text" name="state" id="state" value="" /></p>
<p><label for="zip">Zip</label><br />
<input type="text" name="zip" id="zip" value="" /></p>
</form>
demo_cities.php
<?php
// Data could be pulled from a DB or other source
$cities = array(
array('city'=>'New York', state=>'NY', zip=>'10001'),
array('city'=>'Los Angeles', state=>'CA', zip=>'90001'),
array('city'=>'Chicago', state=>'IL', zip=>'60601'),
array('city'=>'Houston', state=>'TX', zip=>'77001'),
array('city'=>'Phoenix', state=>'AZ', zip=>'85001'),
array('city'=>'Philadelphia', state=>'PA', zip=>'19019'),
array('city'=>'San Antonio', state=>'TX', zip=>'78201'),
array('city'=>'Dallas', state=>'TX', zip=>'75201'),
array('city'=>'San Diego', state=>'CA', zip=>'92101'),
array('city'=>'San Jose', state=>'CA', zip=>'95101'),
array('city'=>'Detroit', state=>'MI', zip=>'48201'),
array('city'=>'San Francisco', state=>'CA', zip=>'94101'),
array('city'=>'Jacksonville', state=>'FL', zip=>'32099'),
array('city'=>'Indianapolis', state=>'IN', zip=>'46201'),
array('city'=>'Austin', state=>'TX', zip=>'73301'),
array('city'=>'Columbus', state=>'OH', zip=>'43085'),
array('city'=>'Fort Worth', state=>'TX', zip=>'76101'),
array('city'=>'Charlotte', state=>'NC', zip=>'28201'),
array('city'=>'Memphis', state=>'TN', zip=>'37501'),
array('city'=>'Baltimore', state=>'MD', zip=>'21201'),
);
// Cleaning up the term
$term = trim(strip_tags($_GET['term']));
// Rudimentary search
$matches = array();
foreach($cities as $city){
if(stripos($city['city'], $term) !== false){
// Add the necessary "value" and "label" fields and append to result set
$city['value'] = $city['city'];
$city['label'] = "{$city['city']}, {$city['state']} {$city['zip']}";
$matches[] = $city;
}
}
// Truncate, encode and return the results
$matches = array_slice($matches, 0, 5);
print json_encode($matches);
答案 0 :(得分:0)
要使用PHP连接到MySQL数据库,您可以使用以下两种方法之一:MySQL或MySQLi。
编辑:连接到数据库并返回数组后,您可以使用当前代码为自动填充查询准备数据。
MySQL示例:
<?php
// Connect to server and select database
$conn = $mysql_connect('HOST', 'USER', 'PASS');
$db = $mysql_select_db('DATABASE');
// To retrieve data from database:
$query = "SELECT column_name FROM table_name"; // Your query
$result = mysql_query($query);
if($result) {
if(mysql_num_rows($result) != 0 {
while($row = mysql_fetch_assoc($result)) {
echo $row[column_name];
}
} else {
echo "Your query returned 0 rows.";
} else {
echo "Your query is invalid.<br />MySQL error " . $mysql_error();
}
?>
库MySQLi:
<?php
$mysqli = new mysqli('HOST', 'USER', 'PASS', 'DATABASE');
$query = "SELECT column_name FROM table_name WHERE column_name = ?";
$result = $mysqli->query($query);
while($row = $result->fetch_array()) {
$rows[] = $row;
}
?>
检索数据的快速和简单方法。我建议使用if / else语句进行错误检查,以帮助您解决问题并提醒最终用户注意问题。
答案 1 :(得分:0)
如果我正确地理解了你的问题,你只想从你的数组获得相同的结果,但是要从你的php代码中的数据库获得。如果是这样,你可以沿着这些方向使用......
<?php
$conn = mysql_connect('host', 'user', 'pass'); //You want to replace that with your connection details, though it should be in a class so you aren't reconnecting every page call
if(!$conn) {
echo json_encode(array('success' => false, 'reason' => 'Could not connect'));
exit;
}
@mysql_select_db('dbname'); // replace with your database name
$term = trim(strip_tags($_REQUEST['term']));
$sql = "SELECT `city`, `state`, `zip` FROM tablename WHERE city LIKE '".$term."%'";
$results = mysql_query($sql);
if($results) {
while($row = mysql_fetch_assoc($results)) {
$matches[] = array('city' => $row['city'], 'label' => "{$row['city']}, {$row['state']}, {$row['zip']}"; //honestly not sure what thats about but you had it so im adding it
}
$matches = array_slice($matches, 0, 5); // again not sure about this, but you have it so here it is
} else {
$matches = array('success' => false, 'reason' => 'no matches found');
}
echo json_encode($matches);
?>
答案 2 :(得分:-1)
将第13行的代码更改为:
$matches[] = array('city' => $row['city'], 'label' => "{$row['city']}, {$row['state']}, {$row['zip']}"