English 中文(简体)
我如何在加入《发展权利宣言》之后获得正确的一栏?
原标题:How can I get the right column after a join using PDO?
  • 时间:2011-10-03 18:53:36
  •  标签:
  • php
  • pdo

我有两个我的Sql表,如:

Cat
------
id : INT NOT NULL PRIMARY KEY
name : VARCHAR(32)

Cheezburger
-------
id : INT NOT NULL PRIMARY KEY
catId : INT FOREIGN KEY REFERENCES Cat (id)
pattyCount : INT

When I perform a LEFT JOIN on the two tables using PHP s PDO I d do the following:

$database = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $pasword);
$query = $database->prepare( 
    SELECT * FROM Cat
    LEFT JOIN Cheezebuger.id ON Cheezburger.catId = cat.id
    WHERE Cat.id = ? );
$query->bindParam(1, $someId);
$query->execute();
$cat = $query-fetch();

我的问题是,我如何区别<代码>id与<代码>有关的领域。 Cat table and the id associated with the Cheezburger table? www.un.org/Depts/DGACM/index_french.htm 页: 1

最佳回答

It s often inadvisable to SELECT *. Instead, be explicit about the columns you want and assign aliases to like-named columns:

SELECT
  Cat.id AS catid,
  Cat.name AS name,
  Cheezeburger.id AS chzid,
  ...
FROM 
  Cat LEFT JOIN Cheezebuger.id ON Cheezburger.catId = cat.id
WHERE Cat.id = ?

Now, to retrieve Cat.id, use $cat[ catid ] and to retrieve Cheezeburger.id, use $cat[ chzid ].

A strong reason against doing SELECT * is that if you add more columns later which are not needed in this particular part of your application, you won t be using extra memory on the database server to retrieve unneeded data and fetch it into the web application where it won t be used. If you happened to add a binary BLOB column later, you could potentially pull over a lot more data unnecessarily.

另一个原因是你发现的原因。 您可请在栏目中模糊不清。

问题回答

• 避免<条码>*,并具体说明你想要的领域:

SELECT cat.ID AS catID, Cheezeburger.id AS icanhaz ...

你们需要列举这些领域,并列出这些领域,这样,我们就好像。

SELECT cat.id as catId, cat.name, cheezburger.id as cheezId ...




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签