Jump to content

Recommended Posts

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;
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/166327-solved-blank-screen/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877071
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877121
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877410
Share on other sites

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

}

Link to comment
https://forums.phpfreaks.com/topic/166327-solved-blank-screen/#findComment-877593
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.