问题
Given the table and names of some columns, I have the following Information schema select query.
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA` = 'm_college'
AND `TABLE_NAME` = 'm_fee'
AND COLUMN_NAME NOT IN ('id', 'classid', 'semid')
But this select does not give me the rows value for each unknown column I select. All I got is names of the unknown columns. Is it possible to select the values of the rows so that I can have column as key and rows as value pair in my php script? I need to insert the column names and row values in other table. Please help or suggest.
回答1:
Below is a very quick attempt at showing an intersect table.
This allows you to have a fixed structure and add fee types on the fly.
CREATE TABLE IF NOT EXISTS `mz_fee222` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`classid` int(11) NOT NULL,
`semid` int(11) NOT NULL,
`batch` year(4) NOT NULL,
`session` int(11) NOT NULL
);
create table fee_type
( fee_id int auto_increment primary key,
description varchar(100)
);
insert fee_type (description) values ('exam'),('marksheet'),('admitcard'),('centre'),('practical'); -- etc as you go
create table mz_fee_intersect
( -- here is where the fee amount is stored. Flexible.
id int auto_increment primary key,
mz_id int not null, -- this is the id from my_fee222 table
fee_id int not null, -- this is the fee_id from fee_type table
fee int not null, -- here is the actual fee, like 100 bucks
-- FK's below (not shown):
--
unique key (mz_id,fee_id) -- forbids duplicates
);
回答2:
SELECT * FROM table_name
selects all of the columns and their values from table_name
. What you are using selects all of the column names from the schema table (which does not contain the information in your table_name
).
You can use SELECT * FROM table_name
in PHP and just use mysqli_fetch_assoc
to get an associative array which is basically a key => value
array where the key
is the column name and the value
is the value for that column in the given row in the array.
Pulled Directory from the PHP docs (http://php.net/manual/en/mysqli-result.fetch-assoc.php) because you wanted an example:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM table_name";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
回答3:
Ok I cannot think why you have a table that gets columns added dynamically other than the obvious but here is a suggestion that I hope helps
If you run a DESCRIBE tablename
and collect the results you get a result set something like this.
Field Type Null Key Default Extra
id int(10) unsigned NO PRI NULL auto_increment
parent_id int(11) NO MUL 0
lft int(11) NO MUL 0
rgt int(11) NO 0
level int(10) unsigned NO NULL
name varchar(50) NO UNI NULL
title varchar(100) NO NULL
rules varchar(5120) NO NULL
If you load this into an array, and then drop the Rows you dont want to select in the later query, you get their column names (in Field column) and even their data types if you want them.
You can then use this array, to build the field list for your column specific query and you also know what to call them when you are processing any results.
I hope this helps.
来源:https://stackoverflow.com/questions/32096145/how-to-select-unknown-number-of-columns-in-mysql