...

View Full Version : db class



johnrau
06-01-2004, 12:36 AM
heres a class i wrote -- when i try $db->query(*) it returns the following:


Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/trulyta/public_html/new/globals.inc.php on line 19


the code:


class db {
function open() {
$this->host = "localhost";
$this->name = "*";
$this->user = "*";
$this->pass = "*";
$this->resource = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->name, $this->resource);
register_shutdown_function($this->close);
}
function query($query) {
// this is line 19, where it says the error is:
$result = mysql_query($query, $this->resource);
return $result;
}
function close() {
mysql_close($this->resource);
}
}

$db = new db;

$db->open;
$db->query("SELECT * FROM tt_drinks WHERE id = 1032");
$db->close;


any ideas would be greatly appreciated!!! thanks!

fimi
06-01-2004, 02:07 AM
what you're doing is asking to execute the mysql query which you gave:
query(*);


-------------------
i use this:
its much easier:
all i do is:
//to run a query:
$newqry = new query("Select * from bla bla bla....");

to get the data from the database
while($row = $newqry->putin_array()){
echo $row['filed1'];
}

//to check how many rows affected:
$newqry->affectedrows();

//close the db
$newqry->close();

<?

class query {
var $server;
var $db;
var $user;
var $password ;
var $error;
var $result;
var $conID;
function query( $sql )
{
$this->server = "localhost";
$this->db = "";
$this->user = "";
$this->password = "";
// connect to server and select database
$db_con = @mysql_connect( $this->server, $this->user, $this->password ) or die( $this->error = "Database connection failure!" );
$db_sel = @mysql_select_db( $this->db ) or die ( $this->error = mysql_error() . " Could not connect to specified database, db may not exist" );

$this->conID = $db_con;
// run query
$this->result = @mysql_query( $sql ) or die( $this->error = "Error: " . mysql_errno() . "&nbsp;<b>" . mysql_error() . "</b>query:$sql" );
// if errors then prints the error msg.
if ( !$db_con ) {
echo $this->error;
exit;
} elseif ( !$db_sel ) {
echo $this->error ;
exit;
}
}
function putin_array()
{
$sql_arrays = mysql_fetch_array( $this->result );
return ( $sql_arrays );
}
function numrows()
{
$sql_numrows = mysql_num_rows( $this->result );
return $sql_numrows;
}
function affectedrows()
{
$sql_affrows = mysql_affected_rows();
return $sql_affrows;
}
function close()
{
@mysql_free_result( $this->conID );
@mysql_close( $this->conID );
}
}

?>

i made this myself,

bye.

firepages
06-01-2004, 04:35 AM
$db->open;

should be

$db->open();

there is no point closing the mysql connection unless you are using a persistant connection in the first place (which you probably don't need/want to do) , but even if you did ,
register_shutdown_function($this->close);

won't work (it actually closes the connection within the constructor not on shutdown) , not cos of the missing `()` , you need to call as so ..



<?
/*no brackets intentional this time*/
register_shutdown_function(array(&$this, 'close'));
?>

johnrau
06-01-2004, 06:22 PM
Thanks for your help! Saved me hours of time!

-J



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum