Web Hosting Talk







View Full Version : [php/mysql] Help/Tips?


JustinSmall
08-27-2008, 02:05 PM
Alright, well I got this code off another thread... it's to produce a while statement with a class :) (I heavily modified it to my needs / expectations!)

class.php
<?
class MySQLFunctions {
public $Info;
protected $table;
protected $statement;

public function __construct() {
$this->Info = array();
}

public function FetchArray($table, $statement) {
$sql="SELECT * FROM $table $statement";
$result = mysql_query($sql)
or die("A MySQL error has occurred.<br />Your Query: " . $sql . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
while($row = mysql_fetch_array($result)){
$this->Info[] = $row;
}
}
}
?>

Now to call it up..

<?
require('class.php');

$pm = new MySQLFunctions();
$pm->FetchArray('mytable','WHERE userid = 1 ORDER BY id ASC');
foreach ($pm->Info as $row)
{
print_r($row['message']);
echo '<br /><br />';
}
?>

Works fantastically!, I simply love it!

But my question is, using the same class, (and mysql_fetch_row) how can I grab information that way... I tried everything and wasn't successful.

+ another question, I know since this is a foreach :p it won't clash with nething. But if I did a multiple mysql_fetch_row throughout the whole page (2 diff tables), and they both had $row['id']; would I get an error?

Thanks in advance.