Hi Guys.
I am currently examining a script from a book I am reading.
It is quite a big script, but I only need help with something small. So there is no need to send you the entire script.
There are only 3 documents (in the application I am studying) that are relevant to my question.
It is probably easiest if I just show you these 3 documents below (to help you help me understand things better):
config.php:
<?php
define( "DB_DSN", "mysql:dbname=mydatabase" );
define( "DB_USERNAME", "root" );
define( "DB_PASSWORD", "thepassword" );
define( "PAGE_SIZE", 5 );
define( "TBL_MEMBERS", "members" );
define( "TBL_ACCESS_LOG", "accesslog" );
?>
This file simply contains constants used to connect to the database and any tables.
DataObject.class.php:
<?php
require_once "config.php";
abstract class DataObject {
protected $data = array();
public function __construct( $data ) {
foreach ( $data as $key => $value ) {
if ( array_key_exists( $key, $this-> data ) ) $this-> data[$key] = $value;
}
}
public function getValue( $field ) {
if ( array_key_exists( $field, $this-> data ) ) {
return $this-> data[$field];
} else {
die( "Field not found" );
}
}
public function getValueEncoded( $field ) {
return htmlspecialchars( $this-> getValue( $field ) );
}
protected static function connect() {
try {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn-> setAttribute( PDO::ATTR_PERSISTENT, true );
$conn-> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
die( "Connection failed: " . $e-> getMessage() );
}
return $conn;
}
protected static function disconnect( $conn ) {
$conn = "";
}
}
?>
This file just contains an abstract class.
The class includes a method which connects to the database and a few other methods and a contructor function to set values etc.
Member.class.php:
(the following document extends the one above)
<?php
require_once "DataObject.class.php";
class Member extends DataObject {
protected $data = array(
"id" => "",
"username" => "",
"password" => "",
"firstName" => "",
"lastName" => "",
"joinDate" => "",
"gender" => "",
"favoriteGenre" => "",
"emailAddress" => "",
"otherInterests" => ""
);
private $_genres = array(
"crime" => "Crime",
"horror" => "Horror",
"thriller" => "Thriller",
"romance" => "Romance",
"sciFi" => "Sci-Fi",
"adventure" => "Adventure",
"nonFiction" => "Non-Fiction"
);
public static function getMembers( $startRow, $numRows, $order ) {
$conn = parent::connect();
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY
$order LIMIT :startRow, :numRows";
try {
$st = $conn-> prepare( $sql );
$st-> bindValue( ":startRow", $startRow, PDO::PARAM_INT );
$st-> bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st-> execute();
$members = array();
foreach ( $st-> fetchAll() as $row ) {
$members[] = new Member( $row );
}
$st = $conn-> query( "SELECT found_rows() AS totalRows" );
$row = $st-> fetch();
parent::disconnect( $conn );
return array( $members, $row["totalRows"] );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e-> getMessage() );
}
}
public static function getMember( $id ) {
$conn = parent::connect();
$sql = "SELECT * FROM " . TBL_MEMBERS . " WHERE id = :id";
try {
$st = $conn-> prepare( $sql );
$st-> bindValue( ":id", $id, PDO::PARAM_INT );
$st-> execute();
$row = $st-> fetch();
parent::disconnect( $conn );
if ( $row ) return new Member( $row );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e-> getMessage() );
}
}
public function getGenderString() {
return ( $this-> data["gender"] == "f" ) ? "Female" : "Male";
}
public function getFavoriteGenreString() {
return ( $this->_genres[$this-> data["favoriteGenre"]] );
}
}
?>
Now this is the document which is causing my confusion.
And to be more specific, it all come down to this little snippet of code:
$st = $conn-> query( "SELECT found_rows() AS totalRows" );
..... Now to me, when I read the above code documents and try to make sense of it all, I understand 99.9 percent of it.
But the thing that makes no sense to me is how:
$st = $conn-> query( "SELECT found_rows() AS totalRows" );
... is able to store the number of found rows? How can it do this when (in the above snippet) the value of $st has been changed and now just merely connects to the database and doesn't target any specific table? How can the value of total rows be retrieved when no table is even referenced in the above snippet?
Paul.