2012-08-05 30 views
0

我在这里有一个代码,其中它通过数据库记录......从不同的表中填充下拉列表。Mysqld意外终止,因为一段PHP代码

我有几个表,其中1台= 1个下拉列表

代码:(showhide_dropdown)

<?php 
$hostname = "localhost"; // usually is localhost, but if not sure, 
      check with your hosting company, 
      if you are with webune leave as localhost 
$db_user = "root"; // change to your database password 
$db_password = ""; // change to your database password 
$database = "minquep_test"; // provide your database name 
$db_table1 = "roles"; // leave this as is 
$db_table2 = "companies"; 
$db_table3 = "albury_branch"; 
$db_table4 = "minquep_branch"; 
$db_table5 = "countries"; 

$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db); 
?> 

<?php 
$roles_sql="SELECT role_id, role_name FROM $db_table1"; 
$comp_sql= "SELECT company_name FROM $db_table2"; 
$albury_sql= "SELECT albury_id, albury_name FROM $db_table3"; 
$minq_sql= "SELECT minquep_id, minquep_name FROM $db_table4"; 
$count_sql= "SELECT country_id, country_name FROM $db_table5"; 

$roles_result=mysql_query($roles_sql); 
$comp_result=mysql_query($comp_sql); 
$al_result=mysql_query($albury_sql); 
$minq_result=mysql_query($minq_sql); 
$count_result=mysql_query($count_sql); 

$roles_options=""; 
$comp_options=""; 
$al_options=""; 
$minq_options=""; 
$count_options=""; 

while ($roles_row=mysql_fetch_array($roles_result)) { 

    $roles_id=$roles_row["role_id"]; 
    $role=$roles_row["role_name"]; 
    $roles_options.="<OPTION VALUE=\"$role\">".$role; 
} 

while ($comp_row=mysql_fetch_array($comp_result)) { 

    $company=$comp_row["company_name"]; 
    $comp_options.="<OPTION VALUE=\"$company\">".$company; 
} 

while ($al_row=mysql_fetch_array($al_result)) { 

    $albury=$al_row["albury_name"]; 
    $al_options.="<OPTION VALUE=\"$albury\">".$albury; 
} 

while ($minq_row=mysql_fetch_array($minq_result)) { 

    $minquep=$minq_row["minquep_name"]; 
    $minq_options.="<OPTION VALUE=\"$minquep\">".$minquep; 
} 

while ($count_row=mysql_fetch_array($count_result)) { 

    $country=$count_row["country_name"]; 
    $count_options.="<OPTION VALUE=\"$country\">".$country; 
} 


?> 

首先,它确实工作得很好,但是当我打开网页,其中的代码被张贴...一个mysql.exe错误信息会突然弹出(正常的微软错误)说:意外的错误...类似的东西。

然后一个MySQL错误/警告将我的网页上显示..

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result 
     resource in C:\xampp\htdocs\minquep-native\pages\showhide_dropdown.php 
     on line 60 

我不明白这个问题。我是PHP新手请帮忙。谢谢。

回答

0

您的问题似乎与此查询:

SELECT country_id, country_name FROM countries 

确保列和表确实存在(尝试直接在mysql中执行查询,看看你会得到什么)。你得到的错误是因为查询无法执行。

+0

谢谢。我现在有另一个问题。我的phpmyadmin仍然拒绝访问。啧啧。再次感谢。可能它的国家表不存在。但是,我上次运行我的程序时看到了它......它真的很奇怪。 – xirukitepe 2012-08-05 14:57:22

0

所以,你所得到的错误是以前的错误的症状:

在第60行,你做这样的事情:

while ($comp_row=mysql_fetch_array($comp_result)) { 

mysql_fetch_array需要MySQL结果对象。但它已经得到了其他的东西(实际上是假的)。它已经否则得到的东西的原因是因为该行:

$comp_result=mysql_query($comp_sql); 

还给假,而不是Mysql的结果,改变这一设置:

$roles_result=mysql_query($roles_sql); 
$comp_result=mysql_query($comp_sql); 
$al_result=mysql_query($albury_sql); 
$minq_result=mysql_query($minq_sql); 
$count_result=mysql_query($count_sql); 

到:

$roles_result=mysql_query($roles_sql); 
if(mysql_errno() != 0) echo mysql_error(); 
$comp_result=mysql_query($comp_sql); 
if(mysql_errno() != 0) echo mysql_error(); 
$al_result=mysql_query($albury_sql); 
if(mysql_errno() != 0) echo mysql_error(); 
$minq_result=mysql_query($minq_sql); 
if(mysql_errno() != 0) echo mysql_error(); 
$count_result=mysql_query($count_sql); 
if(mysql_errno() != 0) echo mysql_error(); 

要看到什么发生错误。

注:

如果你使用像Laravel或任何其他一个框架,它会抓住这些错误的为您服务。另外我会使用mysqli不是mysql,因为mysql已经过时了,请参阅:http://php.net/manual/en/book.mysqli.phphttp://php.net/manual/en/mysqli.overview.php