Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何处理共享公共ID的SQL查询的多个结果?_Php_Mysql - Fatal编程技术网

Php 如何处理共享公共ID的SQL查询的多个结果?

Php 如何处理共享公共ID的SQL查询的多个结果?,php,mysql,Php,Mysql,我很难理解我所知道的应该是简单的东西,也不确定解决方案是在查询中还是在处理结果中,所以我很难在另一篇文章中找到我确信的东西 我正在使用一个MySQL表,从中我需要能够检索和处理共享公共ID的内容。不幸的是,重构数据库不是一个选项 下面是一个简单的SELECT*查询返回的内容,其中contentID='2' |contentType|contentID|content | -------------------------------------- |title |2

我很难理解我所知道的应该是简单的东西,也不确定解决方案是在查询中还是在处理结果中,所以我很难在另一篇文章中找到我确信的东西

我正在使用一个MySQL表,从中我需要能够检索和处理共享公共ID的内容。不幸的是,重构数据库不是一个选项

下面是一个简单的
SELECT*查询返回的内容,其中contentID='2'

|contentType|contentID|content       |
--------------------------------------
|title      |2        |<h1>title</h1>|
|intro      |2        |<p>intro</p>  |
|main       |2        |<p>main</p>   |
| contentType | contentID | content|
--------------------------------------
|标题| 2 |标题|
|简介| 2 |简介

| |main | 2 |main

|
检索特定“标题”、“简介”或“主要”内容的正确方法是什么


提前感谢。

将所有行提取到一个数组中:

$data = array();
foreach ($rows as $r) {
  $data[$r['contentType']] = $r['content'];
}

您可以对每种内容类型使用$array

您必须将内容类型放入where子句中。Id和类型应该形成一个自然ok,并且应该受到这样的约束。如果没有,是时候重新设计了。另外,我认为把内容类型当作一个查找而不是一个字符串。

代码>你已经在你的例子中了,也许你需要澄清你的问题?谢谢埃米尔,这帮助了很多。
<?php
 function executeQuery($query,$connectionObject)
                {
                queryString=$query;
                        recordSet=mysql_query(queryString,$connectionObject);
                        if(!$recordSet)
                        {
                            $errorString=mysql_error($this->connectionObject);
                            return array("error"=>$errorString);
                        }

                        return recordSet;
                    }
                function getNextRecord($recordSet)
                    {
                        $resultArray =mysql_fetch_assoc($recordSet);
                        if(!empty($resultArray))
                        return($resultArray);
                        else
                        return "false";


                    }

        $result = executeQuery("SELECT * FROM foo WHERE contentID = '2'");

        $nextRecord = getNextRecord($result);
        while($nextRecord!="false")
        {
        $nextRecord = getNextRecord($result);
        //...........Process the record.....

         ->$nextRecord['contentType'];
         ->$nextRecord['contentID'];
         ->$nextRecord['content'];
    //..................................

        }
<?php
 function executeQuery($query,$connectionObject)
                {
                queryString=$query;
                        recordSet=mysql_query(queryString,$connectionObject);
                        if(!$recordSet)
                        {
                            $errorString=mysql_error($this->connectionObject);
                            return array("error"=>$errorString);
                        }

                        return recordSet;
                    }
                function getNextRecord($recordSet)
                    {
                        $resultArray =mysql_fetch_assoc($recordSet);
                        if(!empty($resultArray))
                        return($resultArray);
                        else
                        return "false";


                    }

        $result = executeQuery("SELECT * FROM foo WHERE contentID = '2'");

        $nextRecord = getNextRecord($result);
        while($nextRecord!="false")
        {
        $nextRecord = getNextRecord($result);
        //...........Process the record.....

         ->$nextRecord['contentType'];
         ->$nextRecord['contentID'];
         ->$nextRecord['content'];
    //..................................

        }