2012-08-05 29 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