News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

HELP ME: $DB Class

Started by BahadirK, July 12, 2005, 10:50:12 PM

Previous topic - Next topic

BahadirK

Hey guys,

I've write this code:

<?php
class DB {
function 
DB() {
  $this->link mysql_connect($dbhost$dbuser$dbpass);
  mysql_select_db($dbname);
  register_shutdown_function($this->close);
}
function 
query($query) {
  $result mysql_query($query$this->link);
  return $result;
}
function 
close() {
  mysql_close($this->link);
}
}
?>


And i'm using this code:

<?php
$DB 
= new DB;
if (
$DB) {
echo 
'Connection OKAY!';
} else {
echo 
'Connection FAILED!';
}
$forums $DB->query("SELECT cid, order, name FROM rvr_categories ORDER BY order DESC");
while (
$forum mysql_fetch_array($forums))
{
echo 
'Test';

?>


Connection success. But i can't get query results.

Please help me.

Thanks

BahadirK


[Unknown]

Find:

register_shutdown_function($this->close);

Replace:

register_shutdown_function(array($this, $this->close));

Find:

while ($forum = mysql_fetch_array($forums))

Replace:

var_dump($forums);
while ($forum = mysql_fetch_array($forums))

-[Unknown]

BahadirK

Unknown,

I haven't register_shutdown_function function.

Oh sorry, and thank you so much

You're the best still ...

BahadirK

Unknown,

index.php
<?php
/* Requirements */
// Configuration loading ... -Mid
require('config.inc.php');
// MySQL connection ... -Mid
require('db.php');

$DB = new DB;
if (
$DB) {
echo 'Connection OKAY!';
} else {
echo 'Connection FAILED!';
}

$forums $DB->query("SELECT cid, order, name FROM rvr_categories ORDER BY order DESC");

var_dump($forums);
while (
$forum mysql_fetch_array($forums))
{
echo 'Test';
}
?>


db.php
<?php
/* Requirements */
// Configuration loading ... -Mid
require('config.inc.php');


class 
DB {
function DB() {
$this->link mysql_connect($dbhost$dbuser$dbpass);
mysql_select_db($dbname);
register_shutdown_function(array($this->close));
}
function query($query) {
$result mysql_query($query$this->link);
return $result;
}
function close() {
mysql_close($this->link);
}
}
?>


config.inc.php
<?php
// MySQL host address
$dbhost "localhost";
// MySQL username
$dbuser "root";
// MySQL password
$dbpass "";
// MySQL database
$dbname "revery";
// MySQL persist connection
$dbpers "0";
?>


But, I get this:

QuoteConnection OKAY!bool(false)

Thanks

[Unknown]

That means the query failed.  See what mysql_error() gives you.

-[Unknown]

BahadirK


[Unknown]

echo mysql_error();

Instead of the var_dump() call.

-[Unknown]

BahadirK

#8
QuoteYou have an error in your SQL syntax near 'order, name FROM rvr_categories ORDER BY order DESC' at line 1

mmm... I guess mistake on 'order' column ... But i need it.

[Unknown]

You can use `order`.

-[Unknown]

BahadirK

yeah, i did

i've use:
$forums = $DB->query("SELECT 'cid', 'order', 'name' FROM rvr_categories ORDER BY 'order' DESC");

now, i get this error, but i coded it in 'DB class':
QuoteNo Database Selected

[Unknown]

This may be failing, then:

mysql_select_db($dbname);

I don't see where you global that or anything.

-[Unknown]

BahadirK

is in db.php as:

$dbname = "revery";

???

[Unknown]

You have to global it:

global $dbname;

Or pass it to the function.

-[Unknown]

BahadirK

I'd, but i've get same error:

No Database Selected

BahadirK


۞

Your  problem is that you're only half using Object Oriented concepts. Really it makes more sense to use all or nothing.

You should send the db info when you create an instance of the db class.

Like so:

$db = new DB($dbserver, $dbuser, $dbpass, $dbname);


Then db.php would look like this:


<?

class DB {

var $link;


function DB($db_server, $dbuser, $dbpass, $dbname) {
$this->link = mysql_connect($dbserver, $dbuser. $dbpass);
mysql_select_db($dbname);
}

function query($query) {
return mysql_query($query, $this->link);
}
}


If you're going to go for OO programming, do it properly :P

Don't include config files in the classes themselves, send the class the info when you initialize it.
TTTTTT  OOOOOO MMMMMM   BBBBBB


Advertisement: