【问题标题】:PHP MVC with CRUD带有 CRUD 的 PHP MVC
【发布时间】:2014-07-22 18:14:17
【问题描述】:

我在 PHP 上从头开始编写了一个简单的 MVC,为什么当我尝试 print_r 收到的数据时收到此消息 Resource id #7

这是我的代码:

控制器:

$data = $this->query->db_select();
print_r($data);
include 'view/show_all.php';

模型/query.php

 public function db_select(){
    $test = $this->query_db->select("SELECT * FROM sis");
    return $test;
} 

模型/connect.php

<?php
/**
* DB Connect PDO
*/
class Connect
{
    private $DBCOnnect;
    public function __construct(){
        $this->DBConnect = mysql_connect("localhost", "root", "");
        mysql_select_db("activity");
    }
    public function insert($query) {
        return mysql_query($query);
    }
    public function delete($query) {
        return mysql_query($query);
    }
    public function select($query) {
        return mysql_query($query);
    }
    public function update($query) {
        return mysql_query($query);
    }
    public function close() {
        mysql_close($this->DBConnect);
    }
}
?>

【问题讨论】:

  • -1: 一个不使用PDODB Connect PDO?自 PHP 5.5 起,mysql_* 函数也被弃用

标签: php mysql crud


【解决方案1】:

mysql_query() 的返回值是一个资源。你不能只是print_r() 它。您必须循环从资源中获取行。

无论如何,您都不应该使用已弃用的 mysql 扩展。使用 PDO。

【讨论】:

  • 如何实现 PDO?
【解决方案2】:

mysql_query 只返回查询资源,不返回获取的数据。

您必须添加对mysql_fetch_array 或类似名称的呼叫才能获得您想要的。

$data = $this->query->db_select();
while ($row = mysql_fetch_array($data) {
    print_r($row);
}
include 'view/show_all.php';

【讨论】:

  • 我该如何使用它?抱歉,还是新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多