-3

Trying to get the data from a table in my database and store it into a textfield named "about"

however i keep getting an error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in

<?php
require("common.php");     
$query = $db->prepare("SELECT * FROM about");     
$result = $query or die(mysql_error()); // run the query    
$row = mysql_fetch_assoc($result); // fetch a result row      
echo $row['about'];    
?>
juergen d
  • 201,996
  • 37
  • 293
  • 362
cooLLedge
  • 15
  • 1
  • 7
  • sorry i pressed enter and instantly realised - it has been edited – cooLLedge Nov 13 '13 at 11:41
  • What is the table name? – juergen d Nov 13 '13 at 11:43
  • 2
    table is "about" field being "content" there is only one field at the moment, im pretty new so trying to do it one step at a time before adding further parameters to go into individual fields. – cooLLedge Nov 13 '13 at 11:45
  • `$db->prepare` prepare is object of instance pdo? – juniorb2s Nov 13 '13 at 11:45
  • Change the select to `SELECT content FROM about` and your echo to `echo $row['content'];` – juergen d Nov 13 '13 at 11:48
  • @juergend but the error he is getting is not due to what you are saying.and he is already using '*' so whats the point of explicitly use content in select. – R R Nov 13 '13 at 11:51
  • @RishabhRaj: That part is not so important, but afterwards he tries to select `about` which is the table name and not the column. – juergen d Nov 13 '13 at 11:54
  • @juergend you are right but when i saw it was content ,i think the question was updated or i overlooked something. – R R Nov 13 '13 at 11:57
  • @juergend just to be clear - the table in my database is "about" and the field in my database is "content" however the textfield of which i want the information to be put into is "about" which is why i had it as $row['about'] - i have tried the way you have stated and i still receive the same error – cooLLedge Nov 13 '13 at 12:02
  • @user2987509: All this confusion and guessing is the result of an incomplete question. Please add all necessary info in future questions. That way you get a good, quality answer. – juergen d Nov 13 '13 at 12:07
  • @juergend yes apologies... this is literally the first time i have used this site, but i now know for future reference to make myself more clear – cooLLedge Nov 13 '13 at 12:08

2 Answers2

1

thats because you are not executing your query.after Prepare use Execute().your code will look something like this

<?php
require("common.php"); 
$query = $db->prepare("SELECT * FROM about");  
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
print_r($result);//to check the elements of the array
echo $row['content'];    
?>

Remember

PDO::prepare() - Prepares a statement for execution and returns a statement object
PDOStatement::execute() - Executes a prepared statement
R R
  • 2,999
  • 2
  • 24
  • 42
0

You are missing mysql_query();. This to execute your sql query.

$query = "SELECT * FROM about";     
$result = mysql_query($query) or die(mysql_error()); 
Rajiv Ranjan
  • 1,869
  • 1
  • 11
  • 20
  • if you are using PDO please stick to it and mysql_* are deprecated ,please dont use that. – R R Nov 13 '13 at 11:49