0

I have this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\projeto\BD\proj_detalhes.php on line 25

<?php
$bd = "crowdfunding";
$connection = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db($bd) or die(mysql_error());
$sql = "SELECT projeto.id, nome, limite, projeto.descricao FROM projeto,(SELECT valor, recompensa.descricao FROM recompensa, projeto WHERE projeto.id = id_projeto)";
$res = mysql_query($sql);
?>

<html>
<head>
<title>Projects</title>
</head>
<body>
<h1>Details<br /></h1>
<table>
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Final data</strong></td>
<td><strong>Description</strong></td>
<td><strong>Value</strong></td>
<td><strong>Reward Description</strong></td>
</tr>
<?php
while($dados = mysql_fetch_array($res)){
$id = $dados['id'];
$nome = $dados['nome'];
$data = $dados['limite'];
$descricao_proj = $dados['projeto.descricao'];
$valor = $dados['valor'];
$descricao_rec = $dados['recompensa.descricao'];

echo "<tr>
    <td>$id</td>
        <td>$nome</td>
        <td>$data</td>
        <td>$descricao_proj</td>
    <td>$valor</td>
    <td>$descricao_rec</td>  
</tr>";
}
?>
</table><br /><br />
<form action="menu1.php" method="get">
<center><input name="button" type="submit" value="Back"/></center>
</form>
</body>
</html>

My first variables are not in english, try to understand it. But my goal in here is to show from project table my ID name and description and from my reward table i want to show value and description (only if id from project is equal to the variable i created project_id)

  • 1
    Don't use the mysql_* functions anymore! They are deprecated! Try instead using PDO or SQLi. – jankal Dec 04 '15 at 22:31
  • The result of `mysql_query` is a resource that can be used in `mysql_fetch_array` IF there is no error. You should not assume every function call doesn't return an error. – Tristan Dec 04 '15 at 22:32
  • Well, i have one table called projeto (project in english) with these variables: id nome (name in english) descricao (description) limite (deadline, type is DATE) – João Campos Dec 04 '15 at 22:38
  • Have another table called recompensa (reward in english) with these variable: id id_projeto (FK of id in table projeto) descricao (description in english) valor (value in english) – João Campos Dec 04 '15 at 22:41

1 Answers1

0

Your query returns an error, most likely. Try running the query directly against your database using phpMyAdmin or whatever database tool you have for running queries directly.

It looks like "id_projeto" part of the query (at the end) might be causing the error.

UPDATE: Prefix all field names with the table name (or alias) and use a JOIN:

SELECT p.id, p.nome, p.limite, p.descricao, r.valor, r.descricao FROM projeto p LEFT JOIN recompensa r ON p.id = r.id_projeto
AVProgrammer
  • 1,344
  • 2
  • 20
  • 40
  • Well, i have one table called projeto (project in english) with these variables: id nome (name in english) descricao (description) limite (deadline, type is DATE) Have another table called recompensa (reward in english) with these variable: id id_projeto (FK of id in table projeto) descricao (description in english) valor (value in english) I use phpadmin. Now i put $resultado = mysql_query($sql) or die(mysql_error()); to see the error and it says: Every derived table must have its own alias – João Campos Dec 04 '15 at 22:44
  • I've updated my question to reflect the info your comment. – AVProgrammer Dec 04 '15 at 22:50
  • Thanks i tried use INNER JOIN 2 minutes before your answer and runned without errors, so thanks. But one question, whats the difference between INNER JOIN and LEFT JOIN? – João Campos Dec 04 '15 at 22:56
  • And after that i had error on this: $descricao_proj = $dados['projeto.descricao']; $descricao_rec = $dados['recompensa.descricao']; to correct i used in select above projeto.descricao AS v1, but why i need to call to a new variable to work? – João Campos Dec 04 '15 at 22:57
  • LEFT means return all rows from the primary (non-joined) table, even if there are no records in the joined table (recompensa). INNER JOIN means return rows that are in both tables. – AVProgrammer Dec 04 '15 at 22:59
  • Thanks for all your help ;) – João Campos Dec 04 '15 at 23:03
  • The variable names in PHP will be those of the fields WITHOUT the table prefixes, unless you alias a field to explicitly include the table name. So for example: `SELECT projeto.descricao WHERE... -> $proj = $dado['descricao']` OR `SELECT projeto.descricao AS projeto.descricao WHERE... -> $proj = $dado['projeto.descricao']` ...and you're welcome ; ) – AVProgrammer Dec 04 '15 at 23:06