Reputation: 1172
I want to know how I can get the datatype of each field in a SQL database using PHP; I want use this datatype for creating some docs in SOLR.
For Example:
if (datatype == int) then "[field_name] + _i".
So, if I have field bedrooms
and the datatype is integer
, in SOLR I'll save this field to be bedrooms_i
.
Upvotes: 0
Views: 378
Reputation: 3373
You can use mysql_fetch_field()
to retrieve metadata about fields.
mysql_connect('localhost','user','password');
mysql_select_db('database');
$sql = "SELECT * FROM your_table";
$result = mysql_query($sql);
$i = 0;
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo "<pre>".print_r($meta,TRUE)."</pre>";
$i++;
}
Here $meta->type
will give you the field type.
Upvotes: 2
Reputation: 5579
For the sake of argument, I'll assume MySQL.
SELECT COLUMN_NAME, DATA_TYPE, {ETC...}
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your_database' AND
TABLE_NAME = 'your_table'
Upvotes: 0
Reputation: 137398
In MySQL you can use the SHOW COLUMNS to get the name, data type, etc. of all fields/columns in a table.
Upvotes: 0