CS174
Chris Pollett
Oct 17, 2022
mysqli_select_db($db, "vehicles");
$query ="SELECT * FROM CAR"; $result = mysqli_query($db, $query); $num_rows = mysqli_num_rows($result); $num_fields = mysqli_num_fields($result); echo $num_fields; for($j = 1; $j <= $num_rows; $j++) { $row = mysqli_fetch_array($result); print_r($row); }
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db"); // note: here we are using the OO interface to mysqli // we'll talk more about OO in PHP later today; for now, pretend its like Java/C++ $stmt = $mysqli->stmt_init(); if ($stmt->prepare("SELECT FNAME, AGE FROM EMPLOYEE WHERE LNAME=? AND AGE > ?")) { $lnames = ["Smith", "Jones", "Pollett"]; $min_age = 25; foreach($lnames as $lname) { $stmt->bind_param("si", $lname, $min_age); //s == string, i == int, d==double $stmt->execute(); $stmt->bind_result($fname, $age); $i = 0; while($stmt->fetch()) { print("The {$i}th person I found with last name $lname and age greater than $min_age was $fname, $age\n"); $i++; } } $stmt->close(); } $mysqli->close();
$dbh = sqlite_open($filename, $write_mode); $result = sqlite_query($query); $row = sqlite_fetch_array($result); sqlite_close($dbh);
$dbh = new SQLite3($filename, $mode); $results=$dbh->query($query); $row = $results->fetchArray(); $dbh->close();
SSN, PNUMBER--> HOURS SSN --> ENAME PNUMBER --> PNAME, PLOCATION.
Which of the following statements is true?
class MyClass { public $field_var1; public $field_var2 = 5; ... const constant1; const constant2; ... function __construct($arg1, $arg2) { //constructor code } function myFun1() { // code } ... }
new operator
. To dereference fields, or invoke instance functions we use the arrow -> operator.
$arg1 =5; $arg2 = 6; $my_class = new MyClass($arg1, $arg2); echo $my_class->field_var1; $my_class->myFun1();
require("MyClass.php"); //or more likely require_once("MyClass.php");
public $my_variable = 0;
function __construct($n=0) { $this->my_variable = $n; }
private $my_field; protected function myMethod() { /* some code*/}
var $my_field;This is less clear than the public syntax so should be avoided.
class Foo{ static $bob=1;} echo "bob: ".Foo::$bob;
class Goo{ const blob=1;}
Goo::blob
define("PI", 22/7); echo PI;
$my_copy = clone $my_obj;
class A{} class B extends A {}
interface MyInterface { function method1($a, $b); }
class C implements MyInterfaceA, MyInterfaceB {}