1

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>
6
  • 3
    Why are you mixing mysql_* and PDO ? Commented Apr 16, 2014 at 12:44
  • Do you have a PDO connection and a mysql_connect connection? Commented Apr 16, 2014 at 12:44
  • Why are you using PDO and mysql_*? Commented Apr 16, 2014 at 12:44
  • You can remove your 3rd bind by using NOW() which is a MySQL function and will automatically fill the field with the current time. Example: INSERT INTO users (name, email, joined) VALUES (?, ?, NOW()) Commented Apr 16, 2014 at 12:45
  • I thought that was the only way to bind the values that way (?, ?, ?), the insertion on db works fine thou Commented Apr 16, 2014 at 12:45

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

put one function after this line ,

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

you have a error into query .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.