PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table student ( id int primary key auto_increment, student_name varchar(255), email varchar(255) unique key ); create table bill ( id int primary key auto_increment, student_id int, total_balance int, foreign key (student_id) references student(id) );
Copy Clear
Copy Format Clear
<?php $name = "Barry Block"; $email = "barry.b@gmail.com"; $balance = 500; $student_sql = " insert into student (student_name, email) values ( :name, :emailAddress )"; $stmt = $pdo->prepare($student_sql); $stmt->execute([':name'=>$name, ':emailAddress'=>$email]); $bill_sql = " insert into bill (student_id, total_balance) select id, :balance from student where email = :emailAddress"; $stmt = $pdo->prepare($bill_sql); $stmt->execute([':balance'=>$balance, ':emailAddress'=>$email]); $query = "SELECT * from student join bill on bill.student_id = student.id;"; $stmt = $pdo->prepare($query); $stmt->execute(); $students = $stmt->fetch(PDO::FETCH_ASSOC); print_r($students);
Show:  
Copy Clear