11-14-2007, 05:17 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 44
Thanks: 0
|
connect database using oop
my head.php
PHP Code:
<?php
class mysql {
function Connect($host, $name, $pass, $db){
$connection = mysql_connect("$host",
"$name",
"$pass");
mysql_select_db("$db", $connection);
}//ends the connection function
function Close(){
mysql_close($this->connection);
}//ends the close function
function FetchArray($query){
$rows = mysql_fetch_array($query);
return $rows;
}
function FetchNum($query){
$num = mysql_num_rows($query);
return $num;
}
function Query($sql){
$query = mysql_query($sql) or die(mysql_error());
return $query;
}//ends the query function
}//ends the class
?>
my test.php
PHP Code:
<?php
include("head.php");
$DB = new mysql();
$host = "localhost";
$name = "username";
$pass = "password";
$db = "database";
$connection = $DB->Connect($host, $name, $pass, $db);
//define an SQL statement and execute it
$sql = "SELECT username FROM users";
$query = $DB->Query($sql);
//output all rows from the statement
while($row = $DB->FetchArray($query)){
echo "<b>Username: $row[username]<b><br \>";
}
//find the number of rows
$num = $DB->FetchNum($query);
echo "Number of rows: $num";
//close the connection
$DB->Close();
?>
it displays correct but there is a warning below..its stated :
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /web/head.php on line 15
why is it myql_close() seem not functioning.im a newbie of OOP..and im tried to connect to database using OOP.just practicing
|
|
|
|