0

I am confused as to why the first result which is 0 is not being iterated. Could someone please explain why and show me what needs to be done to show [0]'s result?

Here is my Array:
array(3) { [0]=> string(3) "390" [1]=> string(3) "377" [2]=> string(3) "382" } 

Notice how [0] result is not shown via the foreach. The last two which are [1] and [2] show up fine.

You can see the results of this here: http://www.rotaryswing.com/swingviewer/videos.php

<?php 
//iterate through video IDS in our DB
foreach ($pieces as $key => $v) {

$sql ="SELECT id, video_name, link, phase FROM videos WHERE id=?";
    if ($stmt = $mysqli->prepare($sql)) {
        $stmt->bind_param("i", $v);

        if ($stmt->execute()) {
            $stmt->bind_result($id, $vid_name, $vid_link, $phase);
            while ($stmt->fetch()) {
                echo "<a style=\"font-size: 14px;\" href='http://www.rotaryswing.com/golf- instruction/video/rst-index.php?cat=$phase&subcat=Rotary%20Swing%20Tour&video=$id&id=$vid_link&name=$vid_name' target=\"blank\">$vid_name</a><br>";
            }
        }
        else {
            trigger_error("SQL query failed: " . $stmt->error, E_USER_ERROR);
        }
    }
}
?>

When I echo just the pieces it echos fine.

<?php echo $pieces[0] . "<br/>";?>

<?php echo $pieces[1] . "<br/>";?>

<?php echo $pieces[2] . "<br/>";?>
6
  • 1
    I don't see anything wrong in particular here; are you sure there's a database record for id 390? Commented Aug 27, 2013 at 23:13
  • 2
    dont put the query in a loop, use one query then loop the results Commented Aug 27, 2013 at 23:13
  • @Jack yes there is. I think the 0 is returning false so i would have to check that. I think. I don't know Commented Aug 27, 2013 at 23:14
  • Are you sure the SQL statement returns something? Commented Aug 27, 2013 at 23:21
  • var_dump($pieces); would tell you that for certain. Commented Aug 27, 2013 at 23:40

2 Answers 2

1

You could use a WHERE IN with your array:

# create the correct amount of ?
$placeholder = implode(', ', array_fill(0, count($pieces), '?'));
$sql ="SELECT id, video_name, link, phase FROM videos WHERE id IN ({$placeholder})";
if ($stmt = $mysqli->prepare($sql))
{
    # add at the begin the type of your array the field types
    array_unshift($pieces, implode('', array_fill(0, count($pieces), 'i')));
    # bind the field type and each value
    call_user_func_array(array($stmt, 'bind_param'), $pieces);
    if ($stmt->execute())
    {
        $stmt->bind_result($id, $vid_name, $vid_link, $phase);
        while ($stmt->fetch())
        {
?>
<a style="font-size: 14px;" href="http://www.rotaryswing.com/golf-instruction/video/rst-index.php?cat=<?php echo $phase; ?>&subcat=Rotary%20Swing%20Tour&video=<?php echo $id; ?>&id=<?php echo $vid_link; ?>&name=<?php echo $vid_name; ?>" target="blank"><?php echo $vid_name; ?></a><br>
<?php
        }
    }
    else
    {
        trigger_error("SQL query failed: " . $stmt->error, E_USER_ERROR);
    }
}

Using call_user_func_array(array($stmt, 'bind_param'), $pieces); we bind each field type and parameters to the bind_param.

Using $placeholder = implode(', ', array_fill(0, count($pieces), '?')); we create string with the correct amount of placeholders that $pieces have, so if $pieces have 4 ids it will create a string like this ?, ?, ?, ? that we then append inside the query at IN ({$placeholder})

Using array_unshift($pieces, implode('', array_fill(0, count($pieces), 'i'))); we create and append all the types as the first element of the array.

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

Comments

0

Put query outside of the loop as the commenter Dagon said.

Use a query like this:

SELECT id, video_name, link, phase FROM videos WHERE id IN (?)

Then you can send a single query and get all your data at once.

Use $v = implode(',',$pieces); for the contents of IN ().

Using implode should never result in lost array elements.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.