0

Hola buenas tengo este problema con mi php el cual no me ha dejado seguir en el cual me sale el siguiente error

Fatal error: Uncaught Error: Call to undefined function mysql_fetch_assoc() in /storage/ssd1/730/8168730/public_html/Ingresar.php:144 Stack trace: #0 {main} thrown in /storage/ssd1/730/8168730/public_html/Ingresar.php on line 144

les dejo mi código

<html>
<head>
       <style>
        th{
            color:black;
            background-color: white;
            font-family: Arial,sans-serif;
            font-size:24px;
        }
        td{
            background-color: white;
            color: black;
            font-family: Arial,sans-serif;
            font-weight: bold;
        }
input[type=submit]{
    font-family: arial,sans-serif;
    font-weight: bold;
    text-align: center;
    color:black;
    margin-top: 4px;
    margin-bottom: 5px;
    padding: 2px;
    border: 2px solid black;
}
input[type=submit]:hover{
    font-family: arial,sans-serif;
    font-weight: bold;
    text-align: center;
    color:red;
    margin-top: 4px;
    margin-bottom: 5px;
    padding: 2px;
    border: 2px solid #FF0000;
}

    </style>
</head>
<script>
function valida(e){
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8){
        return true;
    }
    patron =/[0-9]/;
    tecla_final = String.fromCharCode(tecla);
    return patron.test(tecla_final);
}
</script>
<body>
<body background="Imagenes/Fondo6.jpg">
<center>
<table width="350" border="1">
<form action="Datos.php" method="post">
<tr >
<th colspan="3">INGRESO DE DATOS</th>
</tr>
<tr>
<td>Rut:</td>
<td colspan="2">
<input type="text" name="txtRut" placeholder="11.111.111-1" maxlength="12" align="right" size="30" tabindex="1" required>
</td>
</tr>
<tr>
<td>Ciudad:</td>
<td><input type="text" name="txtCiudad" size="30" maxlength="25" tabindex="2" required><br></td>
</tr>
<tr>
<td>Fecha:</td>
<td><input type="text" name="txtFecha" size="30" maxlength="20" tabindex="2" required><br></td>
</tr>
<tr>
<td>Producto:</td>
<td><input type="text" name="txtProducto" size="30" maxlength="25" tabindex="2" required><br></td>
</tr>
<tr>
<td>Valor Unitario:</td>
<td><input type="text" name="txtValor" size="30" id="utilidad" required><br></td>
</tr>
<tr>
<td>Cantidad:</td>
<td><input type="text" name="txtCantidad" size="30" id="utilidad"  maxlength="4" required><br></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="txtTotal" size="30" id="utilidad" disabled tabindex="3"><br></td>
</tr>
<tr>
<td>FormaPago:</td>
<td><input type="radio" name="FormaPago" value="Contado">Contado 20% dcto.<br>
<input type="radio" name="FormaPago" value="TarjetaBanco">Tarjeta Banco 10% dcto.<br>
<input type="radio" name="FormaPago" value="TarjetaTienda">Tarjeta Tienda 10% inte.</td>
</tr>
<tr>
<td>Paga Con?:</td>
<td><input type="text" name="txtMetodoPago" size="30" id="utilidad"  maxlength="4" disabled tabindex="3"><br></td>
</tr>
<tr>
<tr>
<td>Descuento:</td>
<td><input type="text" name="txtDescuento" size="30" id="utilidad"  maxlength="4" disabled tabindex="3"><br></td>
</tr>
<tr>
<td>Interes:</td>
<td><input type="text" name="txtInteres" size="30" id="utilidad"  maxlength="4" disabled tabindex="3"><br></td>
</tr>
<tr>
<td>Total Pagar:</td>
<td><input type="text" name="txtTotalPagar" size="30" id="utilidad"  maxlength="4" disabled tabindex="3"><br></td>
</tr>
<td colspan="3" align="center">
<input type="submit" name="BtnGrabar" value="Guardar">
</td>
           </tr>
        </form>
         </table>
      </center>

      <br><br>
       <center>
 <table border="1" width="1000">
          <tr>
          <td colspan="20" align="center">LISTADO DE DATOS DE LA COMPRA</td>
          </tr>
          <tr align="center">
          <td>N°</td>
          <td>Rut</td>
          <td>Ciudad</td>
          <td>Fecha</td>
          <td>Producto</td>
          <td>Val.Unit.</td>
          <td>Cantidad</td>
          <td>Total</td>
          <td>Forma Pago</td>
          <td>Descuento</td>
          <td>Interes</td>
          <td>Total Pagar</td>
           </tr>
               <?php
            include("ConexionPHP.php");
            $TTablaP ="Select * From compra";
            $Buscar = $ConexionBD->query($TTablaP);
            $inc = 0;
            while ($Registro = mysql_fetch_assoc($Buscar))
              {
              $inc++;
              ?>
           <tr>
              <td><?php echo $inc; ?></td>
              <td><?php echo $Registro['Rut']; ?></td>
              <td><?php echo $Registro['Ciudad']; ?></td>
              <td><?php echo $Registro['Fecha']; ?></td>
              <td><?php echo $Registro['Producto']; ?></td>
              <td><?php echo $Registro['ValorUnitario']; ?></td>
              <td><?php echo $Registro['Cantidad']; ?></td>
              <td><?php echo $Registro['Total']; ?></td>
              <td><?php echo $Registro['FormaPago']; ?></td>
              <td><?php echo $Registro['Descuento']; ?></td>
              <td><?php echo $Registro['Interes']; ?></td>
              <td><?php echo $Registro['TotalPagar']; ?></td>
           </tr>
              <?php
            }
              ?>
      </table>
</center>
      </body>
</html>
1
  • que versión de php usas?
    – user75901
    el 8 dic. 2018 a las 15:45

3 respuestas 3

1

Si estas usando PHP 7 o superior, entonces te aviso que desde esta versión; las funciones de mysql_* han sido removidas; entonces deberías trabajar con:

  1. PDO
  2. MySQLi

La conexión debería ser:

<?php

$conexion = new mysqli("tuservidor", "tuusuario", "tu contraseña", "tuBD");

La consulta debería ser así:

$consulta = $conexion->query("SELECT * FROM compra");

Ahora para recorrer el conjunto de valores debería ser algo así:

while ($fila = $consulta->fetch_assoc()) {
        echo $fila["ColumnaNombre1"];
        echo $fila["ColumnaNombre2"];
        ...................
    }
1

Acabo de cambiarlo y me quedo así

          <td>Total Pagar</td>
           </tr>
               <?php
            include("ConexionPHP.php");
            $conexion = new mysqli("localhost", "id8168730_ipex", "walter15", "id8168730_ipexchile");
            $consulta = $conexion->query("SELECT * FROM compra");
            $inc = 0;
            while ($fila = $consulta->fetch_assoc())
              {
              $inc++;
              ?>
           <tr>
              <td><?php echo $inc; ?></td>
              <td><?php echo $fila['Rut']; ?></td>
              <td><?php echo $fila['Ciudad']; ?></td>
              <td><?php echo $fila['Fecha']; ?></td>
              <td><?php echo $fila['Producto']; ?></td>
              <td><?php echo $fila['ValorUnitario']; ?></td>
              <td><?php echo $fila['Cantidad']; ?></td>
              <td><?php echo $fila['Total']; ?></td>
              <td><?php echo $fila['FormaPago']; ?></td>
              <td><?php echo $fila['Descuento']; ?></td>
              <td><?php echo $fila['Interes']; ?></td>
              <td><?php echo $fila['TotalPagar']; ?></td>
           </tr>
              <?php
            }
              ?>
      </table>
</center>
      </body>
</html>

pero aun me sigue saltando el mismo error Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in /storage/ssd1/730/8168730/public_html/Ingresar.php:144 Stack trace: #0 {main} thrown in /storage/ssd1/730/8168730/public_html/Ingresar.php on line 144

y la conexion la tengo asi

<?php
$Servidor = "localhost";
$Usuario = "id8168730_ipex";
$Password = "walter15";
$NombreBD = "id8168730_ipexchile";

$conexion = new mysqli("localhost", "id8168730_ipex", "walter15", "id8168730_ipexchile");
?>
0

Te doy la bienvenida a Stackoverflow.

Si ConexionPHP.php tienes todo lo relativo a la conexión, no tienes que crearla de nuevo como haces aquí:

        include("ConexionPHP.php");
        $conexion = new mysqli("localhost", "id8168730_ipex", "walter15", "id8168730_ipexchile");

Sino simplemente incluir el archivo y usar $conexion, porque ya existe.

De todos modos, yo pondría más controles en el código:

          <td>Total Pagar</td>
           </tr>
               <?php
            include("ConexionPHP.php");
            if( $conexion ){
                if($consulta = $conexion->query("SELECT * FROM compra")){;
                    $inc = 0;
                    while ($fila = $consulta->fetch_assoc())
                    {
                          $inc++;
                          ?>
                       <tr>
                          <td><?php echo $inc; ?></td>
                          <td><?php echo $fila['Rut']; ?></td>
                          <td><?php echo $fila['Ciudad']; ?></td>
                          <td><?php echo $fila['Fecha']; ?></td>
                          <td><?php echo $fila['Producto']; ?></td>
                          <td><?php echo $fila['ValorUnitario']; ?></td>
                          <td><?php echo $fila['Cantidad']; ?></td>
                          <td><?php echo $fila['Total']; ?></td>
                          <td><?php echo $fila['FormaPago']; ?></td>
                          <td><?php echo $fila['Descuento']; ?></td>
                          <td><?php echo $fila['Interes']; ?></td>
                          <td><?php echo $fila['TotalPagar']; ?></td>
                       </tr>
              <?php
                    }
                } else {
                    //Si quieres puedes imprimir un mensaje de consulta fallida
                }
            } else {
                    //Si quieres puedes imprimir un mensaje de conexión fallida
            }
              ?>
      </table>
</center>
      </body>
</html>

Generalmente yo suelo imprimir un mensaje en los else para saber qué error ocurrió. Aquí no lo he hecho, viendo que estabas construyendo una tabla con código que le precede. Si no te muestra el error, pero no hay datos en la tabla, significa que ocurrió alguno de los else, puedes imprimir los mensajes de error temporalmente en la misma tabla o en otra parte, para ver lo que está ocurriendo.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.