New to PDO: basic script failing. Including ORDER BY...
Posted: Tue Sep 03, 2013 4:24 am
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:
This works very nicely.
Next, in index.php I have this:
Which includes this:
In one include called menu.inc, I have this:
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
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/>";
}
?>Next, in index.php I have this:
Code: Select all
include "dbconn.php";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);
?>
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>";
}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