从mysql表中获取Data_type

时间:2016-08-09 07:27:21

标签: mysql wordpress

我使用此代码:

select data_type from information_schema.columns where table_name = '$tableName' and column_name = '$column_name'

但在echo之后,它会返回1。但是当我在phpmyadmin sql中运行它时,它会返回data_type列。

我想要data_type的Mysql Table专栏(我使用Wordpress)。

7 个答案:

答案 0 :(得分:3)

在wordpress中试试这个。您应该使用get_var()get_row()方法。

  

$ wpdb-> query()仅返回(int | false)行数   受影响/选中或错误时出现错误

$dataType = $wpdb->get_var("select data_type from information_schema.columns where table_name = '$tableName' and column_name = '$column_name' ");        
echo $dataType;exit;

已编辑:

我曾在当地尝试过。您应该尝试设置这两个变量$tableName='wp_posts'; $column_name='post_title';,如下所示:

$tableName='wp_posts';
$column_name='post_title';
$data = $wpdb->get_var("select data_type from information_schema.columns where table_name = '$tableName' and column_name = '$column_name'"); 
echo $data;

输出

mediumtext

答案 1 :(得分:3)

Apparently too many tools (PHP / PhpAdmin) are getting in the way of going straight to the answer. If you are just looking for the answer, use the mysql commandline tool:

mysql> SELECT data_type FROM information_schema.columns
    ->     WHERE column_name = 'id'
    ->       AND table_schema = 'try'
    ->       AND table_name = 'a';
+-----------+
| data_type |
+-----------+
| int       |
+-----------+
1 row in set (0.01 sec)

If you do need to use the result in PHP, the treat it like any other SELECT -- use the appropriate API calls in mysqli_* or PDO to run the SELECT, then fetch the rows (one row in this case) and look at the columns (one column in this case).

Note that you should include the table_schema = ... in case there is another database with the same table_name and column_name.

答案 2 :(得分:2)

Wordpress中$tableName$column_name的值不是您所期望的值。

  

我使用此代码:

select data_type from information_schema.columns where table_name = '$tableName' and column_name = '$column_name'
     

但是在echo之后,它返回1.但是当我在phpmyadmin sql中运行它时,它返回了列的data_type。

因为当您在phpmyadmin中运行它时,您将明确替换$tableName$column_name的值。无论你明确地替换什么值,你期望$tableName$column_name在你的Wordpress中。但它们并不是你所期望的那样。您可以通过在Wordpress中明确设置值来发现这一点。

select data_type from information_schema.columns where table_name = 'x' and column_name = 'y'

您的问题中没有足够的信息来确定究竟出了什么问题。你所展示的只是代码的一部分。尽管如此,调试过程的下一步将是显式设置变量,然后从错误点向后工作。

$tableName = 'x';
$column_name = 'y';
// ...
select data_type from information_schema.columns where table_name = '$tableName' and column_name = '$column_name'

向后调试你会发现错误。

答案 3 :(得分:2)

  

您可以简单地获取如下数据类型。在哪里你喜欢。您可以通过更改" $ tableName"的值来更改不同的data_type;和" $ column_name"以下

<?php
global $wpdb;$prefix=$wpdb->prefix;
$tableName=$prefix.'posts';
$column_name='post_title';
echo $sql="SELECT data_type FROM information_schema.columns WHERE table_name = '$tableName' AND column_name = '$column_name' ";
  $dataType = $wpdb->get_var($sql);        
echo $dataType;?>

答案 4 :(得分:1)

它打印一个,因为它是返回值 - 这意味着存在错误。您正在尝试从字符串(您的查询)而不是从实际结果对象中获取结果集。

这将使它发挥作用:

$tableName = mysqli_real_escape_string($tableName);
$columnName = mysqli_real_escape_string($tableName);
$query = "select data_type ";
$query .= "from information_schema.columns ";
$query .= "where table_name = '$tableName' ";
$query .= "and column_name = '$column_name' ";
$vv = $wpdb->query($query); 
$row = mysqli_fetch_row($vv); 

在你的评论中,你说这是你的代码声明:

$query = "select data_type ";
$query .= "from information_schema.columns ";
$query .= "where table_name = '$tableName' ";
$query .= "and column_name = '$column_name' ";
$vv = $wpdb->query($query); 
$x = mysql_fetch_field ( $query ); 
echo $x; 
var_dump($x);

答案 5 :(得分:1)

$ sql =&#34; SELECT data_type FROM information_schema.columns WHERE table_name =&#39; $ tableName&#39; AND column_name =&#39; $ column_name&#39; &#34 ;;

答案 6 :(得分:0)

这是正确的,我也用过它。 $ sql =“SELECT data_type FROM information_schema.columns WHERE table_name ='$ tableName'AND column_name ='$ column_name'”;