Open saykaren opened 2 years ago
MySQLi example
$connection = new mysqli($dbServer, $dbUserName, $dbPassword, $dbName);
$statementObj->bind_result($firstName, $lastName, $penName); $statementObj->store_results();
if ($statementObj->num_rows > 0) { while($statementObj->fetch()) { echo $firstName." ".$lastName." (".$penName.")".PHP_EOL; } }
$statementObj-close(); $connection->close();
PDO connection
$connection = new PDO('mysql:host='.$dbServer.';dbname='.$dbName, $dbUserName, $dbPassword);
$query = "SELECT first_name, last_name, pen_name FROM Authors ORDER BY first_name"; $resultObj = $connection->query($query);
if($resultObj->rowCount() > 0 ) { foreach($resultObj as $singleRowFromQuery) { echo "Author: ".$singleRowFromQuery['first_name'].PHP_EOL; } }
$resultObj = null; $connection - null;
$tempFirstName = 'Lucy Maud';
$query = "Select first_name, last_name, pen_name FROM Authors WHERE first_name = ?"; $statementObj = $connection->prepare($query);
$statementObj->bind_param("s", $tempFirstName); $statementObj-execute();
example of more $statementObj->bind_param("sdi", $stringVar, $floatVar, $intVar);