【问题标题】:MySQL query to get enum values to write them into the dropdownMySQL查询以获取枚举值以将它们写入下拉列表
【发布时间】:2014-02-03 16:33:22
【问题描述】:

我正在尝试将枚举的值放入<option value="$enum">$enum</option>,但到目前为止没有任何运气。

我真的不能说为什么,我得到的错误是“Catchable fatal error: Object of class PDOStatement could not be convert to string in C:\xampp\htdocs\sp-admin\form.php on line 58”

第 58 行是$result = str_replace(array("enum('", "')", "''"), array('', '', "'"), $result);

这是我的 php

$query = "SELECT column_type FROM information_schema.columns
WHERE table_name = 'files' AND column_name = 'cat_page_pos'";
$result = $db->prepare($query);
    $result = str_replace(array("enum('", "')", "''"), array('', '', "'"), $result);
    $arr = explode("','", $result);
    return $arr;

请在这里给我提示

提前致谢

【问题讨论】:

  • 请提供 $result 的打印件。
  • 我什至无法尝试并开始弄清楚您 str_replace 正在尝试做什么。您能解释一下您要替换的是什么以及用什么替换吗?单引号和双引号的数量让我脑残。
  • @TheHumbleRat 这是我的打印 PDOStatement 对象([queryString] => SELECT column_type FROM information_schema.columns WHERE table_name = 'files' AND column_name = 'cat_page_pos')
  • 不要使用information_schema.columns,尝试使用速度更快的DESC table_name

标签: php mysql enums


【解决方案1】:

您的 $result 对象不是您期望的结果,而是要从中获取结果的 PDO 语句。

在您致电$db->prepare 后尝试这样的事情:

//perform SQL query on DB , since it has only be prepared
$result->execute();
//retrieve results from execution, here you obviously expect to get only one result
$data=$result->fetch();

$coltype=$data[0];

然后你会找到你要处理的字符串($coltype 变量中带有“enum('xxx','yyy')”)

【讨论】:

  • 感谢您的提示,这是我现在拥有的 Array ( [column_type] => enum('0','1','2','3','4','5 ') [0] => 枚举('0','1','2','3','4','5') )
  • 所以现在我将清理返回的结果并以我需要的方式使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 2011-11-27
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
相关资源
最近更新 更多