real_query('CREATE TABLE main (mainid INT NOT NULL, txtval VARCHAR(50), PRIMARY KEY (mainid))'); $stmt = $con->prepare('INSERT INTO main VALUES(?,?)'); $stmt->bind_param('is', $mainid, $txtval); $mainid = 1; $txtval = 'A'; $stmt->execute(); $mainid = 2; $txtval = 'B'; $stmt->execute(); $mainid = 3; $txtval = 'B'; $stmt->execute(); $stmt->close(); $con->real_query('CREATE TABLE sub (subid INT NOT NULL, mainid INT NOT NULL, txtval VARCHAR(50), PRIMARY KEY (subid))'); $stmt = $con->prepare('INSERT INTO sub VALUES(?,?,?)'); $stmt->bind_param('iis', $subid, $mainid, $txtval); $subid = 1; $mainid = 1; $txtval = 'A1'; $stmt->execute(); $subid = 2; $mainid = 1; $txtval = 'A2'; $stmt->execute(); $subid = 3; $mainid = 2; $txtval = 'B1'; $stmt->execute(); $subid = 4; $mainid = 2; $txtval = 'B2'; $stmt->execute(); $subid = 5; $mainid = 3; $txtval = 'C1'; $stmt->execute(); $subid = 6; $mainid = 3; $txtval = 'C2'; $stmt->execute(); $stmt->close(); $con->close(); } // remove test data function teardown() { $con = new mysqli('localhost', 'root', '', 'Test'); $con->real_query('DROP TABLE main'); $con->real_query('DROP TABLE sub'); $con->close(); } setup(); $con = new mysqli('localhost', 'root', '', 'Test'); $stmt = $con->prepare('SELECT main.mainid,main.txtval,sub.subid,sub.txtval FROM main JOIN sub ON main.mainid=sub.mainid WHERE ? <= main.mainid AND main.mainid <= ?'); $stmt->bind_param('ii', $start, $end); $start = 1; $end = 2; $stmt->execute(); $stmt->store_result(); $stmt->bind_result($mainid, $maintxt, $subid, $subtxt); $lastmainid = -1; echo "\r\n"; echo "
  • $maintxt
  • \r\n"; echo "\r\n"; echo "\r\n"; $stmt->close(); $con->close(); teardown(); ?>