php - Mysql select from multiple rows in one query -


i have mysql database table (that stores settings app):

table name: settings  id    param_name    param_value -------------------------------- 1     ringtone      12.mp3 2     user          nick 3     email         nick@example.com 4     location      athens, greece 5     phone         0123456789 6     time_offset   gmt+3 

the "id" column auto-incremental , gets value mysql.

the column "param_name" has initial values (such as: ringtone, user ...etc)

the column "param_value" takes it's values dynamically "settings" form each parameter has it's unique value.

i want select these rows 1 query , turn each 1 of them variable such as:

$this_ringtone = "12.mp3"; $this_user = "nick"; $this_email = "nick@example.com"; $this_location = "athens, greece"; $this_phone = "0123456789"; $this_time_offset = "gmt+3"; 

i can multiple queries since rows lot more, prefer single mysqli query.

this kind of hard achieve way want, name of column same values - there different values want specific variables. should instead flip table, looks this

| id  | ringtone | user | email            | location | phone | time_offset | +-----+----------+------+------------------+----------+-------+-------------+ |  1  | 12.mp3   | nick | nick@example.com | greece   | 12345 | utc+3       | |  2  | 25.mp3   | joe  | joe@example.com  | france   | 54321 | utc+3       | +-----+----------+------+------------------+----------+---------------------+ 

you can use code such below

$result = mysqli_query($link, "select * table"); while ($row = mysqli_fetch_assoc($result)) {     $id             = $row['id'];     $ringtone       = $row['ringtone'];     $user           = $row['user'];     ...     ...     $time_offset    = $row['time_offset']; } 

...and on. because fetch value of cell based on name of column, in case param_name , param_value. can't know when should put proper variables, unless table static (never changes, can hard-code - bad practice) or there 6 rows each user - if that's case, you're easier off doing structure shown above anyway.

note if have different rows of values structure above, you'd last 1 way (as select everything, , put variables each row). example, you'd id: 2 , france-guy (and never id: 1, or greece-guy, if have many lines, query should different. because id: 2 last row in table.


even more elegantly, can use php function extract(), this

$result = mysqli_query($link, "select * table"); $row = mysqli_fetch_assoc($result); extract($row); // extract values of array variable of own echo $user; // name of variable same column in database 

hope provided clear guidance on how table-structure should like.


update
right, whatever reason don't want table-structure above, use of variable variables, you'd need 1 query. this

$values = array(); $result = mysqli_query($link, "select * table"); while ($row = mysqli_fetch_assoc($result)) {     ${$row['param_name']} = $row['param_value']; } echo $ringtone; 

this creates variable name of value in $row['param_name'], , variable has value of $row['param_value']. lot easier achieve thought.

if want variable-name $this_name, use ${"this_".$row['param_name']} = $row['param_value'];

you should have param_name column set unique in database, avoid variables being overwritten.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -