Page 1 of 1

Problem connectin MySQL

Posted: Sat Aug 25, 2007 1:11 am
by manteca
I got this class

Code: Select all

<?php 
include("../Connections/config.inc.php");
include("../Connections/funciones.inc.php");

class doctor {

function get_name($id){
	$db =  conecta_db();
	$consc = consulta_db("SELECT * FROM doctores WHERE id='$id'");
	$consc = mysql_fetch_assoc($consc);
	$this->id = $consc['id'];
	$this->nombre1 = htmlentities($consc['nombre1']);
	$this->nombre2 = htmlentities($consc['nombre2']);
	$this->apellido1 = htmlentities($consc['apellido1']);
	$this->apellido2 = htmlentities($consc['apellido2']);
	$this->nombre_com =$this->nombre1 = htmlentities($consc['nombre1']);
	$this->nombre_com.=" ".$this->nombre2 = htmlentities($consc['nombre2']);
	$this->nombre_com.=" ".$this->apellido1 = htmlentities($consc['apellido1']);
	$this->nombre_com.=" ".$this->apellido2 = htmlentities($consc['apellido2']); 
	mysql_close($db);
	}// termino de la funcion
	
	function get_esp($id){
		$db =  conecta_db();
		$conn = consulta_db("SELECT d.id_esp, e.nombre FROM doc_esp d, especialidad e WHERE d.id_doctor='$id' AND d.id_esp=e.id ORDER BY d.id_esp;");
		$x=0;
		while ($row = mysql_fetch_assoc($conn)){
			$this->esp_id[$x] = htmlentities($row['id_esp']);
			$this->esp[$x] = htmlentities($row['nombre']);
			$x++;	
			}
		mysql_close($db);
	}//termino de funcion
	
	function get_subesp($id,$idesp){
		$db = conecta_db();
		$conn = consulta_db ("SELECT d.id_subesp, e.nombre FROM doc_esp d, subespecialidad e WHERE d.id_doctor='$id' AND d.id_subesp=e.id AND d.id_esp='$idesp' ORDER BY d.id_esp");
		$x=0;
		while ($row = mysql_fetch_assoc($conn)){
			$this->subesp[$x] = htmlentities($row['nombre']);
			$x++;
			}
		mysql_close($db);	
	}//termino de funcion
	
	/// de un registro
	
	function get_oneesp($id){
		$db =  conecta_db();
		$conn = consulta_db("SELECT d.id_esp, e.nombre FROM doc_esp d, especialidad e WHERE d.id='$id' AND d.id_esp=e.id");
		$x=0;
		while ($row = mysql_fetch_assoc($conn)){
			$this->esp_id[$x] = htmlentities($row['id_esp']);
			$this->esp[$x] = htmlentities($row['nombre']);
			$x++;	
			}
		mysql_close($db);
	}
	
	function get_oneSubesp($id){
		$db =  conecta_db();
		$conn = consulta_db ("SELECT d.id_subesp, e.nombre FROM doc_esp d, subespecialidad e WHERE d.id='$id' AND d.id_subesp=e.id");
		$row = mysql_fetch_assoc($conn);
		$this->id_subesp = htmlentities($row['id_subesp']);
		$this->subesp = htmlentities($row['nombre']);
		mysql_close($db);
	}//termino de funcion
//termino de clase doctor
}
This page calls the class

Code: Select all

<?php 
include("test_class.php");
$debug = "true";
$db = conecta_db();
$conn = consulta_db("SELECT * FROM doc_esp ORDER BY id ASC");
?>
<table border="1" cellpadding="1" cellspacing="0">
  <tr>
    <th>N°</th>
    <?php if ($debug == "true"){?>
    <th>ID</th>
    <th>ID_esp</th>
    <th>ID_subesp</th>
    <th>ID_doctor</th>
    <?php } ?>
    <th>Doctor</th>
    <th>Especialidad</th>
    <th>Subespecialidad</th>
  </tr>
  <?php 
  $count = 0;
  while ($row = mysql_fetch_assoc($conn)){ 
  $count++;
  $id = $row['id_doctor'];
  $doctor = new doctor;
  $doctor->get_name($id);
  $id = $row['id'];
  $doctor->get_oneesp($id);
  $doctor->get_onesubesp($id);
  ?>
  <tr>
    <td><?php echo $count; ?></td>
    <?php if ($debug == "true"){?>
    <td><?php echo  $row['id'];?></td>
    <td><?php echo  $row['id_esp'];?></td>
    <td><?php echo  $row['id_subesp'];?></td>
    <td><?php echo  $row['id_doctor'];?></td>
    <td><?php echo  $doctor->id."/".$doctor->nombre_com;?></td>
    <td><?php echo  $doctor->esp_id[0]."/".$doctor->esp[0];?></td>
    <td><?php echo  $doctor->id_subesp."/".$doctor->subesp;?></td>
    <?php //mysql_close($db);?>
    <?php }else{?>
    <td><?php echo  $row['id'];?></td>
    <td><?php echo  $doctor->nombre_com;?></td>
    <td><?php echo  $doctor->esp[0];?></td>
    <td><?php echo  $doctor->subesp;?></td>
    <?php //mysql_close($db);?>
    <?php }?>
  </tr>
<?php }?>
</table>
The data shows perfect, but when i hit refresh (F5) i get this error:Warning: mysql_connect(): Can't connect to MySQL server on 'localhost' (10048) in d:\archivos de programa\easyphp1-8\www\pendientes\indisa\nuevo\Connections\funciones.inc.php on line 113
2.Error en la Base de Datos, contacte al Admin.

The error get's from these function, that is in anothere class.

Code: Select all

function conecta_db() {
        $conn = mysql_connect(DBSERVER, DBUSER, DBPASSWD);

        if (!$conn) {
                        echo "2.Error en la Base de Datos, contacte al Admin.";
                        exit;
                }
        $db = mysql_select_db(DBASE, $conn);
                if (!$db) {
                        echo "3.Error en la Base de Datos, contacte al Admin.";
                        exit;
                }

        return $conn;
}
sorry for my english and that the code is in spanish, if anyone doesn't understand it, I could trasnlated it.

Thanks.

Posted: Sat Aug 25, 2007 1:19 am
by Kieran Huggins
try this instead:

Code: Select all

echo "2.Error en la Base de Datos, contacte al Admin. <br/>MySQL dice:".MySQL_error();
It should tell you why it won't connect.

Posted: Sat Aug 25, 2007 3:59 pm
by manteca
It shows
"MySQL dice:Can't connect to MySQL server on 'localhost' (10048)"

Same thing, I know these issue may happend whene you don´t close teh connection, but after eache mysql_connetcion i close it with

Code: Select all

mysql_close($db);
So how can i avoid these error?