Ronal
Ronal

Reputation: 2243

How can I grab data from all of the matching fields?

What is the propper syntax to grab data from all of 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;

Upvotes: 0

Views: 617

Answers (5)

Sadat
Sadat

Reputation: 3481

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 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 its respective column.

Upvotes: 1

Tom Schaefer
Tom Schaefer

Reputation: 897

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

Upvotes: 0

BraedenP
BraedenP

Reputation: 7215

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!

Upvotes: 2

random
random

Reputation: 9955

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.

Upvotes: 0

Mohamed
Mohamed

Reputation: 3600

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'"

Upvotes: 0

Related Questions