need help in php

January 30, 2011 at 02:12:49
Specs: Windows XP
Hello guys,please i need help ,i always get this error while trying to compile it,
Fatal error: Cannot redeclare Mysqldb::getRows() in mysqldb.class.php on line 195
The code is

<?php
/**
* Database management / access class: basic abstraction
*
* @author Michael Peacock
* @version 1.0
*/
class Mysqldb {
/**
* Allows multiple database connections
* each connection is stored as an element in the array, and the
active connection is maintained in a variable (see below)
*/
private $connections = array();
/**
* Tells the DB object which connection to use
* setActiveConnection($id) allows us to change this
*/
private $activeConnection = 0;
/**
* Queries which have been executed and the results cached for
later, primarily for use within the template engine
*/
private $queryCache = array();
/**
* Data which has been prepared and then cached for later usage,
primarily within the template engine
*/
private $dataCache = array();
/**
* Number of queries made during execution process
*/
private $queryCounter = 0;
/**
* Record of the last query
*/
private $last;
/**
* Reference to the registry object
*/
private $registry;
/**
* Construct our database object
*/
public function __construct( Registry $registry )
{
$this->registry = $registry;
}
/**
* Create a new database connection
* @param String database hostname
* @param String database username
* @param String database password
* @param String database we are using
* @return int the id of the new connection
*/
public function newConnection( $host, $user, $password, $database )
{
$this->connections[] = new mysqli( $host, $user, $password,
$database );
$connection_id = count( $this->connections )-1;
if( mysqli_connect_errno() )
{
trigger_error('Error connecting to host.'.$this->connections[$connection_id]->error,E_USER_ERROR);
}
return $connection_id;
}
/**
* Change which database connection is actively used for the next
operation
* @param int the new connection id
* @return void
*/
public function setActiveConnection( int $new )
{
$this->activeConnection = $new;
}
/**
* Execute a query string
* @param String the query
* @return void
*/
public function executeQuery( $queryStr )
{
if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) )
{
trigger_error('Error executing query: ' . $queryStr .' -
'.$this->connections[$this->activeConnection]->error,
E_USER_ERROR);
}
else
{
$this->last = $result;
}
}
/**
* Get the rows from the most recently executed query, excluding
cached queries
* @return array
*/
public function getRows()
{
return $this->last->fetch_array(MYSQLI_ASSOC);
}
/**
* Delete records from the database
* @param String the table to remove rows from
* @param String the condition for which rows are to be removed
* @param int the number of rows to be removed
* @return void
*/
public function deleteRecords( $table, $condition, $limit )
{
$limit = ( $limit == '' ) ? '' : ' LIMIT ' . $limit;
$delete = "DELETE FROM {$table} WHERE {$condition} {$limit}";
$this->executeQuery( $delete );
}
/**
* Update records in the database
* @param String the table
* @param array of changes field => value
* @param String the condition
* @return bool
*/
public function updateRecords( $table, $changes, $condition )
{
$update = "UPDATE " . $table . " SET ";
foreach( $changes as $field => $value )
{
$update .= "`" . $field . "`='{$value}',";
}
// remove our trailing ,
$update = substr($update, 0, -1);
if( $condition != '' )
{
$update .= "WHERE " . $condition;
}
$this->executeQuery( $update );
return true;
}
/**
* Insert records into the database
* @param String the database table
* @param array data to insert field => value
* @return bool
*/
public function insertRecords( $table, $data )
{
// setup some variables for fields and values
$fields = "";
$values = "";
// populate them
foreach ($data as $f => $v)
{
$fields .= "`$f`,";
$values .= ( is_numeric( $v ) && ( intval( $v ) == $v ) ) ?
$v."," : "'$v',";
}
// remove our trailing ,
$fields = substr($fields, 0, -1);
// remove our trailing ,
$values = substr($values, 0, -1);
$insert = "INSERT INTO $table ({$fields}) VALUES({$values})";
//echo $insert;
$this->executeQuery( $insert );
return true;
}
/**
* Sanitize data
* @param String the data to be sanitized
* @return String the sanitized data
*/
public function sanitizeData( $value )
{
// Stripslashes
if ( get_magic_quotes_gpc() )
{
$value = stripslashes ( $value );
}
// Quote value
if ( version_compare( phpversion(), "4.3.0" ) == "-1" )
{
$value = $this->connections[$this->activeConnection]->escape_string( $value );
}
else
{
$value = $this->connections[$this->activeConnection]->real_escape_string( $value );
}
return $value;
}
/*** Get the rows from the most recently executed query, excluding
cached queries
* @return array
*/
public function getRows()
{
return $this->last->fetch_array(MYSQLI_ASSOC);
}
public function numRows()
{
return $this->last->num_rows;
}
/**
* Gets the number of affected rows from the previous query
* @return int the number of affected rows
*/
public function affectedRows()
{
return $this->last->affected_rows;
}
/**
* Deconstruct the object
* close all of the database connections
*/
public function __deconstruct()
{
foreach( $this->connections as $connection )
{
$connection->close();
}
}
}
?>


See More: need help in php

Report •

#1
January 30, 2011 at 02:24:07
The line 195 is
public function affectedRows()

Report •

#2
January 30, 2011 at 02:35:49
If you look at your code you'll find that you declare getRows() twice - once near the middle of the code and once near the end.

Report •

#3
January 31, 2011 at 00:16:17
thanks for ur answer,so what is ur suggestion?

Report •

Related Solutions

#4
January 31, 2011 at 01:33:24
Delete the second declaration of the function! The two are identical.

Report •

#5
January 31, 2011 at 01:45:19
Thanks for ur fast reply..actually after i delet it ,i got more than 5 error message,and just found out the same error has a call stack from the index

Fatal error: Cannot redeclare Mysqldb::getRows() in C:\wamp\www\frof\registry\mysqldb.class.php on line 192
Call Stack
# Time Memory Function Location
1 0.0005 398232 {main}( ) ..\index.php:0
2 0.0031 527872 Registry->createAndStoreObject( ) ..\index.php:8

The index code are:
<?php
/**
* Database management / access class: basic abstraction
*/
class Mysqldb {
/**
* Allows multiple database connections
* each connection is stored as an element in the array, and the
active connection is maintained in a variable (see below)
*/
private $connections = array();
/**
* Tells the DB object which connection to use
* setActiveConnection($id) allows us to change this
*/
private $activeConnection = 0;
/**
* Queries which have been executed and the results cached for
later, primarily for use within the template engine
*/
private $queryCache = array();
/**
* Data which has been prepared and then cached for later usage,
primarily within the template engine
*/
private $dataCache = array();
/**
* Number of queries made during execution process
*/
private $queryCounter = 0;
/**
* Record of the last query
*/
private $last;
/**
* Reference to the registry object
*/
private $registry;
/**
* Construct our database object
*/
public function __construct( Registry $registry )
{
$this->registry = $registry;
}
/**
* Create a new database connection
* @param String database hostname
* @param String database username
* @param String database password
* @param String database we are using
* @return int the id of the new connection
*/
public function newConnection( $host, $user, $password, $database )
{
$this->connections[] = new mysqli( $host, $user, $password,
$database );
$connection_id = count( $this->connections )-1;
if( mysqli_connect_errno() )
{
trigger_error('Error connecting to host.'.$this->connections[$connection_id]->error,E_USER_ERROR);
}
return $connection_id;
}
/**
* Change which database connection is actively used for the next
operation
* @param int the new connection id
* @return void
*/
public function setActiveConnection( int $new )
{
$this->activeConnection = $new;
}
/**
* Execute a query string
* @param String the query
* @return void
*/
public function executeQuery( $queryStr )
{
if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) )
{
trigger_error('Error executing query: ' . $queryStr .' -
'.$this->connections[$this->activeConnection]->error,
E_USER_ERROR);
}
else
{
$this->last = $result;
}
}
/**
* Get the rows from the most recently executed query, excluding
cached queries
* @return array
*/
public function getRows()
{
return $this->last->fetch_array(MYSQLI_ASSOC);
}
/**
* Delete records from the database
* @param String the table to remove rows from
* @param String the condition for which rows are to be removed
* @param int the number of rows to be removed
* @return void
*/
public function deleteRecords( $table, $condition, $limit )
{
$limit = ( $limit == '' ) ? '' : ' LIMIT ' . $limit;
$delete = "DELETE FROM {$table} WHERE {$condition} {$limit}";
$this->executeQuery( $delete );
}
/**
* Update records in the database
* @param String the table
* @param array of changes field => value
* @param String the condition
* @return bool
*/
public function updateRecords( $table, $changes, $condition )
{
$update = "UPDATE " . $table . " SET ";
foreach( $changes as $field => $value )
{
$update .= "`" . $field . "`='{$value}',";
}
// remove our trailing ,
$update = substr($update, 0, -1);
if( $condition != '' )
{
$update .= "WHERE " . $condition;
}
$this->executeQuery( $update );
return true;
}
/**
* Insert records into the database
* @param String the database table
* @param array data to insert field => value
* @return bool
*/
public function insertRecords( $table, $data )
{
// setup some variables for fields and values
$fields = "";
$values = "";
// populate them
foreach ($data as $f => $v)
{
$fields .= "`$f`,";
$values .= ( is_numeric( $v ) && ( intval( $v ) == $v ) ) ?
$v."," : "'$v',";
}
// remove our trailing ,
$fields = substr($fields, 0, -1);
// remove our trailing ,
$values = substr($values, 0, -1);
$insert = "INSERT INTO $table ({$fields}) VALUES({$values})";
//echo $insert;
$this->executeQuery( $insert );
return true;
}
/**
* Sanitize data
* @param String the data to be sanitized
* @return String the sanitized data
*/
public function sanitizeData( $value )
{
// Stripslashes
if ( get_magic_quotes_gpc() )
{
$value = stripslashes ( $value );
}
// Quote value
if ( version_compare( phpversion(), "4.3.0" ) == "-1" )
{
$value = $this->connections[$this->activeConnection]->escape_string( $value );
}
else
{
$value = $this->connections[$this->activeConnection]->real_escape_string( $value );
}
return $value;
}
/*** Get the rows from the most recently executed query, excluding
cached queries
* @return array
*/
public function getRows()
{
return $this->last->fetch_array(MYSQLI_ASSOC);
}
public function numRows()
{
return $this->last->num_rows;
}
/**
* Gets the number of affected rows from the previous query
* @return int the number of affected rows
*/
public function affectedRows()
{
return $this->last->affected_rows;
}
/**
* Deconstruct the object
* close all of the database connections
*/
public function __deconstruct()
{
foreach( $this->connections as $connection )
{
$connection->close();
}
}
}
?>


Report •

#6
January 31, 2011 at 03:20:05
The code that you list in your last post still has the duplicate declaration of getRows().

Report •

#7
January 31, 2011 at 05:00:01
yes,because after i delete it,i got more than 6 errors

Report •

#8
January 31, 2011 at 05:34:09
It might be more useful to show the errors that result after you have removed the duplicate code. It's never going to compile as long as that error persists. Clearing one error often throws up other ones that weren't obvious before.

Report •

#9
January 31, 2011 at 17:49:08
Thank you ijack,i really appreciate your help.
am rewriting the code again,please check the new unexpected error

Thank you


Report •

#10
January 31, 2011 at 18:23:35
Unknown database 'january' in C:\wamp\www\january\registry\mysqldb.class.php on line 59

Check your database name - it does not exist.


Report •

#11
January 31, 2011 at 18:36:56
guys i rewrite the database,all that error doesn't display any more,the new error that display is
Parse error: syntax error, unexpected '>' in C:\wamp\www\january\registry\mysqldb.class.php on line 70
Call Stack
# Time Memory Function Location
1 0.0005 398232 {main}( ) ..\index.php:0
2 0.0026 527872 Registry->createAndStoreObject( ) ..\index.php:8

The new database code is:
<?php
/**
* Database management / access class: basic abstraction
*
*
*
*/
class Mysqldb {

/**
* Allows multiple database connections
* each connection is stored as an element in the array, and the active connection is maintained in a variable (see below)
*/
private $connections = array();

/**
* Tells the DB object which connection to use
* setActiveConnection($id) allows us to change this
*/
private $activeConnection = 0;

/**
* Queries which have been executed and the results cached for later, primarily for use within the template engine
*/
private $queryCache = array();

/**
* Data which has been prepared and then cached for later usage, primarily within the template engine
*/
private $dataCache = array();

/**
* Number of queries made during execution process
*/
private $queryCounter = 0;

/**
* Record of the last query
*/
private $last;

/**
* Reference to the registry object
*/
private $registry;

/**
* Construct our database object
*/
public function __construct( Registry $registry )
{
$this->registry = $registry;
}

/**
* Create a new database connection
* @param String database hostname
* @param String database username
* @param String database password
* @param String database we are using
* @return int the id of the new connection
*/
public function newConnection( $host, $user, $password, $database )
{
$this->connections[] = new mysqli( $host, $user, $password, $database );
$connection_id = count( $this->connections )-1;
if( mysqli_connect_errno() )
{
trigger_error('Error connecting to host. '.$this->connections[$connection_id]->error,E_USER_ERROR);
}
return $connection_id;
}
}

/**
* Close the active connection
* @return void
*/
public function closeConnection()
{
$this->connections[$this->activeConnection]->close();
}

/**
* Change which database connection is actively used for the next operation
* @param int the new connection id
* @return void
*/
public function setActiveConnection( int $new )
{
$this->activeConnection = $new;
}

/**
* Store a query in the query cache for processing later
* @param String the query string
* @return the pointed to the query in the cache
*/
public function cacheQuery( $queryStr )
{
if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) )
{
trigger_error('Error executing and caching query: '.$this->connections[$this->activeConnection]->error, E_USER_ERROR);
return -1;
}
else
{
$this->queryCache[] = $result;
return count($this->queryCache)-1;
}
}

/**
* Get the number of rows from the cache
* @param int the query cache pointer
* @return int the number of rows
*/
public function numRowsFromCache( $cache_id )
{
return $this->queryCache[$cache_id]->num_rows;
}

/**
* Get the rows from a cached query
* @param int the query cache pointer
* @return array the row
*/
public function resultsFromCache( $cache_id )
{
return $this->queryCache[$cache_id]->fetch_array(MYSQLI_ASSOC);
}

/**
* Store some data in a cache for later
* @param array the data
* @return int the pointed to the array in the data cache
*/
public function cacheData( $data )
{
$this->dataCache[] = $data;
return count( $this->dataCache )-1;
}

/**
* Get data from the data cache
* @param int data cache pointed
* @return array the data
*/
public function dataFromCache( $cache_id )
{
return $this->dataCache[$cache_id];
}

/**
* Delete records from the database
* @param String the table to remove rows from
* @param String the condition for which rows are to be removed
* @param int the number of rows to be removed
* @return void
*/
public function deleteRecords( $table, $condition, $limit )
{
$limit = ( $limit == '' ) ? '' : ' LIMIT ' . $limit;
$delete = "DELETE FROM {$table} WHERE {$condition} {$limit}";
$this->executeQuery( $delete );
}

/**
* Update records in the database
* @param String the table
* @param array of changes field => value
* @param String the condition
* @return bool
*/
public function updateRecords( $table, $changes, $condition )
{
$update = "UPDATE " . $table . " SET ";
foreach( $changes as $field => $value )
{
$update .= "`" . $field . "`='{$value}',";
}

// remove our trailing ,
$update = substr($update, 0, -1);
if( $condition != '' )
{
$update .= "WHERE " . $condition;
}
$this->executeQuery( $update );

return true;

}

/**
* Insert records into the database
* @param String the database table
* @param array data to insert field => value
* @return bool
*/
public function insertRecords( $table, $data )
{
// setup some variables for fields and values
$fields = "";
$values = "";

// populate them
foreach ($data as $f => $v)
{

$fields .= "`$f`,";
$values .= ( is_numeric( $v ) && ( intval( $v ) == $v ) ) ? $v."," : "'$v',";

}

// remove our trailing ,
$fields = substr($fields, 0, -1);
// remove our trailing ,
$values = substr($values, 0, -1);

$insert = "INSERT INTO $table ({$fields}) VALUES({$values})";
//echo $insert;
$this->executeQuery( $insert );
return true;
}

public function lastInsertID()
{
return $this->connections[ $this->activeConnection]->insert_id;
}

/**
* Execute a query string
* @param String the query
* @return void
*/
public function executeQuery( $queryStr )
{
if( !$result = $this->connections[$this->activeConnection]->query( $queryStr ) )
{
trigger_error('Error executing query: ' . $queryStr .' - '.$this->connections[$this->activeConnection]->error, E_USER_ERROR);
}
else
{
$this->last = $result;
}

}

/**
* Get the rows from the most recently executed query, excluding cached queries
* @return array
*/
public function getRows()
{
return $this->last->fetch_array(MYSQLI_ASSOC);
}

public function numRows()
{
return $this->last->num_rows;
}

/**
* Gets the number of affected rows from the previous query
* @return int the number of affected rows
*/
public function affectedRows()
{
return $this->last->affected_rows;
}

/**
* Sanitize data
* @param String the data to be sanitized
* @return String the sanitized data
*/
public function sanitizeData( $value )
{
// Stripslashes
if ( get_magic_quotes_gpc() )
{
$value = stripslashes ( $value );
}

// Quote value
if ( version_compare( phpversion(), "4.3.0" ) == "-1" )
{
$value = $this->connections[$this->activeConnection]->escape_string( $value );
}
else
{
$value = $this->connections[$this->activeConnection]->real_escape_string( $value );
}
return $value;
}

/**
* Deconstruct the object
* close all of the database connections
*/
public function __deconstruct()
{
foreach( $this->connections as $connection )
{
$connection->close();
}
}
}
?>


Report •

#12
January 31, 2011 at 19:01:05
Eh, so open it up, find Line 70 and look for the '>' in the wrong place!

I'm having a hard time believing you wrote any of that code, if you can't understand such a basic error message.


Why are you even messing with this? There are many other libraries that already do this...why re-create the wheel?


Report •

#13
February 1, 2011 at 00:20:34
Indeed. Line 69 has been split over two lines. Presumably it was that way in the source that it was copied from. Just Join 69 with the following line.

I have to agree with fmwap - the compiler is telling you exactly where and what the error is (like it did with the previous error); just look where it is telling you to.

If you want to learn php programming you might find it better to start off with a few simple programs; you might then be able to understand the longer ones that you copy and try to run. I haven't really got the time to talk you through php programming from scratch, so I'm going to sign off at this point. Read one of the excellent books on the subject.


Report •

Ask Question