新手在这里,需要帮助。
我使用http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/
中的代码它适用于某些领域。我的问题在于我将国家和州存储在另一个表中,并且我得到了#34;试图获得非对象的属性"错误" if($ address_query-> num_rows){"。
autocomplete.php
$dbhost = 'SERVER';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASE_NAME';
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$address_query = "SELECT * FROM address where customer_id = '" . $customer_id . "' AND (firstname like :term OR lastname like :term) LIMIT 10";
if ($address_query->num_rows) { // got "Trying to get property of non-object" error
$country_query = "SELECT * FROM country WHERE country_id = '" . (int)$address_query->row['country_id'] . "'";
if ($country_query->num_rows) {
$country = $country_query->row['name'];
$iso_code_2 = $country_query->row['iso_code_2'];
} else {
$country = '';
$iso_code_2 = '';
}
$zone_query = "SELECT * FROM zone WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'";
if ($zone_query->num_rows) {
$zone = $zone_query->row['name'];
$zone_code = $zone_query->row['code'];
} else {
$zone = '';
$zone_code = '';
}
$result = $conn->prepare($address_query);
$result->bindValue(":term",$ac_term);
$result->execute();
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$return_arr [] = array(
'label' => $row['firstname'] .' '. $row['lastname'] .' '. $row['company'] .' '. $row['city'] .' '. $row['postcode'],
'value' => $row['firstname'] .' '. $row['lastname'],
'Company' => $row['company'],
'Country' => $row['iso_code_2'],
'postCode' => $row['postcode'],
);
}
}
echo json_encode($return_arr);
flush();
?>
如果我删除了代码
if ($query->num_rows) { // got "Trying to get property of non-object" error
$country_query = "SELECT * FROM country WHERE country_id = '" . (int)$address_query->row['country_id'] . "'";
if ($country_query->num_rows) {
$country = $country_query->row['name'];
$iso_code_2 = $country_query->row['iso_code_2'];
} else {
$country = '';
$iso_code_2 = '';
}
$zone_query = "SELECT * FROM zone WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'";
if ($zone_query->num_rows) {
$zone = $zone_query->row['name'];
$zone_code = $zone_query->row['code'];
} else {
$zone = '';
$zone_code = '';
}
适用于名称,公司和邮政编码。顺便说一下,国家和州的字段是下拉选择表。
任何帮助将不胜感激!
答案 0 :(得分:0)
你的$ address_query不是一个对象而是一个字符串,所以你得到了错误"试图得到非对象的属性"
要访问pdo对象,您需要通过您创建的$ conn访问它
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$address_query = "SELECT * FROM address where customer_id = '" . $customer_id . "' AND (firstname like :term OR lastname like :term) LIMIT 10";
$result = $conn->prepare($address_query);
$result->execute(array(':term'-> $term));
if ($result->rowCount()){
你的sql也没用了,因为你知道表格中的国家ID你应该加入表格
您的代码应如下所示:
$dbhost = 'SERVER';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASE_NAME';
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$address_query = "SELECT * FROM address LEFT JOIN country address.country_id = country. country_id ON where address.customer_id = '" . $customer_id . "' AND (address.firstname like :term OR address.lastname like :term) LIMIT 10";
$result = $conn->prepare($address_query);
$result->bindValue(":term",$ac_term);
$result->execute();
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$return_arr [] = array(
'label' => $row['firstname'] .' '. $row['lastname'] .' '. $row['company'] .' '. $row['city'] .' '. $row['postcode'],
'value' => $row['firstname'] .' '. $row['lastname'],
'Company' => $row['company'],
'Country' => $row['iso_code_2'],
'postCode' => $row['postcode'],
);
}}
答案 1 :(得分:0)
我明白了。我将行改为
$address_query = "SELECT *
FROM address
LEFT JOIN country
ON address.country_id = country.country_id
where address.customer_id = '" . $customer_id . "' AND (address.firstname like :term OR address.lastname like :term) LIMIT 10";
感谢所有帮助过我的人。