PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE customers(customerId INT, custmername varchar(10)); INSERT INTO customers VALUES(1,'John'); CREATE TABLE `orders`(order_id int, customerId INT, ordername varchar(10)); INSERT INTO `orders` VALUES(1,1,'order1'),(2,1,'order2'); SELECT * FROM customers c INNER JOIN `orders` o ON c.customerId = o.customerId WHERE c.customerId = 1;
Copy Clear
Copy Format Clear
<?php // Check existence of id parameter before processing further //if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){ if (1==1){ // Include config file //require_once "../includes/config.php"; // Prepare a select statement // This query works, loads the page, but i want to adjust it to orders $sql = "SELECT * FROM customers c INNER JOIN `orders` o ON c.customerId = o.customerId WHERE c.customerId = ?"; if($stmt = mysqli_prepare($mysqli, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "i", $param_id); // Set parameters $param_id = 1 ; //trim($_GET["id"]); // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ $result = mysqli_stmt_get_result($stmt); if(mysqli_num_rows($result) > 0){ /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */ $firstrow = 0; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo $row; IF ($firstrow ==0) { echo $row['custmername']; }; echo $row['ordername']; // Retrieve individual field value // $addValue = $row['#']; }; } else{ // URL doesn't contain valid id parameter. Redirect to error page header("location: src/error.php"); exit(); } } else{ echo "Oops! Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); // Close connection mysqli_close($con); } else{ // URL doesn't contain id parameter. Redirect to error page header("location: src/error.php"); exit(); } ?>
Show:  
Copy Clear