resultset and two tables in sql query

resultset and two tables in sql query

am 03.12.2007 21:37:38 von bender

Is possible to read variable using table.fieldName like in example?

example query: SELECT table1.*, table2.* FROM teble1, table2 WHERE
table1.id = table2.id;

$rs = $this->dbc->query($sql->createSelect());
while($row=$rs->fetch_array()){
$retVal[]=$row[table.fieldName] // doesnt work, $row[fieldName]
//does work
}

Re: resultset and two tables in sql query

am 03.12.2007 22:36:50 von Shion

bender wrote:
> Is possible to read variable using table.fieldName like in example?
>
> example query: SELECT table1.*, table2.* FROM teble1, table2 WHERE
> table1.id = table2.id;

SELECT * FROM teble1, table2 WHERE table1.id = table2.id;

If you want only a limited number of columns

SELECT table1.col1,table1.col2,table2.col2, table2.col5 FROM teble1, table2
WHERE table1.id = table2.id;


> $rs = $this->dbc->query($sql->createSelect());
> while($row=$rs->fetch_array()){
> $retVal[]=$row[table.fieldName] // doesnt work, $row[fieldName]
> //does work
> }

In PHP you will only get the column name as the array cell key name when you
use fetch_array, so $row['fieldName'] will work, you can get trouble if you
have columns with the same name, I suggest you in those cases use AS in your
query to rename those

SELECT table1.col1,table1.col2 AS col2-1,table2.col2 AS col2-2, table2.col5
FROM teble1, table2 WHERE table1.id = table2.id;

This way you won't have trouble with cell keys having hte same name.


--

//Aho

Re: resultset and two tables in sql query

am 04.12.2007 00:06:58 von bender

J.O. Aho wrote:

Thanks for an answare, but...

>
> SELECT * FROM teble1, table2 WHERE table1.id = table2.id;
>
> If you want only a limited number of columns
>
> SELECT table1.col1,table1.col2,table2.col2, table2.col5 FROM teble1, table2
> WHERE table1.id = table2.id;
>
>

I want to select all columns from two tables (or three or more if
necessary) without alias for each column

>> $rs = $this->dbc->query($sql->createSelect());
>> while($row=$rs->fetch_array()){
>> $retVal[]=$row[table.fieldName] // doesnt work, $row[fieldName]
>> //does work
>> }
>
> In PHP you will only get the column name as the array cell key name when you
> use fetch_array, so $row['fieldName'] will work, you can get trouble if you
> have columns with the same name, I suggest you in those cases use AS in your
> query to rename those
>
> SELECT table1.col1,table1.col2 AS col2-1,table2.col2 AS col2-2, table2.col5
> FROM teble1, table2 WHERE table1.id = table2.id;
>
> This way you won't have trouble with cell keys having hte same name.
>
>

In PHP4 was mysql_result which produced resultset with
tableName.fieldName (if my memory is good:
$mysql_result($rs,$i,tableName.fieldName)) and I wonder if is possible
to get in PHP5 with mysqli?

Re: resultset and two tables in sql query

am 04.12.2007 06:30:53 von Shion

bender wrote:

> In PHP4 was mysql_result which produced resultset with
> tableName.fieldName (if my memory is good:
> $mysql_result($rs,$i,tableName.fieldName)) and I wonder if is possible
> to get in PHP5 with mysqli?

I can say I never got that in PHP4 nor PHP5. FRom the manual for
mysql_fetch_array:

If two or more columns of the result have the same field names, the last
column will take precedence. To access the other column(s) of the same name,
you must use the numeric index of the column or make an alias for the column.
For aliased columns, you cannot access the contents with the original column name.

and mysqli_fetch_array:

If two or more columns of the result have the same field names, the last
column will take precedence and overwrite the earlier data. In order to access
multiple columns with the same name, the numerically indexed version of the
row must be used.

So you will need to use AS in the query if you want to emulate the tablename
included in the column name.

--

//Aho