Jump to content

non object error


AL123

Recommended Posts

New to classes, basic understanding of procedural.

I am trying to echo rows from the data base using PDO.

Here is the code:


<div id="admin">
<form method="POST" action="categoryAdmin.php">
<table>
	<tr><th bgcolor="#EEEEEE">Cat Id</th>
		<th bgcolor="#EEEEEE">Title</th>
		<th bgcolor="#EEEEEE">Description</th>
	</tr>
<?php
global $dbh;
$sql = "SELECT * FROM categories";
$sth = $dbh->prepare($sql);
$sth-> execute();
print_r($sth);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
while($row = $result->fetchAll(PDO::FETCH_ASSOC))
	{echo "<tr><td>$row[0]<td><td>$row[1]<td><td>$row[2]<td><tr>\n";}
?>
	<tr><td><input type="text" name="CatId" size="15" maxlength="10"></td>
		<td><input type="text" name="CatTitle" size="40" maxlength="130"></td>
		<td><input type="text" name="CatDesc" size="45" maxlength="200"></td>
	</tr>
</table>
<input type="hidden" name="addRecord" value="1">
<input type="submit" name="submit" value="Add Category">
</div>

 

print_r($sth); shows:  PDOStatement Object ( [queryString] => SELECT * FROM categories )

print_r($result); shows:  Array ( [0] => Array ( [categoryId] => Fitness [title] => Sholder Guy [description] => Fix that sholder and more ) )

fatal error:  Call to a member function fetchAll() on a non-object in /home/...

the line in question is the while loop. I am not sure what to pass it with reguards to PDO.

 

Thanks -

AL

Link to comment
Share on other sites

Sorry for taking so long to reply. The short answer to both of your statements is - I'm a newbee.

I am not that familiar with the PDO class I just have sections of it working.

A foreach should get me my values.

 

I guess trying to imitate this caused the problem:

function locate_table($usrId)  
{
global $dbh;
$sql = "SELECT * FROM tblBlog WHERE UserId = $usrId ";
$sth = $dbh->prepare($sql);   
$sth-> execute();
$tbl = $sth->fetchAll(PDO::FETCH_ASSOC);//fetchall 
return $tbl;
} 

function viewEdit()  //replay_table($_SESSION['iden']);
{
$shoAgain = locate_table($_SESSION['iden']);
foreach($shoAgain as $key=>$value)
{
	echo "<ul>";
	echo $value['Btype']; 
	echo " <br /> <a href= '../UserView/index.php?id=".$value['id']."&T=".$value['Title']." ' > ".$value['Title']." </a><br /><br /> "; 
	echo "</ul>";
}
}

I am trying to advance my php skills at home, trial and error. Will mark solved if foreach works.

Thanks -

AL

 

Link to comment
Share on other sites

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.