PHP MySQL 数据库类不支持插入
我一直在使用这个类:
<?
class DbConnector {
var $theQuery;
var $link;
function DbConnector() {
$host = 'localhost';
$db = 'my_db';
$user = 'root';
$pass = 'password';
// connect to the db
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
function find($query) {
$ret = mysql_query($query, $this->link);
if (mysql_num_rows($ret) == 0)
return array();
$retArray = array();
while ($row = mysql_fetch_array($ret))
$retArray[] = $row;
return $retArray;
}
function insert($query) {
$ret = mysql_query($query, $this->link);
if (mysql_affected_rows() < 1)
return false;
return true;
}
function query($query) {
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
// get some results
function fetchArray($result) {
return mysql_fetch_array($result);
}
function close() {
mysql_close($this->link);
}
function exists($query) {
$ret = mysql_query($query, $this->link);
if (mysql_num_rows($ret) == 0)
return false;
}
function last_id($query) {
return mysql_insert_id($query);
}
}
?>
我正在使用它进行很多查询并且它工作正常,尽管我试图使用这个查询插入:
global $db;
$query = $db->insert("INSERT INTO `submissions` (
`id` ,
`quote` ,
`filename` ,
`date_added` ,
`uploaded_ip`
)
VALUES (
NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}')
");
并且它给了我这个错误:
Not Found
The requested URL /horh_new/id/<br /><b>Fatal error</b>: Call to a member function insert() on a non-object in <b>/Applications/MAMP/htdocs/horh_new/addit.php</b> on line <b>52</b><br /> was not found on this server.
虽然当我不使用该类时上面,并使用这个代替:
$con = mysql_connect("localhost","root","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO `submissions` (
`id` ,
`quote` ,
`filename` ,
`date_added` ,
`uploaded_ip`
)
VALUES (
NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}')
");
echo mysql_insert_id();
mysql_close($con);
它工作正常。谁能告诉我我的班级出了什么问题吗?我真的很感激。
I've been using this class:
<?
class DbConnector {
var $theQuery;
var $link;
function DbConnector() {
$host = 'localhost';
$db = 'my_db';
$user = 'root';
$pass = 'password';
// connect to the db
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
function find($query) {
$ret = mysql_query($query, $this->link);
if (mysql_num_rows($ret) == 0)
return array();
$retArray = array();
while ($row = mysql_fetch_array($ret))
$retArray[] = $row;
return $retArray;
}
function insert($query) {
$ret = mysql_query($query, $this->link);
if (mysql_affected_rows() < 1)
return false;
return true;
}
function query($query) {
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
// get some results
function fetchArray($result) {
return mysql_fetch_array($result);
}
function close() {
mysql_close($this->link);
}
function exists($query) {
$ret = mysql_query($query, $this->link);
if (mysql_num_rows($ret) == 0)
return false;
}
function last_id($query) {
return mysql_insert_id($query);
}
}
?>
Which I'm using for a lot of queries and it's working fine, although I'm trying to insert using this query:
global $db;
$query = $db->insert("INSERT INTO `submissions` (
`id` ,
`quote` ,
`filename` ,
`date_added` ,
`uploaded_ip`
)
VALUES (
NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}')
");
And it's giving me this error:
Not Found
The requested URL /horh_new/id/<br /><b>Fatal error</b>: Call to a member function insert() on a non-object in <b>/Applications/MAMP/htdocs/horh_new/addit.php</b> on line <b>52</b><br /> was not found on this server.
Although when I don't use that class above, and use this instead:
$con = mysql_connect("localhost","root","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO `submissions` (
`id` ,
`quote` ,
`filename` ,
`date_added` ,
`uploaded_ip`
)
VALUES (
NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}')
");
echo mysql_insert_id();
mysql_close($con);
It works fine. Can anyone tell me what is wrong with my class? I'd really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该这样做
或者更好的使用方法是
添加 DbConnector 类的静态函数,它将在整个请求中仅创建单个实例
并将此类用作
You should do like
Or the better way to use this is
add a static function of class DbConnector, which will create only single instance throughout a request
And use this class as
您需要在使用之前实例化该对象,
现在您可以执行插入操作,
尽管我建议首先添加一个 __construct 方法来连接到数据库
you need to instantiate the object before using it
now you can do an insert
although i suggest adding a __construct method to connect to the database first
您确定 $db 包含您的类的实例吗?也许只需在调用 $db->insert() 之前执行 $db 的 var_dump() 并查看它是否已设置。
Are you sure $db contains an instance of your class? Maybe just do a var_dump() of $db just before your call to $db->insert() and see if it's set.