Mysql error "expects Parameter 1 to be Resource, Boolean Given in"

Asked

Viewed 13,578 times

16

$query = "Select * from servico where ID_SERVICO = $id";
$result = mysql_query($query);
if($row = mysql_fetch_array($result)) {
    $nome = $row['NOME'];       

    if($nome == 'Marketing') {
        include ("servicos/marketing.php"); 
    }
    if($nome == 'Web-Marketing') {              
        include ('servicos/webmarketing.php');
    }
    if($nome == 'Serviços Web') {
        include ("servicos/servicosweb.php");
    }
    if($nome == 'Design Gráfico') {
        include ("servicos/designgrafico.php");
    }
}

I have this code and I can’t see the error in it, but it gives me this error. I know there should already be a topic of this on the site but I’ve tried all the ways and nothing. Gives me the bool error(false)

  • 1

    This happens when your query fails, you can print it and test it directly in the bank or make mysq_* show the bank error like this: $result = mysql_query($query) or die(mysql_error());

  • Solved, thank you.

  • 1

    What was the problem?

  • 2

    It was a wrong name in the query, my lack of attention.

2 answers

15

This mistake happens when mysql_query() or mysqli_query() failure, is usually a syntax error in the sql query and returns a false as explained in manual. To mysql_fetch_*() it is necessary to pass a variable of type resource or mysqli_resource which is the return of mysql_query()/mysqli_query() in case of success.

To fix the error you can force mysql_* /mysqli_ * to display the bank error with the function mysql_error() or mysqli_error()

Version with the old mysql functions_*

$sql = 'SELECT * FROM desc';
$resource = mysql_query($sql) or die(mysql_error());

Proceduralmysqli_* version

$sql = 'SELECT * FROM desc';
$resource = mysqli_query($sql) or die(mysqli_error($conexao));

Version with mysqli_* OO

$sql = 'SELECT * FROM desc';
$resource = $db->query($sql) or die($db->error);

The query will return this error because desc is a reserved word from mysql.

Error Code: 1064. You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'desc'

The other way is to print the query and test directly in the database:

$sql = 'SELECT * FROM desc';
echo $sql;

Related:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

How to print the SQL statement being sent to the database?

  • I put the reward for the question and your answer gain more votes and more relevance, serving as canonical for several duplicates. But it didn’t seem to work out so well.

  • @bfavaretto was worth the intention :)

0

The problem is because the method mysql_query requires a parameter with the database connection.

I leave below an example. Remember to modify the data for connection to the database.

I leave as a tip for you to study the use of PDO for he is safer and the mysql_* is already depreciated..

Example:

$query = "Select * from servico where ID_SERVICO = $id";
// variavel com a conexão do banco de dados.
$conn = mysql_connect('servidor', 'usuario', 'senha') or die ('Verifique seus dados de conexão com o banco de dados. Detalhes: ' . mysql_error());
// selecionando banco de dados
mysql_select_db('nome_do_banco', $conn);

$result = mysql_query($query, $conn);
if($row = mysql_fetch_array($result)) {
    $nome = $row['NOME'];       

    if($nome == 'Marketing') {
        include ("servicos/marketing.php"); 
    }
    if($nome == 'Web-Marketing') {              
        include ('servicos/webmarketing.php');
    }
    if($nome == 'Serviços Web') {
        include ("servicos/servicosweb.php");
    }
    if($nome == 'Design Gráfico') {
        include ("servicos/designgrafico.php");
    }
}

Browser other questions tagged

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