如何从DB获取此值作为字符串或int?
PHPMyAdmin http://img200.imageshack.us/img200/6388/gr55.jpg
我试过这个:
$var = $this->db->query("SELECT SUBSTRING(`category_id`, 1, 3)
AS `category_id` FROM " . DB_PREFIX . "category
WHERE merlion_id = 'Q1'
ORDER BY category_id ASC");
然后我用var_dump
检查了它并收到了:
var_dump($var);
object(stdClass)#26 (3) { ["row"]=> array(1) { ["category_id"]=> string(3) "859" } ["rows"]=> array(1) { [0]=> array(1) { ["category_id"]=> string(3) "859" } } ["num_rows"]=> int(1) }
答案 0 :(得分:0)
这个怎么样:
echo $var->row['category_id'];
您还可以简化查询:
$var = $this->db->query("SELECT `category_id`
AS `category_id` FROM `" . DB_PREFIX . "category`
WHERE merlion_id = 'Q1'
LIMIT 1");
答案 1 :(得分:0)
无需执行SUBSTRING
:
$var = $this->db->query("`category_id`FROM " . DB_PREFIX . "category
WHERE merlion_id = 'Q1'
ORDER BY category_id ASC");
然后,你拥有它,你没有“转换”对象,你从中获取了你需要的东西:
$category_id = $var->row["category_id"];
我建议您在继续使用数据库驱动的应用程序之前深入了解基本的PHP。这都是微不足道的事情。有很多很好的教程,也有关于PHP面向对象的编程。最值得注意的是PHP documentation site。