php 脚本 returns 每行包含一个词

php script returns every row containing a word

这个 php 脚本 returns mySQL 数据库中的每一行:

<?php

$host = "host_name";
$db = "database_name"; 
$user = "user_name";
$pass = "password";

$connection = mysql_connect($host, $user, $pass);

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT * FROM farmer";
        $resultset = mysql_query($query, $connection);

        $records = array();

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;        
        }

        //Output the data as JSON
        echo json_encode($records);
    }


}

?>

此脚本运行良好,但每一行 returns。

假设我的数据库包含农民的姓名及其产品类型。第一列是"id, the second is "farmer_name”,第三列是"produce"。农产品是这样输入的:豆类、玉米、生菜、草莓、西红柿。

我想做的是更改上面的脚本,使其 returns 只有包含种植玉米的农民的姓名以及他们种植的所有农产品类型的行。所以,我不仅想要名称,还想要所有产品的列表,只要农民种玉米。

这是否可能基于数据库的结构方式?我使用 needle/haystack 进行了几次尝试,但都空手而归。我对 php.

知之甚少

就像使用 WHERE

一样简单
$query = "SELECT * FROM farmer WHERE produce = 'corn'";

请注意 mysql_ 功能已贬值

SELECT `farmer_name`, `produce` FROM `farmer` WHERE `produce` LIKE '%corn%'

这将 return 在农产品田的任何地方找到玉米的行。