You are here: MySQL Problem « All PHP « IT news, forums, messages
MySQL Problem

Posted by Zsolt Munoz on 10/30/05 04:11

I am pretty new to OOP w/PHP but I feel that it may be in my best
interest to learn it. I have created a class to connect to a MySQL DB
but when I go to run a query I only get the 1st row from table and
cannot get anything else. Below is the class I created and how I am
executing the program. Maybe someone could give me a hand with this.

Thanks

|----Class.php----|

<?
class MyGo {
var $myserver;
var $mydb;
var $myuser;
var $mypass;
var $run;
var $standby = false;

function MyGo() {
$this->myserver = "localhost";
$this->myuser = "user";
$this->mypass = "pass";
$this->mydb = "db";
}

function connect() {
$connect =
@mysql_connect($this->myserver,$this->myuser,$this->mypass) or
die("Failed to connect to MySQL host.<br>". mysql_error()
.."<br><br><strong>Line:</strong> ". __LINE__
.."<br><strong>File:</strong> ". __FILE__);
$this->select();
return($connect);
}

function select() {
@mysql_select_db($this->mydb) or die("Failed to select mysql DB
{$this->mydb}.<br>". mysql_error() ."<br><br><strong>Line:</strong> ".
__LINE__ ."<br><strong>File:</strong> ". __FILE__);
}

function close() {
@mysql_close($this->connect());
}

function query() {
$this->connect();
$query = @mysql_query($this->run) or die("Failed to query the
database.<br>". mysql_error() ."<br><br><strong>Line:</strong> ".
__LINE__ ."<br><strong>File:</strong> ". __FILE__);
if ($this->standby == false) {
$this->close();
}
return($query);
}

function num_results() {
$this->connect();
$total = @mysql_num_rows($this->query());
return($total);
}

function num_fields() {
$this->connect();
$field = @mysql_num_fields($this->query());
return($field);
}

function name_fields($cnt) {
$this->connect();
$field_name = @mysql_field_name($this->query(), $cnt);
return($field_name);
}

function result() {
$this->connect();
$result = @mysql_fetch_array($this->query());
return($result);
}
}
?>


|----test.php----|
<?
include ("./classes/class.php");
$db = new MyGo;
$db->run = "SELECT * FROM table";
while($row = $db->result()) {
for($cnt = 0; $cnt < $db->num_fields(); $cnt++) {
echo "<strong>".$db->name_fields($cnt).":</strong> ";
echo $row[$cnt]."<br>\n";
}
echo "------------------------------------------------------<br>";
}

$db->close();
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация