0

I seriously have no clue why this isn't working. What I am trying to do is simply get all the emails in my database and then echo them, but no matter what I try mysql_fetch_array is not working. It isn't the SQL query that isn't working, because I have added a if statement that dies if it isn't working, not only that, but I have gone on PHPMyAdmin, and done the exact same query, and it works.

This is my code

<?php
$dbc = mysql_connect('localhost', 'root', ''); //host, username, pass
$db = mysql_select_db('habbo', $dbc);

if(!$dbc || !$db)
    die("Unknown Error.");

$sql = "SELECT `email` FROM `logininfo` WHERE `id` = '1'";
mysql_query($sql);

if(!$sql)
    die(mysql_error());

while($row = mysql_fetch_array($sql))
    echo $row['email'] . " ";

?>

So, I have no idea what is going on, both of my error traps don't come up, I just get an error when I go to the page saying this

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given on line 14

I assume that it means that the query is broken, but the query isn't, because like I said earlier, I have set up error traps, and tried the query in PHPMyAdmin, so it might be something wrong with XAMPP, or something. I have restarted Apache, and MySQL, but still I get the same error.

4 Answers4

1

You're trying to fetch the result from the sql text, not the result of the query;

$sql = "SELECT `email` FROM `logininfo` WHERE `id` = '1'";
$res = mysql_query($sql);

if(!$res)
    die(mysql_error());

while($row = mysql_fetch_array($res))
    echo $row['email'] . " ";
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

$sql in fact IS a string, and not the resource you would need to access the result from MySQL.

You need to store the result of mysql_query(), check if THAT is false and call mysql_error(), and otherwise pass it to mysql_fetch_array().

Sven
  • 69,403
  • 10
  • 107
  • 109
0

$sql should be a mysql_query(). Possibly also add MYSQL_ASSOC

Something like:

$result = mysql_query("SOME QUERY");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo $row['email'] . " ";
}
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
0

You're not setting the result of the mysql_query($sql) function in to a variable. Therefore you're using a string for the latter part of the code.

Try this:

<?php
$dbc = mysql_connect('localhost', 'root', ''); //host, username, pass
$db = mysql_select_db('habbo', $dbc);

if(!$dbc || !$db)
    die("Unknown Error.");

$sql = "SELECT `email` FROM `logininfo` WHERE `id` = '1'";
$result = mysql_query($sql);

if(!$result)
    die(mysql_error());

while($row = mysql_fetch_array($result))
    echo $row['email'] . " ";
?>
Jack B
  • 547
  • 4
  • 22