0

I have the following code that has two queries. The first gets an ID that is going to be used on the next query. I this error on the second query:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\b\pprincipal.php on line 132

Here is the first query (For the ID of the user that has data present in that table)

$query2 = mysql_query("SELECT DISTINCT competencias, id_user FROM ce");
$ut = array();
while ($result = mysql_fetch_array($query2)) $ut[] = $result;
   foreach ($ut as $u){
 $id_user=$u[1];
 echo "<li class='has-sub'><a>" .$u[0]. "</a>";
}

And here is the Query that is giving me the error:

$query3 = mysql_query("SELECT id, nome FROM utilizadores WHERE id=$id_user");
$utt = array();
while ($result2 = mysql_fetch_array($query3, $con)) $utt[] = $result2;
foreach ($utt as $u2){
   echo "<ul><li><a>". $u2[0]. "</a></li>";
}

This second query is done right, because i have echoed the array and it was okay. But it gives me the error on the WHILE loop.

dertkw
  • 7,698
  • 5
  • 37
  • 44
user3536315
  • 17
  • 2
  • 7

3 Answers3

5

I think the error could be here:

$query3 = mysql_query("SELECT id, nome FROM utilizadores WHERE id=$id_user");

If the "nome" indeed "nome" and not "name" then try this query:

$query3 = mysql_query("SELECT `id`, `nome` FROM utilizadores WHERE id='$id_user'");

Note that the ` is different than the '.

dx4
  • 78
  • 7
1

you could try this

$query3 = mysql_query("SELECT id, nome FROM utilizadores WHERE id=$id_user");
$utt = array();
while ($result2 = mysql_fetch_array($query3)) $utt[] = $result2;
foreach ($utt as $u2){
   echo "<ul><li><a>". $u2[0]. "</a></li>";
}

you can't use connection handle on the mysql_fetch_array :)

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

btw you should use mysqli or pdo for db handling :)

Dwza
  • 6,358
  • 4
  • 38
  • 66
0
$query3 = mysql_query("SELECT id, nome FROM utilizadores WHERE id=$id_user");
$utt = array();
while ($result2 = mysql_fetch_array($query3, $con)) $utt[] = $result2;
foreach ($utt as $u2){
   echo "<ul><li><a>". $u2[0]. "</a></li>";
}

remove $con in your second while loop

el3ien
  • 5,121
  • 1
  • 15
  • 31