I have this problem Php Fatal error: Uncaught Error: Call to undefined function mysql_fetch_assoc ()

0

Hi, I have this problem with my php, which has not let me continue, in which I get the following 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

I leave my code

<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>
    
asked by Walter Sepulveda Alvarez 08.12.2018 в 16:42
source

3 answers

0

Welcome to Stackoverflow.

If ConexionPHP.php you have everything related to the connection, you do not have to create it again as you do here:

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

But simply include the file and use $conexion , because it already exists.

Anyway, I would put more controls in the code:

          <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>

Generally I usually print a message in else to know what error occurred. Here I have not done it, seeing that you were building a table with code that precedes it. If you do not see the error, but there is no data in the table, it means that one of the else occurred, you can print the error messages temporarily in the same table or elsewhere, to see what is happening.

    
answered by 08.12.2018 / 21:02
source
1

If you are using PHP 7 or higher, then I warn you from this version; mysql_* functions have been removed; then you should work with:

  • PDO
  • MySQLi
  • The connection should be:

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

    The query should look like this:

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

    Now to go through the set of values should be something like this:

    while ($fila = $consulta->fetch_assoc()) {
            echo $fila["ColumnaNombre1"];
            echo $fila["ColumnaNombre2"];
            ...................
        }
    
        
    answered by 08.12.2018 в 16:50
    1

    I just changed it and I stay like this

              <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>
    

    but I'm still jumping the same 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");
    ?>
    
        
    answered by 08.12.2018 в 17:19