Solved

How to automatically get column names to my query?

Posted on 2014-03-09
5
232 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 108

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 42

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 108

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now