tags:

views:

204

answers:

5

What is the propper syntax to grab data from of all the matching fields?

This example outputs only 1 of the matching fields:

$myvariable = "SELECT post_content 
                 FROM wp_posts 
                WHERE post_name = 'testing' 
                  AND post_status = 'publish' 
                  AND post_type = 'post'";

echo = $myvariable;

Thanks in advance!!!

A: 

list all the fields in your select statement or use * to select all.

$myvariable = "SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'"
Mohamed
I don't think he was trying to select all the columns... He used the wrong wording, but I think he's trying to find all the matching rows and echo them, which I've explained above.
BraedenP
@BraedenP, according this text "This example outputs only 1 of the matching fields:", he is getting some data from the database. so I think he wants to list all fields in the table. I could be wrong, but that is how I understood the question.
Mohamed
A: 

You'll need to pull out the query and run a loop over the result rows that come back.

$query = mysql_query("SELECT post_content FROM wp_posts 
             WHERE post_name='testing' 
             AND post_status='publish' AND post_type = 'post'");

You'll need to adjust the above query to whichever rows you want to display as well as whatever criteria you want to match against.

And then run something like a while loop with each pass of the rows that were returned:

while($result = mysql_fetch_array($query))
{
    echo $result["post_content"];
}

The above will go and pull out all the rows one by one that matched your query and display them however you style it.

random
+2  A: 

You need to fetch an array of that query:

<?php
$query = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");
while($result = mysql_fetch_array($query)){
echo $result['post_content'];
}
?>

That will loop through the result list from the query and echo the post_content field value.

EDIT: Wow... Same thing, a few seconds late. Ha!

BraedenP
Thank you very much. Your answer has been of great help!
Ronal
+1  A: 

Your question is not clear to me. There may be two cases:

  1. You want to get all the rows which match you condition. then you should use a loop to grab all matching records as below-

    $result = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");

    if(mysql_num_rows($result)>0) while($row = mysql_fetch_assoc($result)){ echo $row['post_content']; }

  2. i couldn't understand what do you want to know. if you want to select all the fields of selecting row then use-

    SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'

OR

SELECT col1,col2,...,coln FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'

if you want to check conditions with all the fields then you are on the right track, just compare each value with respective column

Sadat
A: 

If you want to match any, then your SQL is false.

SELECT post_content FROM wp_posts WHERE post_name = 'testing' OR post_status = 'publish' OR post_type = 'post'

The query above finds each record which contains exactly one of the search strings.

But if you want to find it within a sentence then you better use the MATCH function.

MySQL Full-Text Search Functions

Tom Schaefer