Foros del Web » Programando para Internet » PHP »

Error a la conexion de bd

Estas en el tema de Error a la conexion de bd en el foro de PHP en Foros del Web. Buenos dias: Estoy revisando mi codigo, y al quere adaptarlo a otro servidor, tengo que modificar muchos archivo, y quiero simplificar esta tarea. Este es ...
  #1 (permalink)  
Antiguo 14/01/2015, 10:40
 
Fecha de Ingreso: mayo-2014
Ubicación: Mexico
Mensajes: 79
Antigüedad: 9 años, 10 meses
Puntos: 2
Error a la conexion de bd

Buenos dias:

Estoy revisando mi codigo, y al quere adaptarlo a otro servidor, tengo que modificar muchos archivo, y quiero simplificar esta tarea.

Este es el codigo que utilizo para mis consultas, y esta funcionando:

Código PHP:
Ver original
  1. <?php
  2. include_once("../php/dbconfig.php");
  3. $db = new DBConnection();
  4. $db->getConnection();
  5. $sql = "select idcatmatrix as id from catmatrix where idcatagencia='dk09a'";
  6. $handle = mysql_query($sql);
  7. while ($row = mysql_fetch_object($handle)) {  
  8.     echo $row->id;
  9. }
  10. ?>

Codigo de dbconfig.php:

Código PHP:
Ver original
  1. <?php
  2. class DBConnection{
  3.     function getConnection(){
  4.       //change to your database server/user name/password
  5.         mysql_connect("localhost","root","123456") or
  6.          die("Could not connect: " . mysql_error());
  7.     //change to your database name
  8.         mysql_select_db("agenda3") or
  9.              die("Could not select database: " . mysql_error());
  10.     }
  11. }
  12. ?>


Todo bien hasta aqui, ahora utilizo JTable, JTable, y cada tabla tiene su propio conexion a bd, ejemplo:
Código PHP:
Ver original
  1. <?php
  2. try
  3. {
  4.     //Open database connection
  5.     $con = mysql_connect("localhost","root","123456");
  6.     mysql_select_db("agenda3", $con);
  7.  
  8.     //Getting records (listAction)
  9.     if($_GET["action"] == "list")
  10.     {
  11.         //Get record count
  12.         $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM CATMATRIX;");
  13.         $row = mysql_fetch_array($result);
  14.         $recordCount = $row['RecordCount'];
  15.  
  16.         //Get records from database
  17.         $result = mysql_query("SELECT idcatAgencia, idcatMatrix, Contra FROM CATMATRIX ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
  18.        
  19.         //Add all records to an array
  20.         $rows = array();
  21.         while($row = mysql_fetch_array($result))
  22.         {
  23.             $rows[] = $row;
  24.         }
  25.  
  26.         //Return result to jTable
  27.         $jTableResult = array();
  28.         $jTableResult['Result'] = "OK";
  29.         $jTableResult['TotalRecordCount'] = $recordCount;
  30.         $jTableResult['Records'] = $rows;
  31.         print json_encode($jTableResult);
  32.     }
  33.     //Creating a new record (createAction)
  34.     else if($_GET["action"] == "create")
  35.     {
  36.         //Insert record into database
  37.         $result = mysql_query("INSERT INTO catmatrix (idcatMatrix, idcatAgencia, Contra) VALUES (" . $_POST["idcatMatrix"] . ", '" . $_POST["idcatAgencia"] . "', '" . $_POST["Contra"] . "');");
  38.  
  39.    
  40.         //Get last inserted record (to return to jTable)
  41.         $result = mysql_query("SELECT * FROM catmatrix WHERE idcatAgencia = LAST_INSERT_ID();");
  42.         $row = mysql_fetch_array($result);
  43.  
  44.         //Return result to jTable
  45.         $jTableResult = array();
  46.         $jTableResult['Result'] = "OK";
  47.         $jTableResult['Record'] = $row;
  48.         print json_encode($jTableResult);
  49.     }
  50.     //Updating a record (updateAction)
  51.     else if($_GET["action"] == "update")
  52.     {
  53.         //Update record in database
  54.         $result = mysql_query("UPDATE `catmatrix` SET  `idcatAgencia`='" . $_POST["idcatAgencia"] . "', `Contra`='" . $_POST["Contra"] . "' WHERE `idcatMatrix`='" . $_POST["idcatMatrix"] . "';");
  55.  
  56.         //Return result to jTable
  57.         $jTableResult = array();
  58.         $jTableResult['Result'] = "OK";
  59.         print json_encode($jTableResult);
  60.     }
  61.     //Deleting a record (deleteAction)
  62.     else if($_GET["action"] == "delete")
  63.     {
  64.         //Delete from database
  65.         $result = mysql_query("DELETE FROM catmatrix WHERE idcatMatrix='" . $_POST["idcatMatrix"] . "'");
  66.  
  67.         //Return result to jTable
  68.         $jTableResult = array();
  69.         $jTableResult['Result'] = "OK";
  70.         print json_encode($jTableResult);
  71.     }
  72.  
  73.     //Close database connection
  74.     mysql_close($con);
  75.  
  76. }
  77. catch(Exception $ex)
  78. {
  79.     //Return error message
  80.     $jTableResult = array();
  81.     $jTableResult['Result'] = "ERROR";
  82.     $jTableResult['Message'] = $ex->getMessage();
  83.     print json_encode($jTableResult);
  84. }
  85. ?>

lo que quiero es que una vez configurado mi archivo dbconfig, ya no tenga que configurar mas.

En el archivo donde esta mis consultas, trato de mander a traer mi configuracion quedando asi:

Código PHP:
Ver original
  1. <?php
  2. include_once("../php/dbconfig.php");
  3. include_once("../php/functions.php");  
  4. $db = new DBConnection();
  5. $db->getConnection();
  6. $sql = "select idcatmatrix as id from catmatrix where idcatagencia='dk09a'";
  7. $handle = mysql_query($sql);
  8. while ($row = mysql_fetch_object($handle)) {  
  9.     echo $row->id;
  10. }
  11. ?>

lo pruebo y me imprime el resultado esperado. Pero al querer implimentarlo con mis consultas, me manda error:

Código PHP:
Ver original
  1. <?php
  2. include_once("../php/dbconfig.php");
  3. include_once("../php/functions.php");  
  4. $db = new DBConnection();
  5. $db->getConnection();
  6. try
  7. {
  8.     //Open database connection
  9.     $con = mysql_connect("localhost","root","123456");
  10.     mysql_select_db("agenda3", $con);
  11.  
  12.     //Getting records (listAction)
  13.     if($_GET["action"] == "list")
  14.     {
  15.         //Get record count
  16.         $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM CATMATRIX;");
  17.         $row = mysql_fetch_array($result);
  18.         $recordCount = $row['RecordCount'];
  19.  
  20.         //Get records from database
  21.         $result = mysql_query("SELECT idcatAgencia, idcatMatrix, Contra FROM CATMATRIX ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
  22.        
  23.         //Add all records to an array
  24.         $rows = array();
  25.         while($row = mysql_fetch_array($result))
  26.         {
  27.             $rows[] = $row;
  28.         }
  29.  
  30.         //Return result to jTable
  31.         $jTableResult = array();
  32.         $jTableResult['Result'] = "OK";
  33.         $jTableResult['TotalRecordCount'] = $recordCount;
  34.         $jTableResult['Records'] = $rows;
  35.         print json_encode($jTableResult);
  36.     }
  37.     //Creating a new record (createAction)
  38.     else if($_GET["action"] == "create")
  39.     {
  40.         //Insert record into database
  41.         $result = mysql_query("INSERT INTO catmatrix (idcatMatrix, idcatAgencia, Contra) VALUES (" . $_POST["idcatMatrix"] . ", '" . $_POST["idcatAgencia"] . "', '" . $_POST["Contra"] . "');");
  42.  
  43.    
  44.         //Get last inserted record (to return to jTable)
  45.         $result = mysql_query("SELECT * FROM catmatrix WHERE idcatAgencia = LAST_INSERT_ID();");
  46.         $row = mysql_fetch_array($result);
  47.  
  48.         //Return result to jTable
  49.         $jTableResult = array();
  50.         $jTableResult['Result'] = "OK";
  51.         $jTableResult['Record'] = $row;
  52.         print json_encode($jTableResult);
  53.     }
  54.     //Updating a record (updateAction)
  55.     else if($_GET["action"] == "update")
  56.     {
  57.         //Update record in database
  58.         $result = mysql_query("UPDATE `catmatrix` SET  `idcatAgencia`='" . $_POST["idcatAgencia"] . "', `Contra`='" . $_POST["Contra"] . "' WHERE `idcatMatrix`='" . $_POST["idcatMatrix"] . "';");
  59.  
  60.         //Return result to jTable
  61.         $jTableResult = array();
  62.         $jTableResult['Result'] = "OK";
  63.         print json_encode($jTableResult);
  64.     }
  65.     //Deleting a record (deleteAction)
  66.     else if($_GET["action"] == "delete")
  67.     {
  68.         //Delete from database
  69.         $result = mysql_query("DELETE FROM catmatrix WHERE idcatMatrix='" . $_POST["idcatMatrix"] . "'");
  70.  
  71.         //Return result to jTable
  72.         $jTableResult = array();
  73.         $jTableResult['Result'] = "OK";
  74.         print json_encode($jTableResult);
  75.     }
  76.  
  77.     //Close database connection
  78.     mysql_close($con);
  79.  
  80. }
  81. catch(Exception $ex)
  82. {
  83.     //Return error message
  84.     $jTableResult = array();
  85.     $jTableResult['Result'] = "ERROR";
  86.     $jTableResult['Message'] = $ex->getMessage();
  87.     print json_encode($jTableResult);
  88. }
  89. ?>
  #2 (permalink)  
Antiguo 14/01/2015, 10:42
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Error a la conexion de bd

Cita:
Pero al querer implimentarlo con mis consultas, me manda error:
¿Y qué error te da?

La telepatía no anda bien por este foro...
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #3 (permalink)  
Antiguo 14/01/2015, 10:48
 
Fecha de Ingreso: mayo-2014
Ubicación: Mexico
Mensajes: 79
Antigüedad: 9 años, 10 meses
Puntos: 2
Respuesta: Error a la conexion de bd

El error que me mando es de:

Ha ocurrido un error al conectar con el servidor.
  #4 (permalink)  
Antiguo 14/01/2015, 11:01
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Error a la conexion de bd

Eso no parece ser un mensaje propio de MySQL, por lo que o lo definiste tu en tu clase, o bien no se está ejecutando algo...

Mi primera duda es en qué servidor está corriendo eso. ¿Es en tu PC local? ¿En un hosting? ¿En dódne?
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #5 (permalink)  
Antiguo 14/01/2015, 11:27
 
Fecha de Ingreso: mayo-2014
Ubicación: Mexico
Mensajes: 79
Antigüedad: 9 años, 10 meses
Puntos: 2
Respuesta: Error a la conexion de bd

Lo que pasa es que estoy utilizando la interfaz JTable, jtable, jquery, es el mensaje de la interfaz.

Etiquetas: bd, conexion, mysql, select, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 06:09.