Trying to get sql result array into variables

Hi everyone,

I really need some help with a project I’m working on. I have a table in MySQL that I am querying to pull out some data.

This is the results I am getting from my query:


| fld_id | fld_vl |

| CLNT_NM | Motel1 |
| CLNT_MGR | Frank |
| CLNT_TMZ | EST |

What I would like to do is get a variable for each fld_id and populate it with the field value.

So, in essence it would look like this.

$CLNT_NM = ‘Motel1’;
$CLNT_MGR = ‘Frank’;
$CLNT_TMZ = ‘EST’;

How can I do this? I have tried to figure this out but I can’t seem to do it.

Any help you guys can provide would be greatly appreciated. THANKS in advance!!!

I use a technique called MySQLi which may be a little different then how people normally query the database. I believe the following technique will give you what you want.

[php]
$db = new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
$results = $db->query(“SELECT * FROM table”);
while($row = $results->fetch_assoc())
{
$$row[‘fld_id’] = $row[‘fld_vl’];
}
$db->close();

[/php]

This should create variables of the same name as what is in the first field and give them the values that are in the second field. I hope this technique helps you =D.

This is how it is done in standard mysql:
[php]

//database connection
$query = mysql_query(“SELECT * FROM table”);
while($row = mysql_fetch_assoc($query))
{
$fld_id = $row[‘fld_vl’];

	}

[/php]

[php]
//database connection
$query = mysql_query(“SELECT * FROM table”);
while($row = mysql_fetch_assoc($query))
{
$$row[‘fld_id’] = $row[‘fld_vl’];
}

[/php]

Fixed it for you. Remember, he wants the values in the first column as variables that have the value of the values in the second column.

Sponsor our Newsletter | Privacy Policy | Terms of Service