New to PDO: basic script failing. Including ORDER BY...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Hi

I am brand new to the PDO style of coding, so please have a little patience with me here, as I am trying to convert from mysql_.

I have run a simple test.php file which opens a PDO conn:

Code: Select all

<?php
$pdo = new PDO('mysql:host=localhost;dbname=site_name', 'root', '');
$query = "SELECT email FROM admin WHERE type = 'admin'";
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
    echo "$row->email<br/>";
} 
?>
This works very nicely.

Next, in index.php I have this:

Code: Select all

include "dbconn.php";
Which includes this:

Code: Select all

<?php
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'db_name');

$sqlconn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ($sqlconn) {
        mysql_select_db(DBNAME);
}

$pdo = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
?>

In one include called menu.inc, I have this:

Code: Select all

echo "<div class='menuheader'>Categorías</div>";
$query = "SELECT email FROM admin WHERE type = 'admin'";
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
    echo "$row->email<br/>";
} 

$result = $db->query('SELECT id, catid, catname, uk_catname FROM products');
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
      $categ = "$row->catname"; 
      $findcateg ="/ /"; 
      $replacecateg ="-"; 
      $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 
			echo "<div class='submenu'><a href='/categ/$row->catid/$categreplace'>$row->catname</a></div>";
			}
The top one for admin is just to test it works, as it's the same code as test.php. The second one is for real, but incomplete (as I need order by in there).
These are do not work at all. No error codes. The page just stops running at "</div>";....

I'm clearly doing something fatally wrong here. Maybe it doesn't like working within dbconn.php as an include (tho the script is correct if I use mysql_).

So a little patience and guidance would be appreciated.

Simon

ps am trying to learn from here: http://wiki.hashphp.org/PDO_Tutorial_fo ... Developers
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Is it because I am using include 'dbconn.php'; ??
It works for the older version of mysql, maybe not this version?
Have tried require, but that doesn't work either.

Something is clearly blocking it from running.

This isn't producing anything either:

Code: Select all

$result = "SELECT id, catid, catname, uk_catname FROM products";
$q = $pdo->query($result);
if(!$q)
{
  die("Execute query error, because: ". $pdo->errorInfo());
}

while ($row = $result->fetch(PDO::FETCH_OBJ)) {
      $categ = "$row->catname"; 
      $findcateg ="/ /"; 
      $replacecateg ="-"; 
      $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 
			echo "<div class='submenu'><a href='/categ/$row->catid/$categreplace'>$row->catname</a></div>";
			}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

Check that error reporting is enabled. PDO::__construct() throws an exception if it cannot connect to the database, so you'll want to catch that.

Code: Select all

try {
    $pdo = new PDO('mysql:host=' . DBHOST . ';dnbame=' . DBNAME, DBUSER, DBPASS);
} catch (Exception $e) {
    echo 'Could not connect: ' . $e->getMessage();
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Hi.
Have just put that in my code, and it's still producing nothing at all.

Code: Select all

<?php
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'mydb_name');

$sqlconn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ($sqlconn) {
        mysql_select_db(DBNAME);
}

try {
    $pdo = new PDO('mysql:host=' . DBHOST . ';dnbame=' . DBNAME, DBUSER, DBPASS);
} catch (Exception $e) {
    echo 'Could not connect: ' . $e->getMessage();
}
?>

edit: how do I ensure that PDO thing is "enabled"?  Is it in that code, or in php.ini somewhere?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

var_dump($result) in your menu.inc. I'm curious to see what that gives. The PDO constructor isn't throwing any exceptions, and PHP would complain if you were trying to call query() on a non-object. Check the var_dump and check that you've set error_reporting(-1)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Code: Select all

$result = "SELECT id, catid, catname, uk_catname FROM products";
var_dump($result) 
print_r($result);
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
      $categ = "$row->catname"; 
      $findcateg ="/ /"; 
      $replacecateg ="-"; 
      $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 
			echo "<div class='submenu'><a href='/categ/$row->catid/$categreplace'>$row->catname</a></div>";
			}
Like this>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

Sorry, no. I was looking at a code snippet higher up in the thread. Inside your menu.inc:

Code: Select all

$query = "SELECT email FROM admin WHERE type = 'admin'";
$result = $pdo->query($query);
var_dump($result); exit;
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Code: Select all

$query = "SELECT email FROM admin WHERE type = 'admin'";
$result = $pdo->query($query);
var_dump($result); exit;
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
    echo "$row->email<br/>";
} 
Running this, I get nothing at all.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

edit: how do I ensure that PDO thing is "enabled"? Is it in that code, or in php.ini somewhere?
Just saw this now. You can check phpinfo(). PHP should also complain if you try to instantiate a class that doesn't exist.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

[text]API Extensions mysql,mysqli,pdo_mysql [/text]

[text]PDO
PDO support enabled
PDO drivers mysql, sqlite

pdo_mysql
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.11-dev - 20120503 - $Id: 40933630edef551dfaca71298a83fad8d03d62d4 $
[/text]
It states this in there, what else am I looking for?

Surely the fact that it works in test.php proves PDO works tho??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

simonmlewis wrote:Surely the fact that it works in test.php proves PDO works tho??
Agreed. That PHP should just die with no messages whatsoever is still odd. Have you checked error reporting levels? Checked your server logs?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

How do I access error logs on my local machine? This is all running locally until I get it working and go live with it.
Also, how do I ensure error logs are switched on per site, locally?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

That really depends on your server setup; check php.ini
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: New to PDO: basic script failing. Including ORDER BY...

Post by simonmlewis »

Yes I am in there.
When I first installed the latest xampp, it had all errors on showing on the page.
While I don't want that, right now.... I do. How do I enable it so I can make it shout at me the problem? I'm sure it's one like where it's "Off", but needs to be "On".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New to PDO: basic script failing. Including ORDER BY...

Post by Celauran »

If you don't want to enable it globally, just include the following in a file that gets included globally.

Code: Select all

error_reporting(-1);
Also check your php.ini to ensure that display_errors is set to 1
Post Reply