2

When i try to run this code i get the error:

"Warning: mysql_fetch_array() expects parameter 1 to be resource, string given"

i've been trying to debug it but i cant seem to.

it's connecting to a table with 6 fields:

id (int) | name (varchar) | image (BLOB) | description (text) | url (text) | keywords (text)

Could anyone try to fix this?!?!?!

the function is written below....

function get_images()
{
    $limit = 5;
    $count = 0;

    $row = mysql_fetch_array("SELECT * FROM images");
    echo "<table border='1'>";
    while($row)
    {
    $img = $row['name'];
    if ($count < $limit)
        {
            if($count == 0)
                {
                echo "<tr>";
                }
            echo "<td>$img</td>";
        }
    else
        {
        $count = 0;
        echo "</tr><tr><td>$img</td>";
        }
    $count++;
    }
    echo "</td></table>";
}
3
  • It looks like you're new to PHP. Please consider learning the modern PDO database functions instead of the old and busted "mysql" functions. Commented Mar 15, 2011 at 21:36
  • Are you surprised that someone is asking for help with a mysql function?! Are you advertising?! I don't understand the title. May I refer you to this website: english.stackexchange.com ? Commented Mar 15, 2011 at 21:41
  • @Alin Purcaru You clearly understood the problem, you clearly understood that Rahulpwns has a problem with that function, I don't think this is the right place to point that out, instead use your reputation to edit the title. Commented Mar 15, 2011 at 21:52

6 Answers 6

4

You're using it wrong:

$result = mysql_query("SELECT * from images");
$row = mysql_fetch_array($result);

Then do what you need...

Sign up to request clarification or add additional context in comments.

Comments

1

mysql_fetch_array expects parameter 1 to be a valid mysql query object.

So, call mysql_query first.

$query = mysql_query("SELECT * FROM images");
$row = mysql_fetch_array($query);

Comments

1

Where is the connection of DB? You should use like this;

$result = msql_query('SELECT id, name FROM people', $con);
    if (!$result) {
die('Query execution problem: ' . msql_error());
}

while ($row = msql_fetch_array($result, MSQL_ASSOC)) {
    echo $row['id'] . ': ' . $row['name'] . "\n";
}

Comments

0

You have passed wrong parameter to the mysql_fetch_array()

function get_images()
{
    $limit = 5;
    $count = 0;
    $resource = mysql_query("SELECT * FROM images"$res);
    $row = mysql_fetch_array($resource );
    echo "<table border='1'>";
    while($row)
    {

        $img = $row['name'];
        if ($count < $limit)
            {
                if($count == 0)
                    {
                    echo "<tr>";
                    }
                echo "<td>$img</td>";
            }
        else
            {
            $count = 0;
            echo "</tr><tr><td>$img</td>";
            }
        $count++;
        }
        echo "</td></table>";
    }

Comments

0

Your error is here:

$row = mysql_fetch_array("SELECT * FROM images");

You need to do this:

$sql = "SELECT * FROM images"; 
$res = mysql_query ($sql); 
$row = mysql_fetch_array($res);

The problem is that you are trying to send the query in an incorrect way, first you need to execute the query with mysql_query which returns a "resource", then use that resource and extract the information with mysql_fetch_array.

hope this helps :)

Comments

0
$query = mysql_query("SELECT * FROM images");
while ($row = mysql_fetch_array($query)){
 do_something();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.