← PHP

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

Lida 3605 vezes

Offline

alexandremdn 
Membro
Mensagens 94 Gostos 2
Feedback +2

Troféus totais: 25
Trófeus: (Ver todos)
Nineth year Anniversary Level 5 Level 4 Avatar Eighth year Anniversary Seventh year Anniversary Windows User Super Combination Combination Topic Starter

boas...

tenho este código e esta a dar-me o erro que esta no assunto:

Código: [Seleccione]

$servidor = "localhost";
$usuario = "User";
$senha = "senha";
$banco = "BD;

$conexao = mysql_connect($servidor,$usuario,$senha); 

mysql_select_db($banco);

$res = mysql_query("select nome, mail from newsletters");

echo "
<table>
<tr>
<td>Nome</td>
<td>Mail</td>
</tr>
";

while($values=mysql_fetch_array($res)){/*ESTA A DAR ERRO NESTA LINHA*/

echo "<tr><td>" . $values['nome'] . "</td><td>";
$values['mail'] ."</td></tr>";

}/*Fim do while*/
echo "</table>";

mysql_close();



alguem sabe o k esta errado?
Offline

diogoosorio 
Membro
Mensagens 134 Gostos 1
Feedback +1

Troféus totais: 22
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Do manual:

Citar
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

A seguir lê:

http://pt.php.net/manual/en/book.pdo.php

--

Em relação ao teu código, isso é mesmo à campeão. :)

E se a função mysql_connect() falhou? E se a função mysql_select_db() falhou? E se a função mysql_query() falhou?

Posso-te dizer que a 3ª falhou de certeza. Mais uma vez do manual:

Citar
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

--

Se PHP é a 1ª linguagem em que estás a mexer, tenta ir para outra coisa antes (Java, C#, ...). PHP potencia más práticas e o pedaço de código que tens ai em cima é um exemplo claro desta situação.

Boa sorte!