0

Okay. I have these two tables I've been struggling with for the entire day. I end up getting something that works in some cases, and others it doesn't. I will therefore explain how the relation of those two tables shall be.

Explanation of the two tables:

objects: This tables job is to hold a lot objects, these objects contain info about it. It contains a name, an image source etc.

House_room1: This table is used for all users in my application. Every user is represented with a user_id.

ALL objects which has the same user_id as the user logged in shall be printed out. What I mean here is that all the objects that the user has, shall be printed out. So having these two table examples:

Objects (table)

Object

house_room1 (table)

house_room1

So IF user_id=1 PRINT everything from house_room1 with that particular user_id. However theres a hook!

These things should only be printed out 1 at a time. and therefore there should be an iterator going through all the objects that the user has.

This is the query I have made so far:

$stmt->prepare('SELECT ref_id, x, y, z, src, rotation, link, div_id 
                FROM house_room1
                INNER JOIN objects
                ON house_room1.object_id=objects.object_id 
                WHERE house_room1.object_id= ? AND house_room1.user_id = ?');
$stmt->bind_param('ii', $i, $GLOBALS['userid_v']); // $i is the iterator

However this doesn't do what I expected and its a little hard to predict whats going on. Hope to find some help in here, thanks in advance!

EDIT:

$stmt = $mysqli->stmt_init();
$stmt->prepare('SELECT ref_id, x, y, z, src, rotation, link, div_id,objects.object_id 
                                            FROM house_room1
                                            INNER JOIN objects
                                            ON house_room1.object_id=objects.object_id 
                                            WHERE house_room1.user_id = ?');
$stmt->bind_param('i', $GLOBALS['userid_v']);

if ($stmt->execute()) {
        while($row = $stmt->fetch()) {
                if ($row['link'] != "") { 
                    echo '<a href="' . $row['link'] . '"> ';
                }
                if ($row['div_id'] != "") { 
                    echo '<a href="#" onClick="' . $row['div_id'] . '"> '; 
                }
                echo '<img src="' . $row['src'] . $row['rotation'] .'.png" id="object' . $row['ref_id'] . '" class="object' . $row['ref_id'] . '" rel="'.$row['rotation'].'"  alt="'.$row['ref_id'].'" style="position:absolute; left:' . $row['x'] . 'px; top:' . $row['y'] . 'px; z-index:'. $row['z'] . ';">'; if ($row['x'] != 0) { echo'</a>'; }
        }
} else {
        echo 'Something went terrible wrong' . $mysqli->error;
}

This prints the image but it doesn't have any values on $row['src'] or either of the $row spots?

13
  • what do you mean by "printed out 1 at a time"? What is triggering that? One on every user click? One on every row of a table?
    – A.O.
    Apr 16, 2014 at 0:27
  • 1
    just left join on object_id and use the user_id as you have in the where clause. you dont need the limitation on object_id.
    – Rufinus
    Apr 16, 2014 at 0:27
  • @A.O. One on every row of a table. Thanks for asking. So it takes out one object at a time.
    – owwyess
    Apr 16, 2014 at 0:33
  • @Rufinus I tried what you said, but nothing get printed out on user_id=2. hmm
    – owwyess
    Apr 16, 2014 at 0:34
  • 1
    @Ohgodwhy uh, do I have to SELECT a column in order to use it in the same query? I tried so, but it made no difference to me? Or else I did it wrong?
    – owwyess
    Apr 16, 2014 at 0:39

1 Answer 1

1

What you'd normally do is return all the results for that user (instead of by object ID), and then use ->fetch_assoc() to return one row (1 object) at a time, and loop over that. fetch->() will return false once there are no more results, so you can use it as the exit condition for a loop. It's also possible that you're not execute-ing the query. And you shouldn't prepare the query inside a loop since that erases the efficiency of the prepared query in the first place.

If you wanted to print out the objects one at a time you should be doing something like

$stmt->prepare('SELECT ref_id, x, y, z, src, rotation, link, div_id,objects.object_id 
            FROM house_room1
            INNER JOIN objects
            ON house_room1.object_id=objects.object_id 
            WHERE house_room1.user_id = ?');
$stmt->bind_param($GLOBALS['userid_v']);
$stmt->execute();
$stmt->bind_result($ref_id,$x,$y,$z,$src,$rotation,$link,$div_id,$objectid);
while($stmt->fetch()) {
    // each of the variables in bind_result now reference the current row
}
7
  • Hello thanks for this code, I should really consider doing it this way, I just want the query to work how it is now at least, and work my way to a better approach later. Thanks!
    – owwyess
    Apr 16, 2014 at 0:49
  • 1
    but it may not work properly at all depending on how you implement the iterator, that's the problem Apr 16, 2014 at 0:51
  • The iterator is in a for loop: for ($i = 0; $i < $count_total_objects; $i++) { Maybe I should try your approach while I'm at it anyway. Will take some time for me to get into though
    – owwyess
    Apr 16, 2014 at 0:52
  • 1
    see, user 2 only has an object id = 1, and object id = 6, so if they have a total count of 2 objects, the iterator will never reach 6. Also, user 1 has 6 objects of id 4, will the iterator print all of them? Apr 16, 2014 at 0:54
  • 1
    yeah, exactly. note that it has to be in the SELECT statement, so you wouldn't get name unless you add it, but $row['src'] will work. Apr 16, 2014 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.