Freid001 Posted November 29, 2012 Share Posted November 29, 2012 (edited) Ok so I am trying to connect to a mysql database and just simply return the id of a row in the table admin. However I get the following error message: Fatal error: Call to a member function fetch() on a non-object in /srv/disk5/657046/www/worldwarfare.atwebpages.com/testcon.php on line 19 Here is my PHP code: <?php session_start(); $mobile = $_SESSION['mobile']; $user = $_SESSION['username']; $host = '********'; $mysql_user = '******'; $mysql_password = '*******'; $mysql_database = '***********'; //PDO Connect $db= new pdo("mysql:host=$host; $mysql_database", $mysql_user, $mysql_password); //PDO Fetch Statement $query = $db->query("SELECT `id` FROM `Admin`"); while($row = $query->fetch()){ echo $row['id']; } ?> In PHP4 this is what I am trying to do in PDO $con = mysql_connect($host,$mysql_user,$mysql_password); mysql_select_db($mysql_database); $query = mysql_query("SELECT * FROM Admin"); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_assoc($query)) { echo $row['id']; } Any Help Will Be Much Appreciated! Thanks Edited November 29, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/ Share on other sites More sharing options...
Adam Posted November 29, 2012 Share Posted November 29, 2012 Have a read of the manual for PDO::query(). If the query fails, false is returned, not a statement object. Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396289 Share on other sites More sharing options...
Freid001 Posted November 29, 2012 Author Share Posted November 29, 2012 (edited) Have a read of the manual for PDO::query(). If the query fails, false is returned, not a statement object. But the query works fine in PHP 4. Edited November 29, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396295 Share on other sites More sharing options...
Adam Posted November 29, 2012 Share Posted November 29, 2012 Well for one, the SQL in your PHP4 example is different. That doesn't change that the query is failing though. Have a look at PDO::errorInfo() to get more insight into what's going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396296 Share on other sites More sharing options...
Freid001 Posted November 29, 2012 Author Share Posted November 29, 2012 (edited) I have tried using this code. Which works but only returns a blank array. Not sure why? //PDO Connect $db= new pdo("mysql:host=$host; $mysql_database", "657046_ww", "sandon"); //PDO Statement $sth = $db->prepare("SELECT * FROM Admin"); $sth->execute(); $result = $sth->fetchAll(); print_r($result); Edited November 29, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396297 Share on other sites More sharing options...
Adam Posted November 29, 2012 Share Posted November 29, 2012 That means there are no results from the query. Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396298 Share on other sites More sharing options...
Freid001 Posted November 29, 2012 Author Share Posted November 29, 2012 (edited) Must mean there is something wrong with the connection then because the query works fine in PHP 4. But The connection I used to connect to the db in PHP 4 also works fine. Not sure what I am doing wrong. Edited November 29, 2012 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396301 Share on other sites More sharing options...
Adam Posted November 29, 2012 Share Posted November 29, 2012 (edited) The PDO constructor throws an exception if the connection failed. You keep posting different code; can you modify the original code to this and report back what it says: //PDO Fetch Statement $query = $db->query("SELECT `id` FROM `Admin`"); if (!$query) { var_dump($db->errorInfo()); exit; } while($row = $query->fetch()){ echo $row['id']; } FYI this isn't the way to handle erors, it's just to debug the problem. Edited November 29, 2012 by Adam Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396303 Share on other sites More sharing options...
kicken Posted November 29, 2012 Share Posted November 29, 2012 You're not selecting which database to use. Your DSN should look like: "mysql:host=$host;dbname=$mysql_database" Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396304 Share on other sites More sharing options...
Freid001 Posted November 29, 2012 Author Share Posted November 29, 2012 YES! Thanks so much. It is working now! Kind of new to this PDO stuff LOL. Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396306 Share on other sites More sharing options...
Adam Posted November 29, 2012 Share Posted November 29, 2012 Ah, well spotted kicken! Quote Link to comment https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/#findComment-1396307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.