Codeigniter - db-> get()不会返回与PHPMyAdmin相同的查询相同的结果

时间:2014-11-16 21:51:09

标签: php mysql database codeigniter phpmyadmin

我遇到了Codeigniter的问题,我从db-> get()获得的结果与通过PHPMyAdmin向数据库发出的相同查询不同。结果是相同的,除了id字段不是。

清单MySQL表是:

CREATE TABLE IF NOT EXISTS `listings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` tinytext NOT NULL COMMENT 'Name of listing',
  `type` set('Bar','Bar and Restaurant','Cinema','Club','Concert','Dance activity','Dance performance','Demonstration/protest','Event','Exercise','Exhibition','Gallery','Lecture','Opera','Other','Market','Museum','Park','Protest','Pub','Physical activity','Restaurant','Sight seeing','Sport','Street food','Take away','Theatre','Tourist attraction','Transport','Walks') NOT NULL COMMENT 'Type of listing',
  `price` float NOT NULL DEFAULT '0' COMMENT 'Price of listing',
  `shortDescription` tinytext NOT NULL,
  `longDescription` text NOT NULL,
  `website` tinytext NOT NULL,
  `phoneNumber` varchar(11) NOT NULL,
  `address` tinytext NOT NULL,
  `postcode` varchar(10) NOT NULL,
  `image1` varchar(60) NOT NULL COMMENT 'Filename of 1st image',
  `image2` varchar(60) NOT NULL COMMENT 'Filename of 2nd image',
  `image3` varchar(60) NOT NULL COMMENT 'Filename of 3rd image',
  `days` set('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','Monday to Friday','Monday to Saturday','Monday to Sunday') NOT NULL COMMENT 'Days of week this listing is open',
  `openingTimes` text NOT NULL COMMENT 'Text description of opening times in specific format',
  `validFrom` date NOT NULL COMMENT 'Date this listing is active from',
  `validTo` date NOT NULL COMMENT 'Date this listing is availably until',
  `bookmarked` int(11) NOT NULL DEFAULT '0' COMMENT 'Number of times this has been bookmarked',
  `dateAdded` date NOT NULL COMMENT 'date this listing was added',
  `dateLastAmended` date NOT NULL COMMENT 'date this listing was last changed',
  `latitude` float(10,6) NOT NULL,
  `longitude` float(10,6) NOT NULL,
  `publish` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'If set listing is published and available to show',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='Data for each listing' AUTO_INCREMENT=877 ;

listing_categories MySQL表是:

CREATE TABLE IF NOT EXISTS `listings_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `listingId` int(11) NOT NULL COMMENT 'Associated listing',
  `arts` tinyint(1) NOT NULL,
  `entertainment` tinyint(1) NOT NULL,
  `events` tinyint(1) NOT NULL,
  `food` tinyint(1) NOT NULL,
  `nightlife` tinyint(1) NOT NULL,
  `outdoor` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=553 ;

正在运行的查询是:

SELECT *, ( 3959 * acos( cos( radians(51.24977493163702) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-2.8468933105468737) ) + sin( radians(51.24977493163702) ) * sin( radians( latitude ) ) ) ) 
AS distance FROM listings JOIN listings_categories 
ON listings_categories.listingId = listings.id 
WHERE listings.price <= 40 
AND ( listings_categories.arts = 1 
OR listings_categories.entertainment = 1 
OR listings_categories.events = 1 
OR listings_categories.food = 1 
OR listings_categories.nightlife = 1 
OR listings_categories.outdoor = 1) 
HAVING distance < 1000 ORDER BY distance LIMIT 100;

PHPMyAdmin返回并作为PHP数组导出的第一个结果是:

$listings = array(
  array(
'id'=>485,
'name'=>'Layla Restaurant',
'type'=>'Bar,Bar and Restaurant,Restaurant',
'price'=>22,
'shortDescription'=>'Lebanese',
'longDescription'=>'<p>
 Layla Restaurant is a stylish Lebanese restaurant in Esher. Settle down on cushions and enjoy a meal of traditional Lebanese food and meze. Or &nbsp;relax in the lounge and enjoy cocktails and belly dancing that Layla restaurant provides.</p>
',
'website'=>'http://www.laylarestaurant.co.uk/',
'phoneNumber'=>02089447696,
'address'=>'33 High Street Esher Kingston',
'postcode'=>'KT10 9QL',
'image1'=>'5f1ad-layla_fotor.jpg',
'image2'=>'7869f-layla2_fotor.jpg',
'image3'=>'',
'days'=>'Monday to Sunday',
'openingTimes'=>'Mon-Thu 12:00-00:00
Fri-Sat 12:00-01:30
Sun 12:00-23:00',
'validFrom'=>'0000-00-00',
'validTo'=>'0000-00-00',
'bookmarked'=>0,
'dateAdded'=>'2014-11-01',
'dateLastAmended'=>'2014-11-01',
'latitude'=>51.368423,
'longitude'=>-0.367311,
'publish'=>1,
'id'=>464,
'listingId'=>485,
'arts'=>0,
'entertainment'=>0,
'events'=>0,
'food'=>1,
'nightlife'=>1,
'outdoor'=>0,
'distance'=>107.41168679083839)
);

Codeigniter中db-&gt; get()返回的第一个结果是:

["id"]=> string(3) "464" 
["name"]=> string(16) "Layla Restaurant" 
["type"]=> string(33) "Bar,Bar and Restaurant,Restaurant" 
["price"]=> string(2) "22" 
["shortDescription"]=> string(8) "Lebanese"
...

我可以看到问题是由于我在结果中有两个id字段。一个来自listing表,一个来自listing_categories表,当我使用db-&gt; get()的结果时我错了一个。

所以我的问题是如何确保我选择正确的身份证?

我目前使用的代码是:

    foreach ($queryResult->result() as $row)
    {
        $data["results"][$i]["name"] = $row->name;
        $data["results"][$i]["id"] = $row->id;
        $data["results"][$i]["distance"] = $row->distance;
        $data["results"][$i]["shortDescription"] = $row->shortDescription;
        $data["results"][$i]["image1"] = $row->image1;
        $data["results"][$i]["price"] = $row->price;
        $i++;
    }

1 个答案:

答案 0 :(得分:1)

var_dump($row)循环中执行for_each()并检查获得的字段,可能已经覆盖了id字段(我猜可能就是这种情况)或者表名已经已附加到其他id字段。

foreach ($queryResult->result() as $row) { var_dump($row); die(); }查看字段

作为一种更好的编程实践,只要不同的字段名称相同,字段名称为tablename_fieldname总是好的,所以在您的情况下,字段名称应为listings_categories_id和{{1 }}