Solved

How to automatically get column names to my query?

Posted on 2014-03-09
5
237 Views
Last Modified: 2014-05-16
Hi,

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 :)
0
Comment
Question by:kgp43
5 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39915733
How about mysqli_fetch_object? ( You're going to get pounced on for using mysql_ instead of mysqli_ )
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

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

$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 */
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>

Open in new window

0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39915799
$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 ;
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39915810
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

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.
http://php.net/manual/en/control-structures.foreach.php

This may be helpful, too:
http://php.net/manual/en/function.get-object-vars.php
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39916170
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);
var_dump($software);

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39916292
Worth reading the online man page:
http://php.net/manual/en/function.array-keys.php
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question