0

我这里有一个代码,它通过数据库记录填充下拉列表......也来自不同的表。

我有几个表,其中 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 错误消息(正常的 Microsoft 错误)说:意外错误……类似的东西.

然后我的页面上将显示一个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 新手,请帮忙。谢谢。

4

2 回答 2

0

您的问题似乎与此查询有关:

SELECT country_id, country_name FROM countries

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

于 2012-08-05T14:46:51.193 回答
0

所以你得到的错误是早期错误的症状:

在第 60 行,您执行以下操作:

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

mysql_fetch_array需要一个 mysql 结果对象。但是它得到了其他东西(实际上是错误的)。它得到其他东西的原因是因为这条线:

$comp_result=mysql_query($comp_sql); 

返回 False 而不是 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.php http://php.net/manual/en/mysqli.overview.php

于 2014-09-28T15:12:43.977 回答