[英]Choose all columns from the table with types int and varchar
我有一个表存储在testdb(数据库)中,我需要从中选择所有int和varchar类型的列。 我知道函数mysql_field_type,但是在php的第5版中不起作用。
<?php
header('Content-Type: text/html; charset=utf-8');
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$link = mysqli_connect($host , $user, $passw , 'testdb');
if(!$link){
echo "Connection failure";
}else{
echo "Connection success!";
}
$result = mysqli_query($link, 'SELECT * FROM `test_table`');
while ($row = $result->fetch_assoc()) {
$table[] = $row;
}
print_r ($table);
?>
尝试运行以下形式的查询:
select column_name
from Information_schema.columns
where table_name = 'test_table' and (data_type = 'int' or data_type = 'varchar');
这将返回test_table中所有int或varchar列的列表。 然后,您可以遍历它们,并通过PHP中的查询串联或在foreach上提取它们的值。
您可以为此使用fetch_field。 ( http://tr1.php.net/manual/tr/mysqli-result.fetch-field.php )
$cols = array();
while($field = $result->fetch_field()){
if($field->type == 3 || $field->type == 253){
$cols[] = $field->name;
}
}
print_r($cols);
由于您已经将值存储在$ table数组中,因此请在任何列上进行尝试
如果要选择列名,则查询应如下所示
从表名中选择列名
使用is_numeric function
foreach ($table as $new_array)
{
if(is_numeric($new_array))
{
$number[]=$new_array;
}
else{
$string[]=$new_array;
}
}
print_r($string);//see the result
print_r($number);//see the result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.