• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

How to automatically get column names to my query?


I'm using the following (just an example), to get software details from my table:
# Get software info
$software_query = mysql_query("SELECT * FROM software WHERE id='$softwareID' LIMIT 1 ");
while($software_fetch = mysql_fetch_array($software_query)) {

	$software['id'] = $software_fetch['id'];
	$software['title'] = $software_fetch['title'];
	$software['text'] = $software_fetch['text'];
	$software['p1'] = $software_fetch['p1'];
	$software['p2'] = $software_fetch['p2'];
	$software['p3'] = $software_fetch['p3'];
	$software['p4'] = $software_fetch['p4'];
	$software['p5'] = $software_fetch['p5'];
	..... etc

Open in new window

I would like get everything related to that specific software (all columns), but without typing them all, one by one.

Maybe something like this (not valid, does not work) - it shows what i'm looking for:

# Get software info
$software_query = mysql_query("SELECT * FROM software WHERE id='$softwareID' LIMIT 1 ");
while($software_fetch = mysql_fetch_array($software_query)) {

	$software[$column_name] = $software_fetch[$value];

Open in new window

It automatically populate the array with column names and values related to the specific software.
I can add more columns to the table without editing the PHP query.

But I have no idea how to make a query to do that.
Maybe somebody will help me out a bit :)
1 Solution
Tom BeckCommented:
How about mysqli_fetch_object? ( You're going to get pounced on for using mysql_ instead of mysqli_ )
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

$softwareID = '123456'; //whatever it is

$software_query = "SELECT * FROM software WHERE id='$softwareID' LIMIT 1";

if ($result = mysqli_query($link, $software_query)) {

    /* fetch associative array */
    while ($software_fetch = mysqli_fetch_object($result)) {
        // access objects in the array $software_fetch by column name
        // $software_fetch->id;
        // $software_fetch->title;
        // $software_fetch->text;
        // ...

    /* free result set */

/* close connection */

Open in new window

$str = '' ;
$software_query = mysql_query("SELECT * FROM software WHERE id='$softwareID' LIMIT 1 ");
$i= 0 ;
while($software_fetch = mysql_fetch_array($software_query))  continue ; // build the array
foreach($software_fetch as $key=>$value) // explode it
  $str = $str.$key."=>".$value."<br/>" ;
echo $str ;
Ray PaseurCommented:
Step One: Read this article and take it to heart.  You're facing a data base conversion.  The article will tell you why and what you must do to keep your scripts running.

I don't have time to write you an example now, but I can show you the theory.  But first, please take some time to rethink the concept of SELECT * -- it's a time bomb because you risk injecting variables into the scope of your script.  If you add a column to the data base table, and the column name happens to be the same as a variable in your code, well, that's how data bases get accidentally destroyed.  No problem if you are working alone.  Big problem if you have collaborators.

When your query is run, the next step is to fetch the rows.  I like to use the fetch_object() variants because the syntax is simpler.  When you fetch one of the rows in the form of an object, the object property names point to the values.  You can iterate over an object with foreach() using the key => value structure. The key is the name of the column.

This may be helpful, too:
Chris StanyonCommented:
OK. Apart from the fact that you're using mysql_* which is deprecated, you don't actually need to do anything. If you want an array that contains all your database columns automatically, then just pull your database query into an associative array.

You don't need a while loop because you're only ever pulling 1 record

$software_query = mysql_query("SELECT * FROM software WHERE id='$softwareID' LIMIT 1 ");
$software = mysql_fetch_assoc($software_query);

Open in new window

Ray PaseurCommented:
Worth reading the online man page:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now