CRUD data with bootstrap and PHP [duplicated]

1

I have a problem with some statements that I am using for a CRUD of data with bootstrap, php and html.

I have the following code in which I connect to the DB without including an external library.

<?php
            function conectar_bd($query)
    {
      $DB_SERVER = "localhost";
        $DB_USER = "root";
        $DB_DATABASE = "dbAdmin";
        $DB_PWD = "12345";

      $conn=mysqli_connect($DB_SERVER, $DB_USER,$DB_PWD,$DB_DATABASE);
        /* check connection */
        if (mysqli_connect_errno())
        {
            printf("Connect failed: %s\n" , mysqli_connect_error());
            exit();
        }
        if ($result=mysqli_query($conn,$query))
           echo mysqli_connect_error();
        return $result;
    }

    $sql = "SELECT * FROM administradores ORDER BY Nombre ASC";
    $result = mysqli_query($sql);   ##LINEA DE ERROR
    if(mysqli_num_rows($result) == 0){ ##LINEA DE ERROR 
                echo '<tr><td colspan="8">No hay datos.</td></tr>';
            }else{
                $no = 1;
                while($row = mysqli_fetch_assoc($result)){
                    echo '
                    <tr>
                        <td>'.$row['idAdmin'].'</td>
                        <td>'.$row['Nombre'].'</td>
          <td>'.$row['Usuario'].'</td>
                        <td>'.$row['RPE'].'</td>
                        <td>'.$row['admin_desde'].'</td>
                        <td>

                            <a href="edit.php?nik='.$row['idAdmin'].'" title="Editar datos" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>

                            <a href="consultaadmin.php?aksi=delete&nik='.$row['idAdmin'].'" title="Eliminar" onclick="return confirm(\'Esta seguro de borrar los datos '.$row['idAdmin'].'?\')" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
                        </td>
                    </tr>
                    ';
                    $no++;
                }
            }
            ?>

The case is what throws me these two errors when running in the browser and does not show me the consumption.

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in   C: \ xampp \ htdocs \ sistema.tics \ consultadmin.php on line 73

     

Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result,   null given in C: \ xampp \ htdocs \ system.tics \ queryadmin.php on line   74

My table exists and already verify that all the names of both table and rows are fine.

    
asked by Jair Mejía López 20.07.2018 в 20:52
source

2 answers

4

I commented that within mysqli, the configuration should be as follows

$result = mysqli_query($conn, $sql);  
  

In other words, first the variable that carries the connection inside and   then the variable that is saving the query

  

Regarding the error that tells you that num_rows() received a   null value, that's why trying to use variable $result ,   but it comes from mysqli_query() that the variable is missing    $conn therefore the query is not processed successfully and arrives with   a value NUll

According to PHP's own documentation and I quote:

Return Values Returns FALSE in case of error. If a query from type SELECT, SHOW, DESCRIBE or EXPLAIN is successful, mysqli_query () will return a mysqli_result object. For other successful consultations of mysqli_query () will return TRUE.

Source: link

UPDATE

I suggest you leave your code as follows

<?php

      $DB_SERVER = "localhost";
        $DB_USER = "root";
        $DB_DATABASE = "dbAdmin";
        $DB_PWD = "12345";

      $conn=mysqli_connect($DB_SERVER, $DB_USER,$DB_PWD,$DB_DATABASE);
        /* check connection */
        if (mysqli_connect_errno())
        {
            printf("Connect failed: %s\n" , mysqli_connect_error());
            exit();
        }

    $sql = "SELECT * FROM products";
    $result = mysqli_query($conn, $sql);   ##LINEA DE ERROR
    if(mysqli_num_rows($result) == 0){ ##LINEA DE ERROR 
                echo '<tr><td colspan="8">No hay datos.</td></tr>';
            }else{
                while($row = mysqli_fetch_assoc($result)){
                    echo '
                    <tr>
                        <td>'.$row['idAdmin'].'</td>
                        <td>'.$row['Nombre'].'</td>
                        <td>'.$row['Usuario'].'</td>
                        <td>'.$row['RPE'].'</td>
                        <td>'.$row['admin_desde'].'</td>
                    </tr>
                    ';
                }
            }
            ?>
  

I removed the connect function and only invoke said variable in a way   direct where required, I made an example in local and it is functional

    
answered by 20.07.2018 в 21:03
1

You have to specify to which connection you are going to make the query like this:

$result = mysqli_query($conn, $sql);
    
answered by 20.07.2018 в 20:54