host=$dbhost; $this->user=$dbuser; $this->pass=$dbpass; $this->name=$dbname; } /* Okay, here is a method to setup the connection. */ function setup_connection() { /* Create connection and store it in $con */ $time=$_SERVER['REQUEST_TIME']; echo "Hello $time $dbuser
"; echo "Calling mysqli_connect
"; /* Create a new object of type mysqli, its * constructor wants several parameters when * the object is created, the server, the user, * and the user password. If you look at the * documentation there are a lot more * parameters it can take during initialization * but I just did the basic three. */ $con=new mysqli($this->host, $this->user, $this->pass); echo "Called mysqli_connect
"; /* Check the field connect_errno of the object * to see if the database returned an error, if * it did error out of the function/kill the * script. */ if ($con->connect_errno) die("Connection error: " . $con->connect_errno ." ". $con->connect_error); echo "Successfully called mysqli_connect
"; /* Select known database "mysql" */ /* The following sends a message to the mysqli * object to select a new database, which is * identified as dbname. The message returns * success/failure. More information on it can * be found on the mysqli API page. I used the * one found at w3schools. If error, end * function/kill the script. */ if (!$con->select_db($this->name)) die("Selection error: " . $con->error); echo "Successfully called mysqli_select_db $dbname
"; $this->con = $con; return $con; } /* Okay, here is a method called issue_query. It * accepts an SQL query as a string, */ function issue_query($sql) { /* If connection is lost? Reissue connection */ if (!$this->con) $this->setup_connection();//() /* Issue query on Database using user table - * Store in result */ /* Send the message query to the mysqli object, * this issues the MYSQL command found in the * $sql string. */ $res=$this->con->query($sql); if (!$res) { /* Reconnect and retry */ $this->setup_connection(); $res = $this->con->query($sql); } if (!res) die ("Error issuing query" . $con->error()); return $res; } function select_username($username) { $this->username = $username; } } ?>