문제

I have this piece of code:

if (isset($_POST['username']) && isset($_POST['email'])) {

$query = $pdo->prepare('INSERT INTO users (name, email, joined) VALUES (?, ?, ?)') or die ('Error.');
$query->bindValue(1, $_POST['username']);
$query->bindValue(2, $_POST['email']);
$query->bindValue(3, time());

$query->execute();

$sql = mysql_query('SELECT * FROM users;');

while ($row = mysql_fetch_array($sql)) {

    echo ('<div style="font-weight: bold;">' .$row['name']. '</div>');
    echo ($row['email']);
    echo ('<br>');
    echo ('Posted: ');
    echo date('F j, Y, g:i a', strtotime($row['joined']));

}

}

which it accuses this error:

mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\WebServer\htdocs\newsSite\register.php on line 21

and on another file (below), it works fine, but i don't see the difference between them:

<body>
<?php

$query = mysql_query('SELECT * FROM posts;');

while ($row = mysql_fetch_array($query)) {

    echo ('<div style="font-weight: bold;">' .$row['title']. '</div>');
    echo ($row['post']);
    echo ('<br>');
    echo ('Posted: ');
    echo date('F j, Y, g:i a', strtotime($row['date']));

}

?>
</body>
도움이 되었습니까?

해결책

Try like this, this my code snippet I have used long before,

    // PDO
$pdo = new PDO('mysql:dbname=test;host=127.0.0.1', 'example', 'example');

print '<h3>PDO: simple select</h3>';
foreach($pdo->query( 'SELECT * FROM users;' ) as $row)
{
    //Fetching data
}

다른 팁

The problem is (probably) that your query returns false (hence "boolean"), due to some error in the query string or logic. Try "SELECT 1" instead of your query string and if it works well, you'll know for sure that the difference (the cause) must be in the query.

put one function after this line ,

$sql = mysql_query('SELECT * FROM users;');
echo mysql_error();

you have a error into query .

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top