Warning error: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in

Asked

Viewed 1,704 times

-4

I know you probably have other similar answers, but none of them I thought you needed. In the database I have a table with id, name, version, upload and size. I just need to get the version of the file I selected by SELECT the code is below:

<?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 execute it appears the following error: Warning: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in. What should I do?

  • mysqli_fetch_array; i missed an i

  • and $result there is no?

  • I have already checked and I have just corrected, but what would be the right way to put Select?

  • SELECT * FROM files WHERE id = 2. Missed the WHERE

  • but it does not have " ", or ' '

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 it $resource, but I think what I wanted was $result

Besides you’re past the result of a mysqli_query in another:

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

Another mistake is that the WHERE in your SELECT:

SELECT * FROM files id = 2

Should be:

SELECT * FROM files WHERE id = 2

What doesn’t make any sense

And you’re mixing the old API’s mysql with the new API called mysqli, the correct would be:

favorite I know you probably have other similar answers, but none of them I thought you needed. In the database I have a table with id, name, version, upload and size. I just need to get the version of the file I selected by SELECT the following code:

<?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 hint, do not go around doing things randomly, documentations exist to be used, follow the links:

Browser other questions tagged

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