Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to automatically get column names to my query?

Posted on 2014-03-09
5
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

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 44

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 111

Accepted Solution

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

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

730 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