paul590
paul590

Reputation: 1435

retreaving data from a mysql database

I have a mysql database which was created for a website using wordpress, when posting some text using wordpress it stores it on that database. When I try pulling this text using a php file, it returns null, but if I delete this text and write it in manually(no wordpress) it pulls it fine. is there anything specific i need to do in order to pull this text since it was created using wordpress? the structure of the table is as follows, field: post_content type: longtext. Thanks for the help

Code:

$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
    $output[] = $row;
print(json_encode($output));

Upvotes: 0

Views: 147

Answers (2)

anuragbh
anuragbh

Reputation: 593

Wordpress gives you a class that lets you run your queries on the database more easily. Reference: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column

Try:

$result = $wpdb->get_col($wpdb->prepare("SELECT `post_content` FROM `meiplay_posts` WHERE post_type = 'portfolio'")); 

if ($result) 
{
   echo $result->post_content;
   echo json_encode($result);
};

Upvotes: 1

Brendan Long
Brendan Long

Reputation: 54312

Add some error handling:

$result = mysql_query($query) or die(mysql_error());

This will cause an error message to be printed if something goes wrong in mysql_query. Hopefully the error message will help.

Also, the documentation for mysql_query says:

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

So I'd advise using the libraries that the PHP authors recommend.

Also:

$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";

Are your sure that columns with post_type = 'portfolio' exist? Try doing the select without the WHERE part and see if you get anything. Maybe it's slightly different, like Portfolio instead of portfolio?

Upvotes: 1

Related Questions