Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

php mysql insertion takes place twice

Codes here !

Moderators: macek, egami, gesf

php mysql insertion takes place twice

Postby artashkan » Wed Aug 01, 2012 6:30 am

I have the following class to write into mysql DB


<?php


require_once('./autoload.php');


class DBObject
{

private $DB_Name;
private $DB_Open;
private $DB_Conn;

public function __construct($database = DB_NAME, $hostname = DB_HOST, $hostport = DB_PORT, $username = DB_USER, $password = DB_PASS)
{
$this->DB_Name = $database;
$this->DB_Conn = mysql_connect($hostname . ":" . $hostport, $username, $password);
if (!$this->DB_Conn) { die('Critical Stop Error: Database Error<br />' . mysql_error()); }
@mysql_select_db($this->DB_Name, $this->DB_Conn);
}

private function __clone() {}

protected static function _getConnection() {
return $this->DB_Conn;
}

public function __destruct()
{
if(is_resource($this->DB_Conn)) {
@mysql_close($this->DB_Conn); // <-- commented out due to current shared-link close 'feature'. If left in, causes a warning that this is not a valid link resource. -->
}
}



public function Open($database = DB_NAME, $hostname = DB_HOST, $hostport = DB_PORT, $username = DB_USER, $password = DB_PASS)
{
if (! $this->DB_Open)
{
$this->DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
else
{
$this->DB_Open = null;
$this->DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
return $this->DB_Open;
}



public function qry($sql, $return_format = 0)
{
$this->Open();
$query = mysql_query($sql, $this->DB_Conn);

if(! is_resource($query)) {
$err = mysql_error();
throw new Exception($err);
die();
}

switch ($return_format)
{
case 1:
$query = mysql_fetch_row($query);
return $query;
break;
case 2:
$query = mysql_fetch_array($query);
return $query;
break;
case 3:
$query = mysql_fetch_assoc($query);
return $query;
break;
default:
return $query;
}
}



public function execNonQuery($sql)
{
$this->Open();
$query = mysql_query($sql);

if(! $query) {
$err = mysql_error();
throw new Exception($err);
die();
}
return $query;
}



public function select($tablename, $retValues , $conditions , $return_format = 0 )
{

$this->Open();
if (is_array($retValues))
$retFields = array_keys($retValues);
elseif ( strcasecmp($retValues , "all") == 0)
$retFields = array(" * ");

foreach($retFields as $field => $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val="'" . mysql_escape_string($val) . "'";
}
}

foreach($conditions as $field => $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val="'" . mysql_escape_string($val) . "'";
}
}

$arWhere = array_keys($conditions);

$sql = "select ";
$sql .= join(', ' , $retFields);
$sql .= " from $tablename ";
$sql .= ' WHERE ' . join(' AND ', $arWhere);

return $this->qry($sql , $return_format);

}


public function update($table, $arFieldValues, $arConditions , $return_format = 0 ) {

$this->Open();
// create a useful array for the SET clause
$arUpdates = array();
foreach($arFieldValues as $field => $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val="'" . mysql_escape_string($val) . "'";
}
$arUpdates[] = "$field = $val";
}

// create a useful array for the WHERE clause
$arWhere = array();
foreach($arConditions as $field => $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val = "'" . mysql_escape_string($val) . "'";
}
$arWhere[] = "$field = $val";
}

$sql = "UPDATE $table SET ";
$sql .= join(', ', $arUpdates);
$sql .= ' WHERE ' . join(' AND ', $arWhere);

return $this->execNonQuery($sql);
}



public function delete($table, $arConditions) {

$this->Open();
//create a useful array for generating the WHERE clause
$arWhere = array();
foreach($arConditions as $field => $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val = "'" . mysql_escape_string($val) . "'";
}

$arWhere[] = "$field = $val";
}

$sql = "DELETE FROM $table WHERE " . join(' AND ', $arWhere);

return $this->execNonQuery($sql);
}


public function insert($table, $arFieldValues) {
$this->Open();
$fields = array_keys($arFieldValues);
$values = array_values($arFieldValues);

// Create a useful array of values
// that will be imploded to be the
// VALUES clause of the insert statement.
// Run the pg_escape_string function on those
// values that are something other than numeric.
$escVals = array();
foreach($values as $val) {
if(! is_numeric($val)) {
//make sure the values are properly escaped
$val = "'" . mysql_escape_string($val) . "'";
}
$escVals[] = $val;
}

//generate the SQL statement
$sql = " INSERT INTO $table (";
$sql .= join(', ', $fields);
$sql .= ') VALUES(';
$sql .= join(', ', $escVals);
$sql .= ')';

return $this->execNonQuery($sql);
}

}



However whenever I use this class in the form such as below:


Class Some
{


public function saveToDB()
{


if ($this->ip)
{
$db = new DBObject;
if ($rows = $db->insert("user_visits" , array('ip' => $this->ip , 'http_user_agent'=> $this->http_user_agent ) ))
return true; // echo "Data Inserted into DB $rows";
}
return false;
}

}


then :

$some = new Some;
$some->saveToDB();


it saves twice into the DB with almost one second difference.

Any idea why?
artashkan
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Aug 17, 2011 6:28 am

Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.