Error while fetching data in MYSQL

Asked

Viewed 33 times

0

I’m using PHP + MYSQL to make some queries in my database, this page in php is the same one I use on another host, there works normal though, when using on a different host it just doesn’t work.

What have I changed?

Layout and connections.

The error in question is

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, 
boolean given in /home/slim333/public_html/admin/consultar.php on line 134

Ativados:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, 
boolean given in /home/slim333/public_html/admin/consultar.php on line 139

Não Ativados:
Warning: mysql_fetch_array() expects parameter 1 to be resource, 
boolean given in /home/slim333/public_html/admin/consultar.php on line 148

And on the lines where the errors are occurring are the functions:

  1. Line 134 $data=mysql_fetch_assoc($result);
  2. Line 139 $data=mysql_fetch_assoc($result);
  3. Line 148 while($lista2 = mysql_fetch_array($sql)){

The current code looks like this:

<?php
$result = mysql_query("SELECT count(*) as total from n_emails WHERE ativo='s'");
$data = mysql_fetch_assoc($result);
echo "<font class='bebas' size='6' style='color:green'>Ativados: </font><font class='bebas' size='6' style='color:gray'>";
echo $data['total'];
echo "</font><br>";
$result=mysql_query("SELECT count(*) as totali from n_emails WHERE ativo='n'");
$data=mysql_fetch_assoc($result);
echo "<font size='6' class='bebas' style='color:red'>Não Ativados: </font><font class='bebas' size='6' style='color:gray'>";
echo $data['totali'];
echo "</font>"
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<br>
<?php
$sql = mysql_query("SELECT * FROM n_emails order by -id");
while($lista2 = mysql_fetch_array($sql)){
$email = $lista2["email"];
$ativo = $lista2["ativo"];
if($ativo=="n"){
$ativo = "<font face='Arial' size='2' color='red'>Não confirmado</font>";
}
else{
$ativo = "<font face='Arial' size='2' color='green'>Confirmado</font>";
}
?>

I don’t know what I’m doing wrong, the question is:

Is the error really in those lines? If so, how can I manage to fix them ?

I need a light.

@EDIT 20:38

Is not duplicate of this question because in that case, the person had not separated the code in the correct way, but in my case, it is already as the answer to the problem of Cauê Siqueira.

  • 2

    It is exactly the same problem, and the cause is explained in the answer. The "style" of the code does not change the error. If you want, edit with your code and put the part of query, that we can better explain something that you have not understood. But to summarize, it is the same error, and the same cause. The connection or the query has error, and is missing proper treatment before trying to use the feature You are using the fetch in a return that failed.

  • @Bacco, sorry my knowledge in PHP is almost equal to zero haha, I edited the question with the whole code (the part that refers to the error).

  • 2

    The first test would be to add this at the end of the line: $result = mysql_query("SELECT count(*) as total from n_emails WHERE ativo='s'") or die( mysql_error() ); - and no need to apologize, ask more than one question about the same problem is not wrong. The duplicate even helps people find the same answer by searching for different terms. The idea is that if there is an error in $result, PHP will already stop and indicate the problem.

  • 2

    Same thing on that line $sql = mysql_query("SELECT * FROM n_emails order by -id") or die( mysql_error() ); - The "or die" will cause PHP to "die" if the query fails, showing the error on the screen. (is what is in the answer to the question indicated for closure).

  • 1

    Take the test, and tell us what the result was, we’re here to help. If this doesn’t solve, we can test other things.

  • @Bacco, thank you very much for what you are doing ;) finally, I did the tests and it seems the error is not in the code, returned No database selected, I mean, the problem is on the right connection? But I checked the config.php and it has no error, the other parts of the system fail to make queries too, but do not return errors, just do not show the results saying the same thing No database selected

  • There are two good solutions: or you put the table name like this: SELECT talcoisa FROM nomedobanco.n_emails, or use the mysql_select_db( 'nomedobanco' ); before the mysql_query - You can specify on the connection as well, but usually if you use the mysql_select_db separate, or the full name of the table.table name helps use the same connection for different banks

  • Examples: http://pastebin.com/Y1v9fMuN

  • Do not confuse the connection with DB selection. On the connection vc specifies the server, this server can have multiple banks, each database with its tables. In your case, the connection worked, missed to choose the bank only.

  • I did it, now the mistake was SELECT command denied to user 'slima248_user05'@'localhost' for table 'n_emails'

  • I saw that the user seems not to have permission, I searched for the error, I found a query to run directly in phpmyadmin, I started and an error appeared: Mysql: GRANT command denied to user 'slima248_user05'@'localhost' for table 'n_emails', I looked for this mistake too, I found an answer but it did not help me, I’m almost cutting my wrists because it is irritating me and very haha

  • Okay, the important thing is that you are surrounding the real problem, which is to ensure that the thing only continues after solving these steps. In this case, we missed authorizing slima248_user05@'localhost' to access this table. Remember that when using GRANT, you have to say the user name and the host. (or % to specify any host) - And in this case, you have to access as admin to give GRANT to the user

  • Is the problem is just this, the only error, authorize the user to access the table, however not giving, already tried to change the user, give permissions and nothing, same error

  • Hehehe now you’ll have to have some fun with permissions (but you’ve already won the question problem, at least). I would suggest seeing with the DB administrator this permission (or if the DB is yours, access as root user to do Grant)

Show 9 more comments
No answers

Browser other questions tagged

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