Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\a_upload\index.php on line 114

line 144: while($row = mysql_fetch_assoc($R))

    $count = 0;
    $Q = "SELECT (SELECT image_short_name FROM user) as `image_short_name`"
    .",`image_id`,`user_id`,`image_short_name`,`image`"
    ." FROM `image`"
    ." ORDER BY RAND()"
    ." LIMIT 5";
    $R = mysql_query($Q);

    echo "
<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Upload site: browse through thousands of image!</title>
        <link rel='stylesheet' type='text/css' href='css/top_menu.css' />
        <link rel='stylesheet' type='text/css' href='css/all_css.css' />
    </head>
<body>
        <div id ='bg'>
            <div id='context'>

                <table  width='1000px' height='500px' cellpadding='45px'>                       
                    <tr height='100px'>     
                    ";

                     while($row = mysql_fetch_assoc($R))
                            {
                                //get user_id from image table
                                $user_id_r         = $row['user_id'];
                                $image_short_name_r = $row['image_short_name'];

                                //get user name from user table
                                $queryget = mysql_query("SELECT username FROM user") ;
                                $row2 = mysql_fetch_assoc($queryget);   
                                $user_name_db = $row2['username'];

                                echo "<td width='100px'>";
                                $image_db = $row['image'];
                                $src = "data:image/gif;base64," . $image_db;
                                echo" <a href=\"zoom.php?img={$row['image_id']}\"><img src=\"$src\" width='130px' height='130px' class='image_p' /></a>";
                                echo "<center id='image_name'><a href=\"zoom.php?img={$row['image_id']}\">" . $image_short_name_r . "</a></center>";
                                echo " <center id='image_name2'>by $user_name_db</center>";
                                echo "</td>";
                                $count++;
                                if($count == 5)       //5 rows
                                {
                                    echo "</tr><tr height='100px'>"; // Start a new row at 6 cells
                                    $count = 0;
                                }
                            }

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

This warning usually indicates that the execution of your query failed. So use a tool (phpMyAdmin) to check if your query runs correctly.

i dont know how to user phpmyadmin to check query.
to fix this problem should i

while($row = mysql_fetch_assoc($R))

change this line to while($R)?

No, the problem is in the query.

any idea how to debug this?

$R = mysql_query($Q) or die(mysql_error());

i got this error

"Subquery returns more than 1 row"

Member Avatar for diafol
$Q = "SELECT (SELECT image_short_name FROM user) as `image_short_name`"
.",`image_id`,`user_id`,`image_short_name`,`image`"
." FROM `image`"
." ORDER BY RAND()"
." LIMIT 5";

This is wrong. The content after first SELECT expects fieldnames. The subquery needs to go after FROM.

You'd be better off doing a JOIN. I can't see how your tables match up though. Which fields are related? Is there a joined ID?

i have two tables
user->user_id, username, firstname, lastname
image->image_id, user_id, image, image_full_name, image_short_name..etc
and i am tried to do is to echo random 5 images from image table.

so do you mean something like this?

    $Q = "SELECT (SELECT image_short_name FROM user JOIN image) as `image_short_name`"
    .",`image_id`,`user_id`,`image_short_name`,`image`"
    ." FROM `image`"
    ." ORDER BY RAND()"
    ." LIMIT 5";
    $R = mysql_query($Q);

..than print image

$src = "data:image/gif;base64," . $image_db;
 echo" <a href=\"zoom.php?img={$row['image_id']}\"><img src=\"$src\" width='130px' height='130px' class='image_p' /></a>";
Member Avatar for diafol

OK the joined fields will be user_id:

"SELECT u.username, i.image_id, i.image_short_name, i.image FROM `user` AS u INNER JOIN `image` AS i ON u.user_id = i.user_id WHERE u.user_id = 1 ORDER BY RAND() LIMIT 5"

Obviously change the WHERE clause to suit your needs.
This will output the fields: username | image_id | image_short_name | image
Again change the field list to suit your needs.

why cant i do some thing simple like

$Q ="SELECT * from image ORDER BY RAND() LIMIT 5"; ?
Member Avatar for diafol

You can - but you're the one that included user into the mix. I was just following your lead.

commented: ah ic. thanks alot +2

go it to work. thanks guys

    $count = 0;
    $Q ="SELECT * from image ORDER BY RAND() LIMIT 10"; 
    $R = mysql_query($Q);

    echo "
<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Upload site: browse through thousands of image!</title>
        <link rel='stylesheet' type='text/css' href='css/top_menu.css' />
        <link rel='stylesheet' type='text/css' href='css/all_css.css' />
    </head>
<body>
        <div id ='bg4'>
            <div id='context4'>

                    <form class='form7' method='post' action='search.php'>
                    <input type='text' name='search' class='field2'/>  
                    <input type='submit' name='search_button' class='button' value='Search' />
                </form>
                <table  width='1000px' height='500px' cellpadding='45px'>                       
                    <tr height='100px'>     
                    ";

                     while($row = mysql_fetch_assoc($R))
                            {
                                //get user_id from image table
                                $user_id_r         = $row['user_id'];
                                $image_short_name_r = $row['image_short_name'];

                                //get user name from user table
                                $queryget = mysql_query("SELECT username FROM user") ;
                                $row2 = mysql_fetch_assoc($queryget);   
                                $user_name_db = $row2['username'];

                                echo "<td width='100px'>";
                                $image_db = $row['image'];
                                $src = "data:image/gif;base64," . $image_db;
                                echo" <a href=\"zoom.php?img={$row['image_id']}\"><img src=\"$src\" width='130px' height='130px' class='image_p' /></a><br/>";
                                echo "<a href=\"zoom.php?img={$row['image_id']}\"><b class='bold2'>" . $image_short_name_r . "</b></a>";
                                echo " <center id='image_name2'>by $user_name_db</center>";
                                echo "</td>";
                                $count++;
                                if($count == 5)       //5 rows
                                {
                                    echo "</tr><tr height='100px'>"; // Start a new row at 6 cells
                                    $count = 0;
                                }
                            }
            echo"
             </tr>
                </table>
            </div>
        </div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.