Jump to content

Assign each value in array to separated variables


kristian_gl

Recommended Posts

Hello.

I need to get the the column names from a table called "answers", and assign each column name to a variable. The table is not static, so I need to do this dynamically (i.e the code needs to work if there is just one or hundreds of columns).

So far I've used SHOW COLUMNS the get all the column names, and when I echo out $column[0] (which I believe is an array) , I get a list over all the columns, but I can't seem to assign each value in $column[0] to separated variables.

 

CODE:

 

$sql = "SHOW COLUMNS from answers";
$result = mysql_query($sql);

while ($column= mysql_fetch_array($result))
{
echo  $column[0] . "<br/>";
}

 

Any help would be much appreciated

Kristian

Link to comment
Share on other sites

maybe, it looks like it's related with my problem, but I can't seem to figure out how the exact syntax for my problem is (I'm a bit slow today), since none of the examples operates with an array in this form columns[0] (with the "[0]"). If I echo out $columns[1] I get the data types for the columns (i.e INT(4), VARCHAR(320), so it is only the values of $columns[0] that I need to convert to separate variables.

Link to comment
Share on other sites

Not really sure if this is what you are looking for.

 

presumption:

table has 3 columns named id, last_name, first_name

 

this will get the column names and create the following variables

 

$id -- the value of this variable at the time it is created is id (NOT the value of id BUT the letters i and d)

 

$last_name -- the value of this variable at the time it is created is last_name (NOT the value of last_name BUT the letters l, a, s, t  etc etc)

 

$first_name -- as as above

 

$sql = "SHOW COLUMNS from edselford_1963";
$result = mysql_query($sql);

while ($column= mysql_fetch_array($result)){
$arg="";
$val=$column[0];
${$arg . $val} = $val;
echo $val . "<br>";
}

does that help?

Link to comment
Share on other sites

Slightly better to store the column names in an array rather than to pollute the name space. Since you don't necessarily know the names of the variables, you might be overwriting something unintentionally. Also, using an associative array to fetch the results makes it a little clearer, I think.

 

$sql = "SHOW COLUMNS from answers";
$result = mysql_query($sql);

// array to store column names
$colNames = array();

// iterate over result set
while ($row = mysql_fetch_assoc($result)) {

        // append column name to array
        $colNames[] = $row['Field'];
}

// output column names for testing
print_r($colNames);

Link to comment
Share on other sites

yes, I have looked at extract(), but I can't seem to make it work. If somebody could show me exactly how to give each value in:

$columns[0] a different variable it would be really helpful. Remember that I dont know the what the values are, and how many of they there are at any given time, so it makes it a bit more complicated.

Link to comment
Share on other sites

Pseudo code:

 

1. get column names from table "answers"

2. put each column name in a variable (maybe $col1, $col2, $col3 and so on..)

3. use these variables in a SQL-query ( something like this: $sql = "INSERT INTO $db. `answers( `$col1`, `$col2``$col3`) VALUES (NULL, \'hei\', \'\', NULL);";

 

 

Link to comment
Share on other sites

the code needs to work if there is just one or hundreds of columns

 

Are you sure you are not really talking about rows? Because a table design that is dynamically  adding columns as the number of questions/answers changes would be a bad design and result in horrendous code to make it work (as has been demonstrated by the lack of progress in this thread.)

Link to comment
Share on other sites

In fact, I just reviewed your three threads and you are apparently dynamically adding tables (your first thread) and columns (your second and this thread.) Doing both of these things over complicates the process of managing data, takes more code to achieve, results in slower processing (you must execute multiple queries to just find the data), and results in a design that is not flexible.

 

What exactly is the overall goal you are trying to accomplish?

Link to comment
Share on other sites

yes I know that my design is not optimal, but it's just for a school project, and I can live with some flaws (better that then going back to change the whole setup). So if anybody could help me with:

1. get column names from table "answers"

2. put each column name in a variable (maybe $col1, $col2, $col3 and so on..)

 

it would be extremely helpful

 

 

Link to comment
Share on other sites

$query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'YourDatabase' AND TABLE_NAME = 'YourTable'";
$result = mysql_query($query);

$columns = array();

while($row = mysql_fetch_row($result)) {
   $columns[] = $row[0];
}

var_dump($columns);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.