Rediger: Jeg lægger outputtet i en tabel, fordi jeg ikke kan sove...
Okay... se om det er det du vil...
Dette er en tabel, jeg lavede til et andet SO-spørgsmål:
mysql> describe user;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| User_ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Email | varchar(100) | YES | | NULL | |
| Name | varchar(100) | YES | | NULL | |
| Password | varchar(100) | YES | | NULL | |
| FB_ID | int(11) | YES | | NULL | |
| Total_Score | int(11) | YES | | 0 | |
| add_date | datetime | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
og fra DB:
mysql> select * from user limit 1;
+---------+-------+------+----------+-------+-------------+---------------------+
| User_ID | Email | Name | Password | FB_ID | Total_Score | add_date |
+---------+-------+------+----------+-------+-------------+---------------------+
| 1 | NULL | kim | NULL | NULL | 10 | 2013-11-03 23:04:08 |
+---------+-------+------+----------+-------+-------------+---------------------+
+
1 row in set (0.00 sec)
Og koden:
<?php
$mysqli = mysqli_connect("localhost", "root", "", "test");
// this came from http://php.net/manual/en/mysqli-result.fetch-field-direct.php
$mysql_data_type_hash = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
//252 is currently mapped to all text and blob types (MySQL 5.0.51a)
253=>'varchar',
254=>'char',
246=>'decimal'
);
// run the query...
$result = $mysqli->query("select * from user limit 1");
// get one row of data from the query results
$proceso = mysqli_fetch_assoc($result);
print "<table>
<tr>
<th>\$key</th>
<th>\$value</th>
<th>\$datatype</th>
<th>\$dt_str</th>
</tr> ";
// to count columns for fetch_field_direct()
$count = 0;
// foreach column in that row...
foreach ($proceso as $key => $value)
{
$datatype = $result->fetch_field_direct($count)->type;
$dt_str = $mysql_data_type_hash[$datatype];
$value = (empty($value)) ? 'null' : $value;
print "<tr>
<td>$key</td>
<td>$value</td>
<td class='right'>$datatype</td>
<td>$dt_str</td>
</tr> ";
$count++;
}
print "</table>";
mysqli_close($mysqli);
?>
<style>
/* this is css that you don't need but i was bored so i made it pretty...! */
table { font-family:Courier New;
border-color:#E5E8E3; border-style:solid; border-weight:1px; border-collapse:collapse;}
td,th { padding-left:5px; padding-right:5px; margin-right:20px;
border-color:#E5E8E3; border-style:solid; border-weight:1px; }
.right { text-align:right }
</style>
Så... for at præcisere...
Du kan bruge disse variabler i den foreach
at udlæse eller bruge informationen, som du vil:(Jeg bruger min første række af output, som bruger_id, som et eksempel)
-
$key
er kolonne-/feltnavnet (såsomuser_id
) -
$field_types[$key]
kommer fra$result->fetch_field_direct($i)->type
(såsom3
) -
$mysql_data_type_hash[$datatype]
er strengversionen af datatypen ved hjælp af$mysql_data_type_hash
array øverst i koden. Dette er ikke nødvendigt, men jeg inkluderede det, så dette eksempel er mere klart. (såsomint
) -
$proceso[$key] = $value =
er din værdi for denne iteration af foreach-sætningen (såsom1
)
Output:
$key $value $datatype $dt_str
User_ID 1 3 int
Email null 253 varchar
Name kim 253 varchar
Password null 253 varchar
FB_ID null 3 int
Total_Score 10 3 int
add_date 2013-11-03 23:04:08 12 datetime