Like NogDog
I think it is very good to have some error trapping.
Preferably with an echo of the SQL Query string!
If you like to experiment with your own mysql Class
then I really recommend the
PHP MySQLi Class http://www.php.net/manual/en/book.mysqli.php
If you do like me in the mysqli_simple Class I post here
you will have also ALL the parent MySQLi functions in YOUR class.
If you want to use them: $this->mysqli-function-name()
Another advantage using EXTENDS is that you need no $conn object.
As your class is MySQLi and you use $this object
Read more how to EXTENDS parent class into your child classes:
http://php.net/manual/en/keyword.extends.php
mysqli.simple.class.php:
<?php // by halojoy February 2010
if(!class_exists('MySQLi')) exit('MySQLi Class not supported');
class mysqli_simple extends MySQLi{
private $host = 'localhost';
private $user = 'xxxxxxxx';
private $pass = 'xxxxxxxx';
private $mydb = 'my_data';
function __construct(){
$this->init();
@$this->real_connect($this->host,$this->user,$this->pass,$this->mydb);
if($this->connect_error) exit('DB ConnectError: '.$this->connect_error);
}
public function sqlfetch($sql){
$success = $this->real_query($sql);
if(!$success){
echo('<br />SQLerror: '.$sql.'<br />'.$this->error.'<br />');
return;
}
$result = $this->store_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);
$result->free();
return $rows;
}
}// END CLASS
<?php
include 'mysqli.simple.class.php';
$db = new mysqli_simple;
$sql = "SELECT * FROM users WHERE name='Admin'";
$rows = $db->sqlfetch($sql);
echo '<br />'.$sql.'<br />'
.count($rows).' rows in result array<br />';
/* OUTPUT:
SELECT * FROM users WHERE name='Admin'
1 rows in result array
*/
?>
<?php
include 'mysqli.simple.class.php';
$db = new mysqli_simple;
$sql = "SELT * FROM users";
$rows = $db->sqlfetch($sql);
echo '<br />'.$sql.'<br />'
.count($rows).' rows in result array<br />';
/* OUTPUT:
SQLerror: SELT * FROM users
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELT * FROM users' at line 1
SELT * FROM users
0 rows in result array
*/
?>
<?php
include 'mysqli.simple.class.php';
$db = new mysqli_simple;
$sql = "SELECT * FROM bands";
$rows = $db->sqlfetch($sql);
echo '<br />'.$sql.'<br />'
.count($rows).' rows in result array<br />';
/* OUTPUT:
SELECT * FROM bands
9 rows in result array
*/
?>
In this last case $rows is the following array
with 9 records returned:
Array
(
[0] => Array
(
[id] => 1
[name] => John Lennon
[born] => 1940 Liverpool
[died] => 1980 New York
[band] => The Beatles
[lineup] => vocals, guitar, harmonica, piano, keyboards
)
[1] => Array
(
[id] => 2
[name] => Paul McCartney
[born] => 1942 Liverpool
[died] =>
[band] => The Beatles
[lineup] => vocals, bass, lead guitar, piano, keyboards
)
[2] => Array
(
[id] => 3
[name] => George Harrison
[born] => 1943 Liverpool
[died] => 2001 Los Angeles
[band] => The Beatles
[lineup] => vocals, lead guitar, sitar, keyboards
)
[3] => Array
(
[id] => 4
[name] => Ringo Starr
[born] => 1940 Liverpool
[died] =>
[band] => The Beatles
[lineup] => vocals, drums, percussion
)
[4] => Array
(
[id] => 5
[name] => Mick Jagger
[born] => 1943 Dartford
[died] =>
[band] => The Rolling Stones
[lineup] => lead vocals, harmonica, percussion
)
[5] => Array
(
[id] => 6
[name] => Brian Jones
[born] => 1942 Cheltenham
[died] => 1969 Hartfield
[band] => The Rolling Stones
[lineup] => guitars, backing vocals, harmonica, percussion, sitar
)
[6] => Array
(
[id] => 7
[name] => Keith Richards
[born] => 1943 Dartford
[died] =>
[band] => The Rolling Stones
[lineup] => guitars, vocals, bass, keyboards, percussion
)
[7] => Array
(
[id] => 8
[name] => Charlie Watts
[born] => 1941 London
[died] =>
[band] => The Rolling Stones
[lineup] => drums, percussion
)
[8] => Array
(
[id] => 9
[name] => Bill Wyman
[born] => 1936 London
[died] =>
[band] => The Rolling Stones
[lineup] => bass, vocals, percussion, keyboards
)
)