mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetc

2018-12-31 00:42发布

I am trying to select data from a MySQL table, but I get one of the following error messages:

mysql_fetch_array() expects parameter 1 to be resource, boolean given

or

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

or

Call to a member function fetch_array() on boolean / non-object

This is my code:

$username = $_POST['username'];
$password = $_POST['password'];

$result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');

while($row = mysql_fetch_array($result)) {
    echo $row['FirstName'];
}

The same applies to code like

$result = mysqli_query($mysqli, 'SELECT ...');
// mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
while( $row=mysqli_fetch_array($result) ) {
    ...

and

$result = $mysqli->query($mysqli, 'SELECT ...');
// Call to a member function fetch_assoc() on a non-object
while( $row=$result->fetch_assoc($result) ) {
    ...

and

$result = $pdo->query('SELECT ...', PDO::FETCH_ASSOC);
// Invalid argument supplied for foreach()
foreach( $result as $row ) {
    ...

and

$stmt = $mysqli->prepare('SELECT ...');
// Call to a member function bind_param() on a non-object
$stmt->bind_param(...);

and

$stmt = $pdo->prepare('SELECT ...');
// Call to a member function bindParam() on a non-object
$stmt->bindParam(...);

标签: php mysql
30条回答
有味是清欢
2楼-- · 2018-12-31 01:22

There might be two reasons:

  1. Have you opened the connection to the database prior to calling mysql_query function? I don't see that in your code. Use mysql_connect before making the query. See php.net/manual/en/function.mysql-connect.php

  2. The variable $username is used inside a single quote string, so its value will not be evaluated inside the query. The query will definitely fail.

Thirdly, the structure of query is prone to SQL injection. You may use prepared statements to avoid this security threat.

查看更多
浅入江南
3楼-- · 2018-12-31 01:22

Try this code it work fine

assign the post variable to the variable

   $username = $_POST['uname'];

   $password = $_POST['pass'];

  $result = mysql_query('SELECT * FROM userData WHERE UserName LIKE $username');

if(!empty($result)){

    while($row = mysql_fetch_array($result)){
        echo $row['FirstName'];
     }
}
查看更多
骚的不知所云
4楼-- · 2018-12-31 01:23

Put quotes around $username. String values, as opposed to numeric values, must be enclosed in quotes.

$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");

Also, there is no point in using the LIKE condition if you're not using wildcards: if you need an exact match use = instead of LIKE.

查看更多
不流泪的眼
5楼-- · 2018-12-31 01:24

Error occurred here was due to the use of single quotes ('). You can put your query like this:

mysql_query("
SELECT * FROM Users 
WHERE UserName 
LIKE '".mysql_real_escape_string ($username)."'
");

It's using mysql_real_escape_string for prevention of SQL injection. Though we should use MySQLi or PDO_MYSQL extension for upgraded version of PHP (PHP 5.5.0 and later), but for older versions mysql_real_escape_string will do the trick.

查看更多
查无此人
6楼-- · 2018-12-31 01:24

First, check your connection to the database. Is it connected successfully or not?

If it's done, then after that I have written this code, and it works well:

if (isset($_GET['q1mrks']) && isset($_GET['marks']) && isset($_GET['qt1'])) {
    $Q1mrks = $_GET['q1mrks'];
    $marks = $_GET['marks'];
    $qt1 = $_GET['qt1'];

    $qtype_qry = mysql_query("
        SELECT *
        FROM s_questiontypes
        WHERE quetype_id = '$qt1'
    ");
    $row = mysql_fetch_assoc($qtype_qry);
    $qcode = $row['quetype_code'];

    $sq_qry = "
        SELECT *
        FROM s_question
        WHERE quetype_code = '$qcode'
        ORDER BY RAND() LIMIT $Q1mrks
    ";
    $sq_qry = mysql_query("
        SELECT *
        FROM s_question
        WHERE quetype_code = '$qcode'
        LIMIT $Q1mrks
    ");
    while ($qrow = mysql_fetch_array($sq_qry)) {
        $qm = $qrow['marks'] . "<br />";
        $total += $qm . "<br />";
    }
    echo $total . "/" . $marks;
}
查看更多
栀子花@的思念
7楼-- · 2018-12-31 01:25

As scompt.com explained, the query might fail. Use this code the get the error of the query or the correct result:

$username = $_POST['username'];
$password = $_POST['password'];

$result = mysql_query("
SELECT * FROM Users 
WHERE UserName LIKE '".mysql_real_escape_string($username)."'
");

if($result)
{
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
} else {
    echo 'Invalid query: ' . mysql_error() . "\n";
    echo 'Whole query: ' . $query; 
}

See the documentation for mysql_query() for further information.

The actual error was the single quotes so that the variable $username was not parsed. But you should really use mysql_real_escape_string($username) to avoid SQL injections.

查看更多
登录 后发表回答