-1

Hi I have started to change my mysql to PDO before my code starts getting large and risk of causing problems i have tried changing a section of my code but the screen shows as white this is my original code:

class SelectList
{
    protected $conn;

        public function __construct()
        {
            $this->DbConnect();
        }

        protected function DbConnect()
        {
            include "db_config.php";
            $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
            mysql_select_db($db,$this->conn) OR die("can not select the database $db");
            return TRUE;
        }
         public function ShowPrinciple()
        {
            $sql = "SELECT principle.principle_id,principle.description,principle.section_id,COUNT(media.principle_id) as media_count
                    FROM principle 
                    LEFT OUTER JOIN media ON principle.principle_id = media.principle_id 
                    AND principle.section_id = media.section_id 
                    WHERE principle.section_id={$_POST['id']}
                    GROUP BY principle.principle_id,principle.description";
            $res = mysql_query($sql,$this->conn);
            $principle = '<option value="%">choose...</option>';
            while($row = mysql_fetch_array($res))
            {
                $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'].  '...('.$row['media_count'].') </option>';
            }
            return $principle;
        }
}
$opt = new SelectList();

I have tried a simple query to start off with to change but I seem to have done something wrong could some one point out to me I'm positive it will be something I have changed.

This is the code that I have currently got:

class SelectList
{

$host = '127.00.00.00';
$user = 'user';
$password = 'password';
$db =  'database'; 

 try{ 
    $conn = new PDO("mysql:host=$host;dbname=$db", $user, $password);
    echo 'connected to the database<br />';

    public function ShowPrinciple()
    {

        $stmt = $conn -> prepare(
                    "SELECT principle.principle_id,principle.description,principle.section_id,COUNT(media.principle_id) as media_count
                    FROM principle 
                    LEFT OUTER JOIN media ON principle.principle_id = media.principle_id 
                    AND principle.section_id = media.section_id 
                    WHERE principle.section_id={$_POST['id']}
                    GROUP BY principle.principle_id,principle.description");
        $q = $conn->query($stmt) or die("failed!)";
        $principle = '<option value="%">choose...</option>';
        while($row = fetchAll($q))
        {
            $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'].  '...('.$row['media_count'].') </option>';
        }
        return $principle;
    }catch(PDOException $e){
        echo $e->getMessage();
    }
}
$opt = new SelectList();

Any help would be much appreciated.

RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
user3387522
  • 127
  • 4
  • 19
  • your $conn->prepare call is missing the closing parenthesis. – Tularis Mar 07 '14 at 11:11
  • @Tularis I hadn't noticed this thank you but I still have the same results – user3387522 Mar 07 '14 at 11:13
  • your $conn->query() or die( block has the same problem. – Tularis Mar 07 '14 at 11:14
  • A blank page means that your script is throwing an error but you haven't configured PHP to display error messages. That's something you need to fix before you go further; it's impossible to code properly without the aid of error messages. Here's a [brief explanation](http://stackoverflow.com/a/5680885/13508). – Álvaro González Mar 07 '14 at 11:14
  • @ÁlvaroG.Vicario my server is configured to display the error message but still is not currently doing so – user3387522 Mar 07 '14 at 11:23
  • There are many thing wrong with the way you are doing it, and your presentation does not help either. One thing I don't get is that, why you would use `try/catch` blocks and `or die()` at the same time? If you don't solve your problems consider using a pdo wrapper like [This](https://github.com/simon-eQ/PdoWrapper) – samayo Mar 07 '14 at 11:29
  • @Simon_eQ as I said before I am new to using PDO so at the moment I am at a learning curve at the moment I have been following this page with the [examples](http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#1) – user3387522 Mar 07 '14 at 11:34
  • @user3387522 Don't check that site, check [this one](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) you'll learn much more, with little time if you used that site – samayo Mar 07 '14 at 12:02
  • @ÁlvaroG.Vicario I have tried a couple of the examples and started my coding from scratch but I am still not getting anything on the web page. – user3387522 Mar 07 '14 at 12:20
  • You are really convinced that your PHP script is configured to display errors. How do you explain that you don't seem to get any? – Álvaro González Mar 07 '14 at 12:37
  • @ÁlvaroG.Vicario tbh at moment i believe it is a problem with the script and not the server due to the fact that i have have used dubug code before to make sure error messages are displayed so i believe it is either i have chosen the wrong method to display the errors or somewhere in my code there a wrong syntax or something missing that is stopping it from running the main fact is my php skill base needs improving to understand the concepts of PDO and error feedback and general syntax – user3387522 Mar 07 '14 at 12:41
  • @user3387522 PHP errors are logged in the apache error log files regardless if display errors is on. I always use logs to trouble shoot because it will give you the same details. Depending on your system look for your apache log file. error_log or error.log. There is no other way to trouble blank errors because it can literally be anything from syntax errors to libraries not installed. – Panama Jack Mar 07 '14 at 15:13

3 Answers3

1

So you don 't know how to write classes, right? First: check your error reporting! Turn it up if it 's not turned up. PHP should output some fatal errors here. The following code can def not work:

class Bla {
    $var1 = 'foo';
    $var2 = 'bar';

    try {
        public function doSth() { ... }
    } catch (SomethingException $e) {
        doSthWthExcpetion();
    }
}

That 's not proper PHP. Make yourself comfortable with dependency injection or something else, that work 's as this:

class Bla {
    protected $dbHandle = null;

    public function __construct() {
        $this->dbConnect();
    }

    public function dbConnect() {
        $this->dbHandle = new PDO(...);
    }

    public function doSthDatabseish($variable) {
        try {
            $stmt = $this->dbHandle->prepare("SLECET something FROM myTable WHERE variable = :variable");
            $stmt->bindValue(':variable', $variable);
            $stmt->execute();

            foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
                $value[] = $row;
            }
        } catch (PDOException $e) {
            // errorhandling
        }

        return $value;
    }
}

Do you see the difference? Never write code between functions. Code belongs in the function definition. As your first example class shows, you establish the database connection in a connectDB() function. So why don 't you in the PDO Version of your class?

Marcel
  • 4,854
  • 1
  • 14
  • 24
-1

your $conn->prepare() call is missing the closing parenthesis. (ie. change $conn->prepare(... to $conn->prepare(...); (note te ")" character at the end).

Same thing goes for your $conn->query() or die(... part.

The reason you're seeing a white page is because your server is configured not to show error on screen. Adjust your php.ini file to display_errors=On and error_reporting=E_ALL to actually see the errors.

Tularis
  • 1,506
  • 1
  • 8
  • 17
  • i have fixed both of the missing brackets but my php.ini file has been set to display errors as you have said any ideas ? – user3387522 Mar 07 '14 at 11:19
  • You'll have to change the settings inside the php.ini file itself; either directly, or (if using apache) via a. .htaccess file. – Tularis Mar 07 '14 at 11:23
  • yes I have gone into the php.ini file through the server and made sure that all of the values are correct it was already set to show the error messages as you said above – user3387522 Mar 07 '14 at 11:25
-1

Your main error is with

or die("failed!)"; 

which is redundant and with the parenthesis wrong.

To set error mode . After

$conn = new PDO(...

Add

$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 

Also your use of PDO is wrong.You either use

query() Without parameters (Your case)

$query = ( "SELECT ....");
$stmt = $conn->query($query);  
// setting the fetch mode  
 $stmt->setFetchMode(PDO::FETCH_ASSOC);  
$principle = '<option value="%">choose...</option>';
while($row = $stmt->fetch()) { 

etc...

OR prepare() execute() With parameters

$stmt = $conn -> prepare("SELECT....")
//Bind parameters here or as an array in execute()
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC); 
   while($row = $stmt->fetch()){
david strachan
  • 7,174
  • 2
  • 23
  • 33