Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question
add comment

marked as duplicate by John Conde, Joe, sasha.sochka, smerny, DwB Jul 29 at 12:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

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'] . " ";
share|improve this answer
 
Oh, thanks, I forgot to do that, lol. What a simple mistake, thanks for clearing it though. –  Business Business Jul 28 at 18:15
add comment

$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().

share|improve this answer
add comment

$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'] . " ";
}
share|improve this answer
add comment

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'] . " ";
?>
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.