Error fetching MySQL data with PHP [closed]

-1

I can not get information to the database using php. Here's the code I've implemented to fetch this information:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$sql = "SELECT pergunta, resA, resB,resC,resD,resE,acertaram,falharam,dificuldade,resposta,imgSrc FROM perguntas Where ID = 1";  //This is where I specify what data to query
$result = mysqli_query($conn, $sql);


if(mysql_num_rows($sql)){

    echo $result;

} else echo '<h1 style=" font-size: 30px; font-color: red ">ERRO AO PESQUISAR<h1><br /> <p>Ocorreu um erro enquanto se processava a querry.</p>';

The result is always the last echo and I can not figure out why. When I run this querry in phpmyadmin it returns a column so it should enter if , but it does not go in. Does anyone know where to go wrong?

    
asked by anonymous 02.02.2017 / 12:34

2 answers

2

It is $result instead of $sql .

Another thing is that its function is mysql_num_rows . It should be mysqli_num_rows , with i not mysqli .

$sql = "SELECT
    pergunta,
    resA,
    resB,
    resC,
    resD,
    resE,
    acertaram,
    falharam,
    dificuldade,
    resposta,
    imgSrc
FROM
    perguntas
WHERE
    ID = 1";

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

if(mysqli_num_rows($result) > 0){

}
else{

}
    
02.02.2017 / 12:36
0

You are passing the query raw instead of passing the resource:

mysql_num_rows($sql)

In addition, the mysql_ * function is used instead of mysqli_ * as required. The correct one would be:

mysqli_num_rows($result)
    
02.02.2017 / 12:36