开发者

PHP MYSQL Display Entire Column

开发者 https://www.devze.com 2023-03-30 09:10 出处:网络
I have a page and I want to only display my name column, not age or anything else. Under that though I want to show the entire age column and so on, so I 开发者_开发百科don\'t want to make multiple da

I have a page and I want to only display my name column, not age or anything else. Under that though I want to show the entire age column and so on, so I 开发者_开发百科don't want to make multiple database called I just want to make on big database called via: SELECT * FROM database Suggestion?


$sql = "SELECT name, age, blah, blah FROM table";
$result = mysql_query($sql) or die(mysql_error());

$names = $ages = $blahs = array();

while($row = mysql_fetch_assoc($result)) {
    $names[] = $row['name'];
    $ages[] = $row['age'];
    $blahs[] = $row['blah'];
}

... output names ...
... output ages ...
... output blahs ...


If you read the entire contents of that query into an array (you may want to read up on mysql_fetch_assoc() on php.net), you'll be able to output the contents in any way you please.


If I understand correctly you just want:

SELECT name FROM database;

Or in general:

SELECT column-name FROM table-name;


I have a page and I want to only display my name column, not age or anything else. Under that though I want to show the entire age column and so on

This statement seems counter-intuitive. You'll definitely want to clarify what you're trying to do, maybe with some examples. But to me it sounds like you want to display only a specific subset of columns. Not one column, but at the same time not all columns.

First of all, you don't have to display everything that comes back from your SELECT statement. You can do a SELECT * FROM table and only display the fields that are relevant.

More to the point, though, is that when you specify a column name then you're not limited to just one column. You can specify several, for example: SELECT name, age, some_other_column FROM table

When you say something like SELECT * FROM database I get a little concerned that you want to select everything from your entire database into memory. If this is your intent, please don't go that route. Relational databases are very good at optimizing queries, so there's nothing wrong with sending them lots of queries. That's not to say that you should have too many trips to the database, but having the entire application depend on only a single trip to the database is taking it a bit far. (If this wasn't your intent, no big deal. I just want to make sure.)

It also sounds like you may be trying to express the idea of a JOIN in your SELECT query, and are just having trouble articulating it. Are you trying to get data from multiple tables in a single trip to the database? If so, please show us your table structures. Another thing that relational databases are very good at doing is pivoting data between different tables to present different views of the data. And there may even be some structural changes you can make to your data model to improve that process.


If you have a table like to

table1
-----------
id integer
name varchar(30)
age integer
sex enum('male','female')
pretty enum('yes','no','mwha')

Instead of doing SELECT * FROM table1

You do

SELECT name FROM table1 
WHERE age BETWEEN 20 AND 30 
      AND sex = 'female' 
      AND pretty = 'yes'

If you want to know all the names per age you do

SELECT age, group_concat(name) as names 
FROM table1
WHERE age BETWEEN 20 AND 30 
      AND sex = 'female' 
      AND pretty = 'yes'
GROUP BY age

In php you then explode the names in the names field.

See:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
http://php.net/manual/en/function.explode.php

If you want to show the fields below one and other you can use the following code horror:

Coding horror

SELECT name AS col1 FROM table1 WHERE ....
UNION
SELECT '*****' as col1
UNION
SELECT age AS col1 FROM table1 WHERE ....
UNION
SELECT '*****' as col1
UNION
......

And loop through the results using the '*' as end of run markers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号