Código PHP:
<?php
/*
* Created on 25/01/2010
* Euler Sánchez Gómez
*
*/
class ConexionMYSQL {
private $servidor;
private $user;
private $password;
private $db;
public function __construct ($servidor, $user, $password, $db) {
$this->servidor=$servidor;
$this->user=$user;
$this->password=$password;
$this->db=$db;
}
public function conectarMYSQL(){
if ($this->enlace=mysql_connect($this->servidor,$this->user,$this->password)){
if(mysql_select_db($this->db,$this->enlace)) {
echo "Conexión exitosa";
}
else{
echo "Error";
echo mysql_error();
}
}
else{
echo "Error";
echo mysql_error();
}
}
public function sqlQuery($query){
$query = addslashes($query);
$result = mysql_query($query,$this->enlace);
if($result)
{
return $result;
//$num = mysql_num_rows($result);
//return $num;
}
else
{
echo "Error";
}
}
public function getData($linkQuery){
$n=mysql_num_fields($linkQuery);
//echo $n;
$arrayNames=array();
for($i = 0; $i < $n; $i++)
{
$arrayNames[] = mysql_fetch_field($linkQuery)->name;
//echo $arrayNames[$i];
}
$par=array();
while ($row = mysql_fetch_array($linkQuery)){
foreach($arrayNames as $nombre){
$par[$nombre] = $row[$nombre];
}
$data[] = $par;
}
return $data;
}
public function CloseConexion() {
mysql_close($this->enlace);
}
}
?>
Código PHP:
<?php
include ("./class_MYSQL_conexion.php");
$a = new ConexionMYSQL ("localhost","root","my_password","my_database");
$a->conectarMYSQL();
$query="SELECT * from users";
$r=$a->sqlQuery($query);
$p=$a->getData($r);
print_r ($p);
?>