Hi all,
I found these examples (by the way, I am WIDE OPEN to any BETTER examples) on making an OOP class for my database (and table select) connections.
But when I test it, I am getting an array for my table, but the title is the same for all rows??? And the loop for all rows only gives rows 2-5 (there are 5 total rows in this table).
Here is what I get...
Single Row
VideoID: 1
Title: Is America #One?
Output All rows
VideoID: 2 Title: Is America #One?
Output All rows
VideoID: 3 Title: Is America #One?
Output All rows
VideoID: 4 Title: Is America #One?
Output All rows
VideoID: 5 Title: Is America #One?
Number of rows: 5
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/inthecla/public_html/Don/mysql_class.php on line 16
And the code (from all 3 files) is...
(passwords.php)
$host = 'localhost';
$name = 'x';
$pass = 'x';
(mysql_class.php)
class mysql {
function Connect($host, $name, $pass, $db){
$connection = mysql_connect("$host", "$name", "$pass");
mysql_select_db("$db", $connection) OR DIE (mysql_error());
}//ends the connection function
function Close(){
mysql_close($this->connection);
}//ends the close function <-----------------------------This is Line 16 (the error from the output)
function FetchRow($query){
$rows = mysql_fetch_row($query);
return $rows;
}
function FetchArray($query){
$array = mysql_fetch_array($query);
return $array;
}
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
and the output...
(testconnect.php)
//select the table and db
$table = 'Videos';
$db = 'Main_table';
//include the needed name/password
include("includes/passwords.php");
include("mysql_class.php");
//make a new db connection
$DB = new mysql();
//connect
$connection = $DB->Connect($host, $name, $pass, $db);
//define an SQL statement and execute it
$sql = "SELECT * FROM " . $table;
$query = $DB->Query($sql);
//fetch a single row and output it
$newsrow = $DB->FetchRow($query);
$VideoID = $newsrow[0];
$Title = $newsrow[1];
echo "<b>Single Row</b><br /><br />VideoID: $VideoID<br>
Title: $Title<br><br>";
//output all rows from the statement
while($array = $DB->FetchArray($query)){
extract($array);
echo "<b>Output All rows</b><br /><br />VideoID: $VideoID<b>
Title: $Title<br />";
}
//find the number of rows
$num = $DB->FetchNum($query);
echo "Number of rows: $num";
//close the connection
$DB->Close($sql);
I am just looking for some direction and is this a good way to approach an OOP solution for connecting and selecting from my tables?
I found more complex example, but if course, to complex for me to comprehend (yet, I am working on understanding them eventually).
Thanks in advance,
Don