Error while fetching Mysql data with PHP

Asked

Viewed 56 times

-1

I can’t get information from the database using php. See the code I implemented to get 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’t figure out why. When I run this querry on phpmyadmin it returns a column so it should enter the if, but it doesn’t. Someone can tell me where I went wrong?

2 answers

2


IS $result in place of $sql.

Another thing is that its function is mysql_num_rows. Must be mysqli_num_rows, with i in the 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{

}
  • It remains exactly the same after the exchange. It seems that it does not return anything here $result = mysqli_query($conn, $sql);

  • Sey SGBD? I don’t know what this is, but as had said in phpmyadmin it returns a column

  • SGBD is a system or software that you manage the database, like Phpmyadmin. I changed my answer, give a read.

  • I can’t believe I fell for that. That’s how you get into if (even without the comparison), I can conclude that he searched the information of the corresponding column?

  • You can. You don’t need the comparison. If it’s bigger than 0 automatically is TRUE, then it enters the if.

0

You are passing raw from the query instead of going to Resource:

mysql_num_rows($sql)

In addition, the mysql_* function is being used and not mysqli_* as required. The right thing would be::

mysqli_num_rows($result)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.