簡體   English   中英

從表中選擇所有類型為int和varchar的列

[英]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

檢查此鏈接: -http : //php.net/manual/zh/function.is-numeric.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM