此代码应回答 OP 关于他当前代码的问题:
$query = mysql_query("
SELECT `first_name`, `last_name`,`county`
FROM `contact`
WHERE `first_name` ='". mysql_real_escape_string(trim($_POST['name'])) ."'
");
(mysql_num_rows($query) !== 0) ? print mysql_result($query,0,'first_name') : print 'Name not found';
?>
另外根据您的错误,请确保此行:
`last_name`,`county`
目前不是
`last_name`.`county`
注意逗号/句点的区别。
或者,如果您尝试显示名字、姓氏和县,请使用:
$query = mysql_query("
SELECT `first_name`, `last_name`,`county`
FROM `contact`
WHERE `first_name` ='". mysql_real_escape_string(trim($_POST['name'])) ."'
");
(mysql_num_rows($query) !== 0) ? print mysql_result($query,0,'first_name')." ".mysql_result($query,0,'last_name')." ".mysql_result($query,0,'county') : print 'Name not found';
?>
最终获得更好的性能和支持的代码使用
$mysqli = new mysqli("your_host", "your_user", "your_password", "your_database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT `first_name`, `last_name`,`county`
FROM `contact`
WHERE `first_name` ='". mysql_real_escape_string(trim($_POST['name'])) ."'
";
$result = $mysqli->query($query);
while($row = $result->fetch_array()){
$rows[] = $row;
}
if($result->num_rows === 0){
print 'Name not found';
}
foreach($rows as $row){
print $row['first_name']." ".$row['last_name']." ".$row['county'];
}
/* free result set */
$result->close();
/* close connection */
$mysqli->close();