Jump to content

Php Pdo Connection Help!


Freid001

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/271377-php-pdo-connection-help/
Share on other sites

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);

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.