Jump to content

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

Edited by Freid001
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);

Edited by Freid001

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 by Freid001

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 by Adam
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.