Lassie Posted July 17, 2009 Share Posted July 17, 2009 I am trying to learn oop and am using David Powers Php Object Orientated solutions. Unfortunately, I cant send this to him and need some pointers. I am trying to establish a database connection using classes. I have php5.1.1 and mysql 5.0.7 On connecting I get a blank screen and no error messages. Any ideas to solve this appreciated. test script <?php require_once '../Pos/MysqlImprovedConnection.php'; require_once '../Pos/MysqlImprovedResult.php'; try { $conn = new Pos_MysqlImprovedConnection('localhost', 'mike', 'j345', 'blog'); $result = $conn->getResultSet('SELECT * FROM blog'); foreach ($result as $row) { foreach ($row as $field => $value) { echo "$field: $value<br />"; } echo '<br />'; } } catch (RuntimeException $e) { echo 'This is a RuntimeException: ' . $e->getMessage(); } catch (Exception $e) { echo 'This is an ordinary Exception: ' . $e->getMessage(); } ?> classes ** * A wrapper class for connecting to MySQL with MySQL Improved * * Apart from the constructor and destructor, this has only one method: * getResultSet(), which returns a Pos_MysqlImprovedResult object. * * @package Pos * @author David Powers * @copyright David Powers 2008 * @version 1.0.0 */ class Pos_MysqlImprovedConnection { /** * MySQLi connection * * @var mysqli Database connection using MySQL Improved. */ protected $_connection; /** * Creates a database connection using MySQL Improved. * * @param string $host Database server name. * @param string $user Database user account. * @param string $pwd User account password. * @param string $db Name of database to connect to. */ public function __construct($host, $user, $pwd, $db) { $this->_connection = @new mysqli($host, $user, $pwd, $db); if (mysqli_connect_errno()) { throw new RuntimeException('Cannot access database: ' . mysqli_connect_error()); } } /** * Submits query to database and returns result as Pos_MysqlImprovedResult object. * * @param string $sql SQL query. * @return Pos_MysqlImprovedResult Result of query. */ public function getResultSet($sql) { $results = new Pos_MysqlImprovedResult($sql, $this->_connection); return $results; } /** * Closes the database connection when object is destroyed. * */ public function __destruct() { $this->_connection->close(); } } [code] <?php /** * A wrapper class for mysqli::query() to implement the Iterator and Countable interfaces. * * Submits a MySQLi query and makes the mysqli_result object iterable and countable. * * @package Pos * @author David Powers * @copyright David Powers 2008 * @version 1.0.1 */ class Pos_MysqlImprovedResult implements Iterator, Countable { /** * Stores value of current element. * * @var mixed */ protected $_current; /** * Stores current element key. * * @var int */ protected $_key; /** * Determines whether a current element exists. * * @var bool */ protected $_valid; /** * Stores the database result. * * @var mysqli_result */ protected $_result; /** * Uses a MySQLi connection to submit a query and stores the result in the $_result property. * * @param string $sql SQL query. * @param mysqli $connection MySQLi connection to database. */ public function __construct($sql, $connection) { if (!$this->_result = $connection->query($sql)) { throw new RuntimeException($connection->error . '. The actual query submitted was: ' . $sql); } } /** * Gets next row from database result and increments key. * */ public function next() { $this->_current = $this->_result->fetch_assoc(); $this->_valid = is_null($this->_current) ? false : true; $this->_key++; } /** * Returns value of current element. * * @return mixed */ public function current() { return $this->_current; } /** * Returns key of current element. * * @return int */ public function key() { return $this->_key; } /** * Determines whether there is current element. * * @return bool */ public function valid() { return $this->_valid; } /** * Moves to first row of database result. * */ public function rewind() { if (!is_null($this->_key)) { $this->_result->data_seek(0); } $this->_key = 0; $this->_current = $this->_result->fetch_assoc(); $this->_valid = is_null($this->_current) ? false : true; } /** * Returns number of rows in database result. * * @return int */ public function count() { return $this->_result->num_rows; } } Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/ Share on other sites More sharing options...
Maq Posted July 17, 2009 Share Posted July 17, 2009 A blank screen usually implies a fatal error. You probably have error_reporting turned off, which you should, but first and foremost you should add these lines directly after your opening <?php tag to temporarily turn error_reporting on: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877071 Share on other sites More sharing options...
Lassie Posted July 17, 2009 Author Share Posted July 17, 2009 Thanks error reporting is on but still blank screen. echo Hello world test shows classes loading. ? Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877075 Share on other sites More sharing options...
Nix Posted July 17, 2009 Share Posted July 17, 2009 Place the following code before anything in page: <?php ob_start(); ?> and the following at the end: <?php ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877092 Share on other sites More sharing options...
Lassie Posted July 17, 2009 Author Share Posted July 17, 2009 Have done , but no change ? Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877104 Share on other sites More sharing options...
J.Daniels Posted July 17, 2009 Share Posted July 17, 2009 Do either a print_r() or var_dump() on $result. Make sure it is an array that you can loop through. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877111 Share on other sites More sharing options...
PFMaBiSmAd Posted July 17, 2009 Share Posted July 17, 2009 Your code has a fatal parse error. You need to set the error_reporting and display_errors settings so that they are on before your code is parsed in order to show fatal parse errors. When learning php, developing php code, or debugging php code, you should have those set in your master php.ini, a local php.ini (when php is running as a CGI application, or in a .htaccess file (when php is running as an Apache Module - you need to use the numeric equivalent of E_ALL in a .htaccess file.) Attempting to set them in a script has no effect for fatal parse errors because the code is never executed to cause the settings to take effect. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877121 Share on other sites More sharing options...
Lassie Posted July 17, 2009 Author Share Posted July 17, 2009 display errors == on and error_reporting = E_All & E_NOtice & E_Strict enabled in php.ini ? Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877146 Share on other sites More sharing options...
PFMaBiSmAd Posted July 17, 2009 Share Posted July 17, 2009 What does a phpinfo(); statement show for those two settings? Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877171 Share on other sites More sharing options...
Lassie Posted July 17, 2009 Author Share Posted July 17, 2009 display_errors=on error_reporting=23*9 sorry this is form memory as I am not at my test machine right now. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877361 Share on other sites More sharing options...
Maq Posted July 17, 2009 Share Posted July 17, 2009 display_errors=on error_reporting=23*9 sorry this is form memory as I am not at my test machine right now. It's better to wait and give us the correct information. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877367 Share on other sites More sharing options...
J.Daniels Posted July 17, 2009 Share Posted July 17, 2009 Looking through the class code, $conn->getResultSet('SELECT * FROM blog'); returns an instance of Pos_MysqlImprovedResult. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877385 Share on other sites More sharing options...
Lassie Posted July 17, 2009 Author Share Posted July 17, 2009 phpinfo() shows error_reporting=2039 display_errrors=on Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877400 Share on other sites More sharing options...
PFMaBiSmAd Posted July 17, 2009 Share Posted July 17, 2009 I tried the posted code and it works for me, however, I do have something in the table I tried. When the query in the posted code does not return any rows, the code produces a blank page because to does not output anything. It needs to test how many rows are in the result set and DO SOMETHING if there are none, such as outputting a message to the user and it should ONLY execute the foreach() code when there is at least one row in the result set. mysqli is already an OOP class. Wrapping it with another class adds an necessary layer that in this case is hiding what is actually going on. Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877410 Share on other sites More sharing options...
Lassie Posted July 18, 2009 Author Share Posted July 18, 2009 I think its wrapped in another class because the code is part of a series of exercices building a script to access the db, otput an xml file and subsequently create an RSS feed. Being new to oop I follow what you say but how to test a row is returned? Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877528 Share on other sites More sharing options...
PFMaBiSmAd Posted July 18, 2009 Share Posted July 18, 2009 The mysqli_result->num_rows property would give that value. The result class you are using has a ->count() method that returns that property. if($result->count() > 0){ // result set has 1 or more rows } else { // result set has no rows } Quote Link to comment https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.