这让我困惑了几个小时,我已经达到了放弃的程度。
我的项目有3个相关文件:
dbconfig.php
register.php
testmysql.php
dbconfig.php具有以下功能:
public function connString()
{
$connString['host'] = $this->host;
$connString['username'] = $this->username;
$connString['password'] = $this->password;
$connString['database'] = $this->database;
return $connString;
}
我在testmysql.php
执行此操作并且完美无缺:
require('dbconfig.php');
$dbObject = new dbconfig();
$conn = $dbObject->connString();
mysql_connect($conn['host'],$conn['username'],$conn['password']) or die(mysql_error());
mysql_select_db($conn['database']) or die(mysql_error());
但是当我在register.php
中做同样的事情时,却失败了:
<div class="div_texbox">
<select id="field1" name="field1" class="textbox">
<option value="0">Select</option>
<?php
require('dbconfig.php');
$dbObject = new dbconfig();
$conn = $dbObject->connString();
mysql_connect($conn['host'],$conn['username'],$conn['password']) or die(mysql_error());
mysql_select_db($conn['database']) or die(mysql_error());
$result = mysql_query("SELECT * FROM table") or die ('error selecting');
while ($row = mysql_fetch_array( $result ))
{
echo "<option value=\"" . $row['ID'] . "\">" . $row['value'] . "</option>";
}
?>
</select>
</div>
我何时说“失败”的详细信息&#39;
Field1
是一个应该填充的选择框,但它没有显示,表单中没有其他字段显示(可能是因为die
)。
我尝试安装PHP调试器以查看抛出的消息,但无法使其工作。
如果我在连接参数中进行硬编码,则一切正常。
var_dump
$conn
显示它在所有正确的索引处保留所有正确的值(在testmysql.php
有效的情况下也很明显。)
此外,它的一些故障排除使它似乎在mysql_connect
我分析了渲染页面的HTML代码以查看PHP脚本的输出,这就是它所显示的内容:
( ! ) Fatal error: Cannot redeclare class dbconfig in C:\wamp\www\site\dbconfig.php on line 10
Call Stack
#TimeMemoryFunctionLocation
10.0005698544{main}( )..\register.php:0
21.2728763824require( 'C:\wamp\www\site\dbconfig.php' )..\register.php:106
dbconfig构造函数
public function __construct($config = NULL)
{
if ($config != NULL)
{
$this->host = $config['hostname'];
$this->database = $config['database'];
$this->username = $config['username'];
$this->password = $config['password'];
}
else
{
$this->host = $this->defaultConfig['hostname'];
$this->database =$this->defaultConfig['database'];
$this->username = $this->defaultConfig['username'];
$this->password = $this->defaultConfig['password'];
}
}
答案 0 :(得分:1)
您似乎不止一次地包含dbconfig.php
,这导致无法重新声明类致命错误。
更改
的任何参考require('dbconfig.php')
到
require_once 'dbconfig.php';