PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > resultset and two tables in sql query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
resultset and two tables in sql query

Réponse
 
LinkBack Outils de la discussion
Vieux 03/12/2007, 20h37   #1
bender
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut resultset and two tables in sql query

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
}
  Réponse avec citation
Vieux 03/12/2007, 21h36   #2
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: resultset and two tables in sql query

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
  Réponse avec citation
Vieux 03/12/2007, 23h06   #3
bender
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: resultset and two tables in sql query

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?
  Réponse avec citation
Vieux 04/12/2007, 05h30   #4
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: resultset and two tables in sql query

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09765 seconds with 12 queries