函数从数据库表中填充第一个列表框,但不是第二个列表框 [英] Function populates first listbox but not the second from a database table

查看:99
本文介绍了函数从数据库表中填充第一个列表框,但不是第二个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数 popList()带有两个参数 - 列的名称和表的名称。
基本上, popList()函数连接到一个 mysql 服务器,然后是一个数据库并构建一个sql查询根据您传递给函数的内容来检索数据,以便在页面上填充列表框。



只有一个listbox可以正常工作,但如果有页面上的第二个列表框再次调用具有不同参数(列名称和表名称)的函数,并将其显示为空,并且在第一个已填充列表框源代码右下的页面源代码中打印以下内容:

$ b_b


警告:mysql_connect()[function.mysql-connect]:访问
拒绝用户'ODBC'@'
C:\xampp\htdocs\test on line 7
未连接:$ b本地主机(使用密码:NO) $ b拒绝访问用户'ODBC'@'localhost'(使用密码:NO)

我在两者中使用相同的连接凭证呼叫这是默认的localhost,root和没有密码(纯粹是为了测试目的)

为什么它的行为如此?



编辑* (来源)*:

 <?php 
函数popList($ col,$ tbl){

require_oncecredentials.php;

//打开一个到MySQL服务器的连接
$ connection = mysql_connect($ host,$ username,$ password);
if(!$ connection){
die('Not connected:'。mysql_error());
}

//设置活动的MySQL数据库
$ db_selected = mysql_select_db($ database,$ connection);
if(!$ db_selected){
die('不能使用db:'。mysql_error());
}

$ sql =SELECT $ field FROM $ table;
$ query = mysql_query($ sql);

while($ row = mysql_fetch_array($ query)){
echo'< option value ='。$ row [$ field]。'>'。 $ row [$ field]。 < /选项>;
}

mysql_close($ connection);
}
?>

解决方案

:看起来像使用 require_once include_once 会导致问题,但使用 include require 实际上解决了这个问题。



<使用 credentials.php 中的实际代码替换 require_oncecredentials.php,如下所示:

 函数popList(...){

需要credentials.php; //改为require_once需要。同样包含


...
}


解决方案

require_once 完成以下操作:它需要一次。第一次调用 popList 时, credentials.php 中的所有内容都会被转储到函数的作用域中。第二次调用 popList 什么也没有发生,因为 credentials.php 已经需要一次



你应该有一个配置对象而不是一堆文件中的变量,你不应该使用 require 在这样的函数中包含,并且应该打开 E_STRICT ,以便您看到关于 $ password 等的警告没有被定义。


Function popList() takes two parameters - name of the column and name of the table. Basically the popList() function connects to a mysql server and then a database and builds an sql query to retrieve data based on what you passed to the function to be used to populate a listbox on the page.

Only one listbox works fine with the function, but if there's a second listbox on the page that calls the function again with different parameters(column name and table name) passed it appears empty and the following is printed in the source of the page right under the source of the first populated listbox:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\test on line 7
Not connected : Access denied for user 'ODBC'@'localhost' (using password: NO)

I use the same connection credentials in both calls which are default localhost, root and no password(purely for testing purposes)

Why is it behaving like this?

EDIT*(source)*:

<?php
function popList($col, $tbl) {

    require_once "credentials.php";

    // Opens a connection to a MySQL server
    $connection=mysql_connect($host, $username, $password);
    if (!$connection) {
      die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
      die ('Can\'t use db : ' . mysql_error());
    }

    $sql = "SELECT $field FROM $table";
    $query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) {
        echo '<option value="' . $row["$field"] . '">' . $row["$field"] . '</option>';
    }

    mysql_close($connection);
}
?>

SOLUTION:

NEW: Seems like using require_once and include_once causes the issue, but using include or require actually fixes the problem.

Replaced require_once "credentials.php" with the actual code from credentials.php like so:

function popList(...) {

require "credentials.php"; // changed to require from require_once. same with include


...
}

解决方案

require_once does what it says: it requires once. The first time popList is called, everything in credentials.php gets dumped into your function's scope. The second time popList is called, nothing happens because credentials.php was already required once during this run.

You should have a configuration object somewhere rather than a pile of variables in a file, you should never use require or include within a function like that, and you should have E_STRICT turned on so you'll see the warnings about $password etc not being defined.

这篇关于函数从数据库表中填充第一个列表框,但不是第二个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆