Jump to content

From mysql to an array ? How?


Rommeo

Recommended Posts

My language table structure looks like :

 

id | english

mail_accepted | Your e-mail is accepted.

thank_you | Thank you for your visit.

 

 

What I want to do is, I want to take all these data to an array like : "lang" array

And I can use these in the page like

<?php
echo "goodbye ".$lang['thank_you'];
?>

 

I know how to take data, lets say

<?php
$query = "select * from language.. ";
mysql_query($query);
?>

But then ?, which function should I use to take the data to my "$lang" array ?

Link to comment
https://forums.phpfreaks.com/topic/220150-from-mysql-to-an-array-how/
Share on other sites

$lang = array();
$rs = mysql_query('SELECT id, english FROM lang');
while ( $row = mysql_fetch_array($rs) )
{
  $lang[$row['id']] = $row['english'];
}

Add debugging and whatnot, but that's the structure.

 

Also, your table design is wrong, you should make 3 columns:  id, language, text.  This way, you can add a new language without adding columns.

 

-Dan

Also, your table design is wrong, you should make 3 columns:  id, language, text.  This way, you can add a new language without adding columns.

 

-Dan

Actually my table looks like :

id | english | german | swedish .. etc.

I m able to add more columns and languages,I did not understand why its wrong ?

 

select id,english from language

select id,german from language

select id,swedish from language

etc..

 

Because you have to alter the table (which takes a long time) every time you have to add a new language.

 

If you designed it my way, you can say:

SELECT id, text FROM lang WHERE language = 'swedish';

 

You could also have a second table holding id/value pairs for the languages so you can say:

SELECT id, test FROM lang WHERE language = 3;

 

If you continue down your path, you will have to add a new column for every new language, which will lock the table (and prevent your site from working while the operation completes).

 

-Dan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.