Jump to content

Is there a way to make rsults from SQL query into an array


Lostnode

Recommended Posts

Ok, so this seems kinda vague, cus normally thats what you do, however I am talking about a differnt kinds of output from a query... Here is what i am a talking about:

 

My Query Code:

$query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2;
$result = mysql_query($query,$which);
$num = mysql_num_rows ($result);


$id = mysql_result($result,$i,"id");
$storeid = mysql_result($result,$i,"storeid");
$tinypass = mysql_result($result,$i,"tinypass");
$taxsetting = mysql_result($result,$i,"taxsetting");
$gst = mysql_result($result,$i,"gst");
$pst = mysql_result($result,$i,"pst");
$hst = mysql_result($result,$i,"hst");
$canpar = mysql_result($result,$i,"canpar");
$cp_fuelcharge = mysql_result($result,$i,"cp_fuelcharge");
$cp_markup = mysql_result($result,$i,"cp_markup");
$fedex = mysql_result($result,$i,"fedex");
$fe_fuelcharge = mysql_result($result,$i,"fe_fuelcharge");
$fe_markup = mysql_result($result,$i,"fe_markup");
$dhl = mysql_result($result,$i,"dhl");
$dh_fuelcharge = mysql_result($result,$i,"dh_fuelcharge");
$dh_markup = mysql_result($result,$i,"dh_markup");
$tnt = mysql_result($result,$i,"tnt");
$tn_fuelcharge = mysql_result($result,$i,"tn_fuelcharge");
$tn_markup = mysql_result($result,$i,"tn_markup");

 

As you can see, my variables are the same as my table column names... now if this "project" goes thorugh I could have hundreds of columns, I would hate to have to define each one making my file even larger.

 

Is there a way to make it dynamically create the variables via an array or something

 

Theoriticaly something like this:

$query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2;
$result = mysql_query($query,$which);
$num = mysql_num_rows ($result);

(While loop here)
$Table_name_var = mysql_result($result,$i,"table_name");
(end while loop)

 

Possible?

extract?

 

<?php
$query = "SELECT * FROM settings WHERE storeid='store350' ";
$which = $handle_db2;
$result = mysql_query($query,$which) or die(mysql_error());
$num = mysql_num_rows ($result);

if ($num > 0) {
$data = mysql_fetch_assoc($result);
extract($data);
}
?>

extract?

 

<?php
$query = "SELECT * FROM settings WHERE storeid='store350' ";
$which = $handle_db2;
$result = mysql_query($query,$which) or die(mysql_error());
$num = mysql_num_rows ($result);

if ($num > 0) {
$data = mysql_fetch_assoc($result);
extract($data);
}
?>

 

Ok, would this make it so that I can use the column names as variables?

 

for instance if I changed my code to the above, would the variable $dh_markup exist with the value of the dh_markup column from my db?

well theres another way too.... you can try this:

 

foreach($array_results as $key => $val){

    $$key = $val;

}

 

so your column names (the associative index) will be variables that hold the values

 

That is what extract() does.

That is what extract() does.

 

well indeed... .but theres some advantage of using loop over extract().... you might already know it but anyways heres a link i found....:

 

http://davidwalsh.name/convert-key-value-arrays-standard-variables-php

 

besides lostnode wanted to know the other way too....i guess..!!

 

 

Thank you for the responses, I hadn't tested it out but I saw that somewhere in my code was a snippet that does that extract and it seems to work fine.  I will test it out when I have a chance.  As I have seen it work in another file, I am marking this solved.

 

 

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.