2014-10-18 27 views
2

我有一个存储在testdb(数据库)的表,我需要从中选择int和varchar类型的所有列。我知道函数mysql_field_type,但它不适用于第5版的PHP。从类型为int和varchar的表中选择所有列

<?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); 

?> 

回答

0

既然你已经存储在$表阵的值试试这个任意列

,如果你想选择列名然后将查询应该是这样的

选择列名从TABLE_NAME

使用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 

检查此链接: - http://php.net/manual/en/function.is-numeric.php

3

尝试运行形式的查询:

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提取它们的值。