Ваша функция Fetch
извлекает только одну строку из базы данных, и вы не возвращаете результаты ...
Метод не самый лучший, но для достижения того, что вы пытаетесь сделать:
public function Fetch($set_table_name){
$query=mysql_query("SELECT * FROM ".$set_table_name);
$result = array();
while ($record = mysql_fetch_array($query)) {
$result[] = $record;
}
return $result;
}
Это сделает каждую строку частью $ result, но вам нужно будет получить к ней следующий доступ:
Вы бы вызвали функцию извлечения следующим образом:
$result = $connect->Fetch('posts');
echo $result[0]['columnName']; // for row 0;
Или в цикле:
for ($x = 0; $x < count($result); $x++) {
echo $result[$x][0] . "<BR>"; // outputs the first column from every row
}
Тем не менее, извлечение всего набора результатов в память не является хорошей идеей (некоторые скажут, что это очень плохая идея).
Редактировать: Похоже, что у вас есть другие проблемы с вашим классом ... Я должен бежать, но завтра проверю, и если другие не уладят вас, я расширю.
Edit2: Хорошо, собираемся сделать очередной урок в вашем классе и попытаться объяснить несколько вещей:
class MySQL {
//declaring the private variables that you will access through $this->variable;
private $host;
private $username;
private $password;
private $database;
private $conn; // Adding the connection, more on this later.
public function __Construct($set_host, $set_username, $set_password){
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
// Combining the connection & connection check using 'or'
// Notice that I removed the semi-colon after the mysql_connect function
$this->conn = mysql_connect($this->host, $this->username, $this->password)
or die("Couldn't connect");
}
public function Database($set_database)
{
$this->database=$set_database;
// Adding the connection to the function allows you to have multiple
// different connections at the same time. Without it, it would use
// the most recent connection.
mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
}
public function Fetch($table_name){
// Adding the connection to the function and return the result object instead
return mysql_query("SELECT * FROM ".$table_name, $this->conn);
}
}
$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$posts = $connect->Fetch('posts');
if ($posts && mysql_num_rows($posts) > 0) {
echo "Here is some post data:<BR>";
while ($record = mysql_fetch_array($posts)) {
echo $record[0]; // or a quoted string column name instead of numerical index.
}
} else {
echo "No posts!";
}
Надеюсь, это поможет ... Это должно как минимум начать. Если у вас есть дополнительные вопросы, задавайте их отдельно.