execute query mysql PHP [duplicated]

0

I'm trying to execute a query and show me the data.

<?php 
    $sql = "SELECT nombre FROM canciones WHERE idartistas = '$idartistas' ";                 
?> 

I have that done in php and to show the data I do the following;

<p align="left">
  <?php print $sql?>
</p>

When I open the page, it shows me the query and not the query data.

I have changed and put this but now it gives me this fault:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in   C: \ xampp \ htdocs \ PaginaWebPBLlocal \ todoartista.php on line 88

     

Warning: mysqli_error () expects exactly 1 parameter, 0 given in   C: \ xampp \ htdocs \ PaginaWebPBLlocal \ todoartista.php on line 88 Query   failed:

    $conexion = new mysqli("localhost", "root", "", "discografica");
        if ($conexion->connect_errno) {
    echo "Fallo al conectar a MySQL: (" . $conexion->connect_errno . ") " . $conexion->connect_error;
}
    $query = "SELECT nombre FROM canciones WHERE idartistas = '$idartistas' ";
    $result = mysqli_query($query) or die('Consulta fallida: ' . mysqli_error());

?> 
    
asked by NuriaOveeeeeeejero 21.02.2018 в 17:08
source

2 answers

0

The error at the moment of making the query and obtaining the result is because you are only passing 1 parameter to mysqli_query :

$result = mysqli_query($query) or die('Consulta fallida: ' . mysqli_error());

The correct syntax for mysqli_query is this: $resultado($conexionABaseDeDatos, $query, MODO_DEL_RESULTADO);

(The constant of the result mode can be MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on your needs) .

The second error is because mysqli_error() requires the connection to the database as a parameter: mysqli_error($conexionDB)

So the correct way to get your result would be the following :

$resultado = mysqli_query($conexion, $query, MYSQLI_USE_RESULT) or die('Consulta fallida: ' . mysqli_error($conexion));

After this the variable $ result will be equal to the data of the conssulta, in this way you will be able to print what it contains.

I hope it was your help:)

    
answered by 21.02.2018 в 17:50
0

What you are doing there is to create a simple variable called sql with the value in string:

SELECT nombre FROM canciones WHERE idartistas = '$idartistas'

For connection to databases you need other PHP objects that allow you to execute the queries, you can guide this example for connection with MYSQL: link

    
answered by 21.02.2018 в 17:15