Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP SQL函数不';t返回有效的SQL数据类型_Php_Mysql - Fatal编程技术网

PHP SQL函数不';t返回有效的SQL数据类型

PHP SQL函数不';t返回有效的SQL数据类型,php,mysql,Php,Mysql,因此,我使用以下函数获取SQL数据库中某些字段的数据类型 $type = mysql_field_type($result, $i); $len = mysql_field_len($result, $i); 我需要使用这些数据类型来最终构造另一个表。问题是我从这些函数得到的返回不是有效的SQL数据类型,我得到的返回如下:string(25)、int(11)、string(25) Int是可以的,但string不是SQL数据类型,所以我需要找出哪些返回需要重新格式化为正确的数据类型,并使

因此,我使用以下函数获取SQL数据库中某些字段的数据类型

$type  = mysql_field_type($result, $i);
$len   = mysql_field_len($result, $i);
我需要使用这些数据类型来最终构造另一个表。问题是我从这些函数得到的返回不是有效的SQL数据类型,我得到的返回如下:string(25)、int(11)、string(25)


Int是可以的,但string不是SQL数据类型,所以我需要找出哪些返回需要重新格式化为正确的数据类型,并使用函数来实现这一点吗?还是我错过了另一种方法?

尝试运行如下查询:

SELECT column_type FROM information_schema.columns WHERE table_schema = 'databaseName' AND table_name = 'tableName' AND column_name = 'columnName';

尝试运行如下查询:

SELECT column_type FROM information_schema.columns WHERE table_schema = 'databaseName' AND table_name = 'tableName' AND column_name = 'columnName';

这是因为
mysql\u field\u len
返回模式长度(如果存在)

将获取数据库中指定的id字段长度

另外,
string
是对
mysql\u field\u type
的有效返回,不确定问题出在哪里

示例

<?php
mysql_connect("localhost", "mysql_username", "mysql_password");
mysql_select_db("mysql");
$result = mysql_query("SELECT * FROM func");
$fields = mysql_num_fields($result);
$rows   = mysql_num_rows($result);
$table  = mysql_field_table($result, 0);
echo "Your '" . $table . "' table has " . $fields . " fields and " . $rows . " record(s)\n";
echo "The table has the following fields:\n";
for ($i=0; $i < $fields; $i++) {
    $type  = mysql_field_type($result, $i);
    $name  = mysql_field_name($result, $i);
    $len   = mysql_field_len($result, $i);
    $flags = mysql_field_flags($result, $i);
    echo $type . " " . $name . " " . $len . " " . $flags . "\n";
}
mysql_free_result($result);
mysql_close();
?>

这是因为
mysql\u field\u len
返回架构长度(如果存在)

将获取数据库中指定的id字段长度

另外,
string
是对
mysql\u field\u type
的有效返回,不确定问题出在哪里

示例

<?php
mysql_connect("localhost", "mysql_username", "mysql_password");
mysql_select_db("mysql");
$result = mysql_query("SELECT * FROM func");
$fields = mysql_num_fields($result);
$rows   = mysql_num_rows($result);
$table  = mysql_field_table($result, 0);
echo "Your '" . $table . "' table has " . $fields . " fields and " . $rows . " record(s)\n";
echo "The table has the following fields:\n";
for ($i=0; $i < $fields; $i++) {
    $type  = mysql_field_type($result, $i);
    $name  = mysql_field_name($result, $i);
    $len   = mysql_field_len($result, $i);
    $flags = mysql_field_flags($result, $i);
    echo $type . " " . $name . " " . $len . " " . $flags . "\n";
}
mysql_free_result($result);
mysql_close();
?>

您将看到mysql\u field\u type()函数将按照(http://it2.php.net/mysql_field_type):

或许您可以尝试以下方式:

$table = "my_table";
$result = mysql_query("DESCRIBE TABLE `{$tabler}`");
while ($row = mysql_fetch_object($result)) {
    $field = $row->Field;
    $type = $row->Type;
}

希望对您有所帮助。

您将看到mysql\u field\u type()函数将根据返回以下数据(http://it2.php.net/mysql_field_type):

或许您可以尝试以下方式:

$table = "my_table";
$result = mysql_query("DESCRIBE TABLE `{$tabler}`");
while ($row = mysql_fetch_object($result)) {
    $field = $row->Field;
    $type = $row->Type;
}

希望这能有所帮助。

在mysql\u field\u len的描述中,哪里说它返回模式长度?我看到的描述是“返回指定字段的长度”。是的,
string
是对
mysql\u field\u type
的有效返回,但在mysql中不是有效的数据类型,正如OP在文章中所说。他需要使用现有列的数据类型来创建另一个表,而创建表时,
string
不是有效的MySQL数据类型。在
MySQL\u field\u len
的描述中,哪里说它返回架构长度?我看到的描述是“返回指定字段的长度”。是的,
string
是对
mysql\u field\u type
的有效返回,但在mysql中不是有效的数据类型,正如OP在文章中所说。他需要使用现有列的数据类型创建另一个表,创建表时,
string
不是有效的MySQL数据类型。我建议使用Travesty3解决方案-使用
INFORMATION\u SCHEMA.COLUMNS
和/或
INFORMATION\u SCHEMA.TABLES
我建议使用Travesty3解决方案-使用
INFORMATION\u SCHEMA.COLUMNS
和/或
INFORMATION\u SCHEMA.TABLES