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>";
}
Rahulpwns
  • 23
  • 2
  • It looks like you're new to PHP. Please [consider learning the modern PDO database functions](http://php.net/manual/en/book.pdo.php) instead of the old and busted "mysql" functions. – Charles Mar 15 '11 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: http://english.stackexchange.com/ ? – Alin Purcaru Mar 15 '11 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. – Ozmah Mar 15 '11 at 21:52

6 Answers6

4

You're using it wrong:

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

Then do what you need...

jpea
  • 3,114
  • 3
  • 26
  • 26
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);
vicTROLLA
  • 1,554
  • 12
  • 15
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";
}
NevzatR
  • 245
  • 1
  • 3
  • 14
0
$query = mysql_query("SELECT * FROM images");
while ($row = mysql_fetch_array($query)){
 do_something();
}
Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
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>";
    }
Piotr Salaciak
  • 1,653
  • 1
  • 15
  • 28
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 :)

Ozmah
  • 140
  • 8