我的代码发生了错误。我不知道为什么会发生,我无法获得受影响的行数。
我的tbl_item_events中有200条记录。为避免冗余记录,我实现了一个简单的代码来避免这种情况。但它不起作用。始终$count=0
为$count
为零。
<?php
//class Database
class Database
{
private $username;
private $pass;
private $server;
private $database;
public function __construct(){
$this->username='root';
$this->pass='';
$this->server='localhost';
$this->database='my_db';
$this->initial_db();
}
public function initial_db(){
$con = mysql_connect($this->server, $this->username, $this->pass) or die( 'unable to connect to database');
$db=mysql_select_db($this->database, $con);
if(!$db)
die('unable to select db');
}
public function insert($row){
for($i=0;$i<count($row);$i++){
$status = 2;
$remoteAddr = 0;
$query = "INSERT INTO tbl_item_events SET publish_date = '".date("Y-m-d H:i:s")."', approved_date = '".date("Y-m-d H:i:s")."', user_id = '300'";
$query.= ", creation_date='".date("Y-m-d H:i:s")."', status= '".$status."', ip_address ='".$remoteAddr."'";
foreach($row[$i] as $key => $value)
{
$query.=", ".$key."='".$value."' ";
}
if($this->avoid_redundace($row[$i]['title']))
$result= mysql_query($query);
}
}
public function avoid_redundace($title){
$query='SELECT title FROM tbl_item_events WHERE title="'.$title.'"';
$result= mysql_query($query);
$count=mysql_num_rows($result);
if($count>0)
return FALSE;
return TRUE;
}
}
?>