RE: [PHP] What am i doing wrong? SQL Query in PHP
| From: | Rahul.Brenda | Date: | Mon, 10 Mar 2003 05:41:37 +0000 |
| Subject: | RE: [PHP] What am i doing wrong? SQL Query in PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-138916@lists.php.net to get a copy of this message | ||
Hi,
Thanks.. i went through your reply and certainly now i
understand the mistake i was making. Quite logical
actually, that i hadn't actually selected the "title"
column. But i'm sure i wouldnt' have been able to
figure this out myself.
And surely, i too try to stay away from Numerical
Indexes.
Well thank you, this does enlighten quite a bit.
Rahul S. Johari
> In case you're wondering, your method is fine. You
> can continue to use
> this or what chris suggested. Either way, the
> problem is that you need
> an alias for your column that you selected. With the
> above code, you
> didn't select a column called "title" so you can't
> use $row['title'].
> You created a column called "substring_index(title,
> ' ', 3)" so you'll
> have to use $row["substring_index(title, ' ', 3)"].
> As you can probably
> guess, there's an easier way to do it, though...
>
> If you use a query such as:
>
> SELECT SUBSTRING_INDEX(title, ' ', 3) AS f_title
> FROM news ORDER BY id
> LIMIT 4
>
> You can then use $row['f_title'] to print the
> result. Using "AS" in your
> query will rename the column to "f_title" or
> whatever you put there.
> This is called assigning the column an alias.
>
> Of course, though all of this you could of just used
> $row[0] to get the
> value. I tend to stay away from using numerical
> indexes though, as it
> makes it harder for other people to follow your
> code. When you see
> $row['f_title'], it gives you a good idea of what
> you're displaying.
> When you see $row[0], you have no idea what it is
> and you have to go
> find the query that was run in order to determine
> what column zero was.
>
> Hope that helps.
>
> ---John W. Holmes...
>
> PHP Architect - A monthly magazine for PHP
> Professionals. Get your copy
> today. http://www.phparch.com/
>
>
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/