Welcome! Log In Create A New Profile

Advanced

[PHP] How to explode an array from a Database?

Posted by Michelle Konzack 
Michelle Konzack
[PHP] How to explode an array from a Database?
May 28, 2017 02:00PM
Sitting on my line!

I fetch successfully rows/columns from my database I get a multiple
array which I read with foreach, but if I have a row, which delimter
must I use to explode the row?

Thanks

--
Michelle Konzack Miila ITSystems @ TDnet
GNU/Linux Developer 00372-54541400
German Geek
Re: [PHP] How to explode an array from a Database?
May 28, 2017 02:20PM
No need to explode, pardon the pun :-). You need to fetch the rows into an
array or other data structure like an iterator. Detailed guides are on the
web.

On Sun, 28 May 2017 at 23:55 Michelle Konzack <linux4michelle@gmail.com>
wrote:

> Sitting on my line!
>
> I fetch successfully rows/columns from my database I get a multiple
> array which I read with foreach, but if I have a row, which delimter
> must I use to explode the row?
>
> Thanks
>
> --
> Michelle Konzack Miila ITSystems @ TDnet
> GNU/Linux Developer 00372-54541400 <+372%205454%201400>
>
Michelle Konzack
[PHP] Re: How to explode an array from a Database?
May 28, 2017 04:40PM
I tried to use the following (base) code:

----8<------------------------------------------------------------------
[01] $db_link = db_connect();
[02] $db_result = db_query($db_link, "SELECT serial,$category FROM categories");
[03] $cat_array = db_fetch_all($db_result);
[04]
[05] sort($cat_array, SORT_NUMERIC);
[06]
[07] foreach($cat_array as $row) {
[08] extract($row);
[09] $strg .= " <a href=\"/?what=category&num=$serial\">$category</a><BR>\n";
[10] }
[11]
[12] db_close($db_link);
----8<------------------------------------------------------------------

the problem is in db_query (line 02) with "$category" where the value
can change between e.g.

category
cat_de
cat_fr
cat_ee
cat_ru
...

so, how I can evaluate the name of the value of the variable $category
in line 09?

Thanks

--
Michelle Konzack Miila ITSystems @ TDnet
GNU/Linux Developer 00372-54541400
First of all, I would recommend using
http://php.net/manual/en/book.pdo.php PDO.
Having something like
db_query($db_link, "SELECT serial,$category FROM categories");
is a bad idea if you are running this code in a web page, because of SQL
injections.

There is a fetch for assoc and then you can find the array key of the
associate array.

You'll find a lot of resources just googling some of these words. Maybe
look into using the preg_split function and finding a regular expression to
split on if you have to do that anywhere.

More of the code and samples of data would also help with helping you.

On Mon, 29 May 2017 at 02:34 Michelle Konzack <linux4michelle@gmail.com>
wrote:

> I tried to use the following (base) code:
>
> ----8<------------------------------------------------------------------
> [01] $db_link = db_connect();
> [02] $db_result = db_query($db_link, "SELECT serial,$category FROM
> categories");
> [03] $cat_array = db_fetch_all($db_result);
> [04]
> [05] sort($cat_array, SORT_NUMERIC);
> [06]
> [07] foreach($cat_array as $row) {
> [08] extract($row);
> [09] $strg .= " <a
> href=\"/?what=category&num=$serial\">$category</a><BR>\n";
> [10] }
> [11]
> [12] db_close($db_link);
> ----8<------------------------------------------------------------------
>
> the problem is in db_query (line 02) with "$category" where the value
> can change between e.g.
>
> category
> cat_de
> cat_fr
> cat_ee
> cat_ru
> ...
>
> so, how I can evaluate the name of the value of the variable $category
> in line 09?
>
> Thanks
>
> --
> Michelle Konzack Miila ITSystems @ TDnet
> GNU/Linux Developer 00372-54541400 <+372%205454%201400>
>
Larry Garfield
Re: [PHP] Re: How to explode an array from a Database?
May 31, 2017 12:20AM
To the specific issue you're having:

"SELECT serial, $category as cat FROM categories"

That way the result rows will always have a column named "cat".

That said, German Geek is correct that if you feel the need to do that,
you should probably be looking at a different approach, or using a
formal query builder library. (Doctrine DBAL is the most popular but by
no means the only.)

Also, extract() is almost never the right tool. Just reference into the
array: $row['cat']. (And then remember to escape your output properly.)

--Larry Garfield

On 05/28/2017 07:16 PM, German Geek wrote:
> First of all, I would recommend using
> http://php.net/manual/en/book.pdo.php PDO.
> Having something like
> db_query($db_link, "SELECT serial,$category FROM categories");
> is a bad idea if you are running this code in a web page, because of SQL
> injections.
>
> There is a fetch for assoc and then you can find the array key of the
> associate array.
>
> You'll find a lot of resources just googling some of these words. Maybe
> look into using the preg_split function and finding a regular expression to
> split on if you have to do that anywhere.
>
> More of the code and samples of data would also help with helping you.
>
> On Mon, 29 May 2017 at 02:34 Michelle Konzack <linux4michelle@gmail.com>
> wrote:
>
>> I tried to use the following (base) code:
>>
>> ----8<------------------------------------------------------------------
>> [01] $db_link = db_connect();
>> [02] $db_result = db_query($db_link, "SELECT serial,$category FROM
>> categories");
>> [03] $cat_array = db_fetch_all($db_result);
>> [04]
>> [05] sort($cat_array, SORT_NUMERIC);
>> [06]
>> [07] foreach($cat_array as $row) {
>> [08] extract($row);
>> [09] $strg .= " <a
>> href=\"/?what=category&num=$serial\">$category</a><BR>\n";
>> [10] }
>> [11]
>> [12] db_close($db_link);
>> ----8<------------------------------------------------------------------
>>
>> the problem is in db_query (line 02) with "$category" where the value
>> can change between e.g.
>>
>> category
>> cat_de
>> cat_fr
>> cat_ee
>> cat_ru
>> ...
>>
>> so, how I can evaluate the name of the value of the variable $category
>> in line 09?
>>
>> Thanks
>>
>> --
>> Michelle Konzack Miila ITSystems @ TDnet
>> GNU/Linux Developer 00372-54541400 <+372%205454%201400>
>>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login