Error Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in [duplicate]

-4

I know you should have other similar answers but in none of them did I find what I needed. In the database I have a table with id, name, version, upload and size. I need only get the version of the file that I selected by SELECT the code is as follows:

<?php
$num = 1; 
if($num == 1){
    $sql = mysqli_query( $link,'SELECT * FROM files id = 2');
    $resource = mysqli_query($sql) or die(mysqli_error($conexao));

    while($ln = mysql_fetch_array($result, MYSQL_NUM)){
        echo " ID: ". $ln['id'];
        echo " Nome: " .$ln['nome'];
        echo " Versão: ". $ln['version'];
        echo " Upload: ". $ln['upload'];
        echo " Size: ". $ln['size'];
        echo "Deu certo";
        echo '<tr><td>'.$ln['version'].'</td>;
}
?>

But whenever I try to run it, the following error appears: Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in. What should I do?

    
asked by anonymous 21.09.2017 / 21:20

1 answer

3

Your script has at least 5 errors

First, this line is wrong:

$resource = mysqli_query($sql) or die(mysqli_error($conexao));

Another strange thing is that for the connection you have two variables $conexao and $link , one of them must be wrong.

Here you set $resource , but I believe that what I wanted was $result

Otherwise you passed the result of a mysqli_query to another:

$sql = mysqli_query( $link,'SELECT * FROM files id = 2');
$resource = mysqli_query($sql) or die(mysqli_error($conexao));

Another error is that WHERE is missing from your SELECT:

SELECT * FROM files id = 2

Should be:

SELECT * FROM files WHERE id = 2

What makes no sense

And you're mixing the old API of mysql with the new API called mysqli , the correct one would be:

Favorite I know I should have other similar answers, but in none of them did I find what I needed. In the database I have a table with id, name, version, upload and size. I need only get the version of the file that I selected by SELECT the code is as follows:

<?php
$num = 1; 
if($num == 1){

    $result = mysqli_query($link, 'SELECT * FROM files WHERE id = 2') or die(mysqli_error($link));

    while ($ln = mysqli_fetch_array($result, MYSQLI_NUM)) {
        ...
    }

    mysqli_free_result($result);

A very simple example of mysqli

<?php
//$link é a variavel da "conexão"
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* Verifica erros */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

$result = mysqli_query($link, 'SELECT ...') or die(mysqli_error($link));

/* array numerica */
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    printf ("%s (%s)\n", $row[0], $row[1]);
}

/* array associativa */
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    printf ("%s (%s)\n", $row["Nome"], $row["Sobrenome"]);
}

/* associativa e numerica */
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    printf ("%s (%s)\n", $row[0], $row["Sobrenome"]);
}

/* libera a memoria */
mysqli_free_result($result);

/* fecha a conexão */
mysqli_close($link);

Read the documentation

A tip, do not go about doing things randomly, documentation exists to be used, follow the links:

21.09.2017 / 21:26