Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on PHP code is not excluding the watched videos.

Post

PHP code is not excluding the watched videos.

+0
−0

Can someone explain why this PHP code is not excluding the watched videos?

The email_id parameter is used to specify the location of the watched videos array. The code should remove the watched_videos array from the user table, but it's not working as expected. Since the watched_videos array is stored as a string, it needs to be converted to an array. Additionally, I want the code to exclude watched videos when the video_id is sent back in an AJAX request.

<?php

// Database credentials

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "refwebsite";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the start and limit parameters
$start = 1;
$limit = 12;
$email_id = "clins@example.com";

// Get the watched_videos array for the user with the given email_id
$sql_watched = "SELECT watched_videos FROM users WHERE email_id='$email_id'";
$result_watched = mysqli_query($conn, $sql_watched);
$row_watched = mysqli_fetch_assoc($result_watched);
$watched_videos = explode(",", $row_watched['watched_videos']);

// Generate a comma-separated list of unique_id values to exclude
$exclude_list = implode(",", $watched_videos);

// Select two random videos from the "less_videos" table, excluding watched videos
$sql_less = "SELECT video_id, description, points FROM less_videos WHERE unique_id NOT IN ('" . implode("', '", $watched_videos) . "') ORDER BY RAND() LIMIT 2";
$result_less = mysqli_query($conn, $sql_less);

// Select ten random videos from the "more_videos" table, excluding watched videos
$sql_more = "SELECT video_id, description, points FROM more_videos WHERE unique_id NOT IN ('" . implode("', '", $watched_videos) . "') ORDER BY RAND() LIMIT 10";
$result_more = mysqli_query($conn, $sql_more);

// Combine the selected videos into a single array
$data = array();
while ($row = mysqli_fetch_assoc($result_less)) {
    $data[] = $row;
}
while ($row = mysqli_fetch_assoc($result_more)) {
    $data[] = $row;
}

// Shuffle the array to randomize the order of videos
shuffle($data);

// Extract the required subset of videos
$data_subset = array_slice($data, $start, $limit);

// Return the data as JSON
echo json_encode($data_subset);

// Close the connection
mysqli_close($conn);
?>

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Output queries (1 comment)
Output queries
Alexei‭ wrote over 1 year ago

Can you print out (echo?) your queries and include them in the post, so that it is clear what is being executed? This should help with identifying the issue.

Security tip: Try using parameterized queries to avoid SQL Injection.