3v4l.org

run code in 300+ PHP versions simultaneously
<?php // -- PULLING IN PRODUCTS TO READ OUT TO USER -- // $stmt = (" SELECT products.ID, products.title, products.category, products.location, products.price, products.negotiable, products.description, products.photo, products.user_id FROM products"); $result = $db->query($stmt); foreach ($result as $row) { $product_id = $row['ID']; $title = htmlspecialchars($row['title'], ENT_QUOTES); // User input, prevent first order XSS $category = $row['category']; // local variable for remainder, ensuring to use htmlspecialchars() for any user input that will be read out to browser } // -- INSERTING ONE PRODUCT TO MAIN PRODUCT PAGE USING PREPARED STATEMENT -- // // Incoming user input from some form // Assign input to local variables // Probably should validate data $null_value = null; // To bind null values... I'm lazy $stmt = $db->prepare(" INSERT INTO products VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param('sssssssss', $null_value, $title, $category, $location, $price, $negotiable, $description, $photo, $user_id); $stmt->execute(); // -- INSERTING MULTIPLE PRODUCTS TO MAIN PRODUCT PAGE USING PREPARED STATEMENT -- // // Incoming user input from some form $null_value = null; // To bind null values... I'm lazy // Assuming user input is from a form // Verify it is a form submission if($_SERVER['REQUEST_METHOD'] === 'POST') { foreach ($_POST['product_array'] as $product) { foreach ($product as $row) { // Validate data types // Assign to to local variables $stmt = $db->prepare(" INSERT INTO products VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param('sssssssss', $null_value, $title, $category, $location, $price, $negotiable, $description, $photo, $user_id); $stmt->execute(); } } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: Undefined variable $db in /in/Obdml on line 20 Fatal error: Uncaught Error: Call to a member function query() on null in /in/Obdml:20 Stack trace: #0 {main} thrown in /in/Obdml on line 20
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Undefined variable $db in /in/Obdml on line 20 Fatal error: Uncaught Error: Call to a member function query() on null in /in/Obdml:20 Stack trace: #0 {main} thrown in /in/Obdml on line 20
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.3.33, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Notice: Undefined variable: db in /in/Obdml on line 20 Fatal error: Uncaught Error: Call to a member function query() on null in /in/Obdml:20 Stack trace: #0 {main} thrown in /in/Obdml on line 20
Process exited with code 255.
Output for 7.3.32, 7.4.26
Fatal error: Uncaught Error: Call to a member function query() on null in /in/Obdml:20 Stack trace: #0 {main} thrown in /in/Obdml on line 20
Process exited with code 255.

preferences:
142.07 ms | 401 KiB | 181 Q