我正在创建一个搜索功能或进程,从数据库中选择并在列表框中显示数据,用于选择已存在于MySQL数据库中的国家但我收到错误,我不知道如何修复它可以任何人帮助我我是新来的PHP。错误是(警告:mysql_fetch_array()期望参数1是资源,布尔值在第36行的C:\ wamp \ www \ Unnamed Site 2 \ resources \ searchForm.php中给出)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search page</title>
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.searchtitle {
text-align: center;
}
</style>
</head>
<body>
<!--<?php require_once('header.php'); ?>-->
<table width="80%" height="432">
<tr>
<td width="14%" valign="top"><!--<?php require_once('leftsideBar.php'); ?>-->
</td>
<td width="86%" valign="top"><h2 class="searchtitle">Search Types</h2>
<table width="95%" height="94">
<form action="searchProcess.php" method="post" id="searchForm">
<tr>
<td width="20%"><label for="searchByName2">By Name</label>
<select name="searchByName" id="searchByName2">
</select></td>
<td width="20%"><label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('Connections/connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT * FROM country");
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option id="<?php echo $viewallCountries['country_id']; ?> "><?php echo $viewallCountries['country_name'] ?></option>
<?php } ?>
</select></td>
<td width="25%"><label for="searchByGovernorate">By Governorate</label>
<select name="searchByGovernorate" id="searchByGovernorate">
</select></td>
<td width="15%"><label for="searchByCity">By City</label>
<select name="searchByCity" id="searchByCity">
</select></td>
<td width="20%"><label for="searchBySpecialization">By Specialization</label>
<select name="searchBySpecialization" id="searchBySpecialization">
</select></td>
</tr>
</form>
</table>
</tr>
</table>
</body>
</html>
<?php
$hostname_conn = "localhost";
$database_conn = "lam_el_chamel_db";
$username_conn = "root";
$password_conn = ".....";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>
我添加我的connfig文件
答案 0 :(得分:3)
mysql_query会返回FALSE
。您需要检查返回的值。如果是FALSE
,则可以使用mysql_error从数据库中获取错误消息。
答案 1 :(得分:0)
你也可以在这里使用mysql_num_rows
,它会检查查询是否返回任何行。
$getallCountries = mysql_query("SELECT * FROM country");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
// insert inside option
}
}
您没有选择数据库使用此代码来选择db
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_conn, $conn);
将mysql_fetch_array
更改为mysql_fetch_assoc
。