0

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;
                                }
                            }
3
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by hwoarang69
Featured Replies
  • 1

    > 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. Read More

  • 1

    No, the problem is in the query. Read More

  • 1

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

  • 1
    diafol 3,720   5 Years Ago

    $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 … Read More

  • 1
    diafol 3,720   5 Years Ago

    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 | … Read More

1

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.

0

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)?

1
$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?

0

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>";

Edited by hwoarang69

1

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.

0

why cant i do some thing simple like

$Q ="SELECT * from image ORDER BY RAND() LIMIT 5"; ?
1

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

Votes + Comments
ah ic. thanks alot
0

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>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.