lynxus Posted December 22, 2009 Share Posted December 22, 2009 Hi Guys, Ive got a MASSIVE sql query that returns to columns, But doesnt actually have a colum name. Normlaly i could use while($row = mssql_fetch_array($result)) { echo " Layer3Id " . $row["Layer3Id"]; echo " CustID " . $row["CustomerId"]; } However in this case i cant use the names.. How can i still achieve the same output without column identifiers? IE: currently it whines that theres undefined indexes ( because the column has no name ) Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/ Share on other sites More sharing options...
anthylon Posted December 22, 2009 Share Posted December 22, 2009 Try using mysql_fetch_assoc directive instead of mysql_fetch_array. It will return recordset with column names instead of index number. Please, set the topic status [solved] if this works for you! Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982244 Share on other sites More sharing options...
lynxus Posted December 22, 2009 Author Share Posted December 22, 2009 There are no column names. Due to the way it has 30 or 40 left joins etc. it doesn't return a colmn name. ( and it uses MSsql not MySQL ) Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982252 Share on other sites More sharing options...
anthylon Posted December 22, 2009 Share Posted December 22, 2009 Sorry about that! :'( Have you tried with analogy: mssql_fetch_assoc($query) ? It must work! Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982256 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 Either use aliases in your SQL query (ie SELECT COUNT(id) AS id_count FROM table).to give the return field a name, alternatively I'd guess it will add them as $row[0], $row[1] etc. but you can use print_r($row) to see what values it contains. Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982259 Share on other sites More sharing options...
teynon Posted December 22, 2009 Share Posted December 22, 2009 Sounds like your running a query like SELECT * FROM table. If thats the case, the numbers reference the sections in order. Say your table is: ID Name Date the array would be 0=ID 1=Name 2=Date I don't like using * unless I am using absolutely everything from the table. Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982261 Share on other sites More sharing options...
anthylon Posted December 22, 2009 Share Posted December 22, 2009 Hm, even if he is using SELECT * FROM... it still must return name of column. As cags said he should try print_r($row); and see how it looks like. Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982277 Share on other sites More sharing options...
lynxus Posted December 22, 2009 Author Share Posted December 22, 2009 Either use aliases in your SQL query (ie SELECT COUNT(id) AS id_count FROM table).to give the return field a name, alternatively I'd guess it will add them as $row[0], $row[1] etc. but you can use print_r($row) to see what values it contains. Good stuff!! I didnt think of that! Thanks! Link to comment https://forums.phpfreaks.com/topic/186007-while-through-sql-query-that-doesnt-have-column-names/#findComment-982278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.