1

I'm building a batabase driven website which uses Javascript to play videos on my website using the YouTube API, but I can't load in the data I have extracted via PHP into the Javascript player.

My table is (Example codes only):

ID | One | Two
---------------
01 | 832 | 943

Here's the PHP code I use to get the data:

<?php
$code_sql = "SELECT * FROM codes WHERE id = '$url_id'";
$code_res = mysqli_query($con, $code_sql);
while($code = mysqli_fetch_assoc($code_res)){

    $code_1 = $code["one"];
    $code_2 = $code["two"];

};
?> 

And Here's the Javascript I'm trying to load it into:

var $vid1 = <?php echo json_encode("$code_1"); ?>;
var $vid2 = <?php echo json_encode("$code_2"); ?>;
var videoIDs = [
    '$vid1',
    '$vid2'
];

When I load the page it gives me a Invalid Paramiters in the video player. Any ideas where I'm going wrong?

4
  • 1
    You're constantly overriding $code_1 and $code_2 in your php while loop, making them hold the last result only Commented Feb 23, 2017 at 9:51
  • And variables dont need to be in doublequotes json_encode($code_1) Commented Feb 23, 2017 at 9:52
  • @baao, cam you please explain what you mena by overriding? And how do I fix that? Commented Feb 23, 2017 at 9:56
  • Possible duplicate of Good practice to return DateTime from PHP to JS Commented Feb 23, 2017 at 11:15

1 Answer 1

0

Remove quotes from json_encode. Also remove single quote inside array

var $vid1 = <?php echo json_encode($code_1); ?>;
var $vid2 = <?php echo json_encode($code_2); ?>;
var videoIDs = [
    $vid1,
    $vid2
];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but when I try this with quotes, it completely blocks out the video player. Any ideas why?
Remove double quotes from outside of php then try. See my edited answer

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.