PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE users (`user_name` varchar(25), `age` smallint, `previous_level` varchar(20), `current_level` varchar(20)) ; INSERT INTO users (user_name, age, previous_level, current_level) VALUES ('User1', 25, 'N/A', 'blue'), ('User2', 30, 'blue', 'bronze'), ('User3', 28, 'bronze', 'silver'), ('User4', 35, 'silver', 'gold'), ('User5', 22, 'gold', 'diamond'), ('User6', 27, 'N/A', 'blue'), ('User7', 33, 'blue', 'bronze'), ('User8', 29, 'bronze', 'silver'), ('User9', 26, 'silver', 'gold'), ('User10', 31, 'gold', 'diamond'), ('User11', 24, 'N/A', 'blue'), ('User12', 32, 'blue', 'bronze'), ('User13', 34, 'bronze', 'silver'), ('User14', 23, 'silver', 'gold'), ('User15', 36, 'gold', 'diamond'), ('User16', 29, 'N/A', 'blue'), ('User17', 27, 'blue', 'bronze'), ('User18', 30, 'bronze', 'silver'), ('User19', 26, 'silver', 'gold'), ('User20', 25, 'gold', 'diamond'); Select * from users;
Copy Clear
Copy Format Clear
<?php $user_name = 'Floris'; $user_pass = 'abcd'; $previousLevels = [ 'bronze' => 'blue', 'silver' => 'blue, bronze', 'gold' => 'blue, bronze, silver', 'diamond' => 'blue, bronze, silver, gold', ]; $curLevel = 'gold'; $previousLevelString = $previousLevels[$curLevel]; // Split the previous levels string into an array $previousLevelsArray = explode(', ', $previousLevelString); echo "previousLevelsArray: " . $previousLevelsArray . "\n"; // Generate placeholders for the IN clause $placeholders = implode(', ', array_fill(0, count($previousLevelsArray), '?')); // Prepare the SQL query with the dynamically generated IN clause $query = "SELECT * FROM users WHERE current_level = ? AND previous_level IN ($placeholders)"; $q = mysqli_prepare($mysqli, $query); // Bind parameters $q->bind_param(str_repeat('s', count($previousLevelsArray) + 1), $curLevel, ...$previousLevelsArray); $q->execute(); $result = $q->get_result(); // Get the result set while ($row = $result->fetch_assoc()) { print_r($row); // Output each row }
Show:  
Copy Clear