Page 1 of 10

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

Posted: Tue Sep 03, 2013 4:24 am
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

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

Posted: Tue Sep 03, 2013 5:20 am
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>";
			}

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

Posted: Tue Sep 03, 2013 6:37 am
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();
}

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

Posted: Tue Sep 03, 2013 6:41 am
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?

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

Posted: Tue Sep 03, 2013 6:49 am
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)

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

Posted: Tue Sep 03, 2013 6:52 am
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>

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

Posted: Tue Sep 03, 2013 6:55 am
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;

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

Posted: Tue Sep 03, 2013 6:57 am
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.

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

Posted: Tue Sep 03, 2013 7:02 am
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.

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

Posted: Tue Sep 03, 2013 7:04 am
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??

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

Posted: Tue Sep 03, 2013 8:15 am
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?

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

Posted: Tue Sep 03, 2013 8:19 am
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?

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

Posted: Tue Sep 03, 2013 8:27 am
by Celauran
That really depends on your server setup; check php.ini

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

Posted: Tue Sep 03, 2013 8:30 am
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".

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

Posted: Tue Sep 03, 2013 8:34 am
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