【问题标题】:I'm trying to Display all rows of database using oop in php and can't get it work我正在尝试在 php 中使用 oop 显示所有数据库行,但无法正常工作
【发布时间】:2014-07-16 06:28:28
【问题描述】:

我在数据库中有一个名为“产品”的表,它保存了产品的所有详细信息...... 我想使用类显示产品表的所有行 我已经完成了编码,但没有得到想要的结果......希望尽快得到帮助 这是我的 database.php 包含数据库类

<?php
class Database {

    var $host="localhost";
    var $username="cccsql";    
    Var $password="ccc123";
    var $database="trainees_pankaj";
    var $fr_query;
    var $row= array() ;


public function connect() 
{
    $conn= mysql_connect($this->host,$this->username,$this->password);
    if(! $conn )
    {
    die('Could not connect: ' . mysql_error());
    }
}

public function db() 
{
    $conn_db = mysql_select_db($this->database);
    if(! $conn_db )
    {
        echo 'Could Not Connect the Database';
    }
}

public function insert($sql)
{
 $run = mysql_query($sql) ;

}

public function delete($del_sql) 
{
    $run = mysql_query($del_sql) ;

}

public function update($up_sql) 
{
    $run = mysql_query($up_sql) ;

}

public function fetchRow($fr) 
{
        $run = mysql_query($fr) ;
        $row = mysql_fetch_array($run);
        $ans = $row[0]." ". $row[1];
        return $ans;
}

function fetchAll($fr_query) 
{
       $run = mysql_query($fr_query);

       $data = array() ; 
       while($row = mysql_fetch_array($run)){
       $data[] = $row ; 
       } 
       return $data;   
}
}

?>

这是我的 index.php 页面,用于显示所有行...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<title>Task 1</title>
 <link rel="stylesheet" type="text/css" href="css/mypo-list.css">
 </head>
 <body>
<div class="container">
    <h4 align="center">Product Detail</h4> 
    <br>
    <a class="add_but" href="add_product.php"><button>Add More Product</button></a>
<?php
include ("database.php");

$n=new Database();
$n->connect();
$n->db();

$fr_query = "SELECT * FROM product";

 $m= new Database();

$reach = array();
$m->fetchAll($fr_query);
$reach = (array)$m;

print_r('<table class="table_view"><tr><td class="tb_head">Product Id</td><td    class="tb_head">Product Name</td><td class="tb_head">Product Description</td><td class="tb_head">sku</td><td class="tb_head">Price</td><td class="tb_head">Quantity</td><td class="tb_head">Status</td><td class="tb_head">Created At</td><td class="tb_head">Updated At</td><td class="tb_head">Option</td></tr>');
while ($reach)
{
  echo '<tr>'; 

  $cr_date = date('M j Y g:i A', strtotime($reach['created_at'])); 
  $up_date = date('M j Y g:i A', strtotime($reach['updated_at']));
  $test_date = 'Nov 30 -0001 12:00 AM';
  if ($up_date == $test_date)
  {
      $set_up_date = 'NULL';
  }
  else {
      $set_up_date = $up_date;
  }
  $par_field = $reach['product_id'];
  $opt = '<td><a class="opt_but" href="edit.php?id='.$par_field.'">Edit</a>&nbsp&nbsp&nbsp&nbsp<a class="opt_but" href="delete.php?id='.$par_field.'">Delete</a></td>';
  echo '<td>'.$reach['product_id'].'</td>'.'<td>'.$reach['name'].'</td>'.'<td>'.$reach['description'].'</td>'.'<td>'.$reach['sku'].'</td>'.'<td>'.$reach['price'].'</td>'.'<td>'.$reach['quantity'].'</td>'.'<td>'.$reach['status'].'</td>'.'<td>'.$cr_date.'</td>'.'<td>'.$set_up_date.'</td>'.$opt;
         print_r('</tr>');  
} 
?>

</div>
</body>
</html>

【问题讨论】:

  • 停止使用已弃用的mysql_*函数;改用 PDO / MySQLi;另外,您的insert()update()delete() 函数是多余的,毫无意义。
  • 如何使用PDO/MySQLi ??
  • 目前的结果是什么?
  • 怎么会急呢?你接受了一份你没有技能的合同?你应该承担你的行为。

标签: php mysql database oop fetchall


【解决方案1】:

这很简单:

$fr_query = "SELECT * FROM product";

 $m= new Database();

$reach = array(); // anyway you don't need to assign value twice
$reach = $m->fetchAll($fr_query);
//$reach = (array)$m; this is mistake

你最好使用foreach:

foreach ($reach as $reachRow) 
{
    // code here
}

最后你的代码看起来像这样:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<title>Task 1</title>
 <link rel="stylesheet" type="text/css" href="css/mypo-list.css">
 </head>
 <body>
<div class="container">
    <h4 align="center">Product Detail</h4> 
    <br>
    <a class="add_but" href="add_product.php"><button>Add More Product</button></a>
<?php
include ("database.php");

$n=new Database();
$n->connect();
$n->db();

$fr_query = "SELECT * FROM product";

 $m= new Database();

$reaches = $m->fetchAll($fr_query);

print_r('<table class="table_view"><tr><td class="tb_head">Product Id</td><td    class="tb_head">Product Name</td><td class="tb_head">Product Description</td><td class="tb_head">sku</td><td class="tb_head">Price</td><td class="tb_head">Quantity</td><td class="tb_head">Status</td><td class="tb_head">Created At</td><td class="tb_head">Updated At</td><td class="tb_head">Option</td></tr>');
foreach ($reaches as $reach) 
{
  echo '<tr>'; 

  $cr_date = date('M j Y g:i A', strtotime($reach['created_at'])); 
  $up_date = date('M j Y g:i A', strtotime($reach['updated_at']));
  $test_date = 'Nov 30 -0001 12:00 AM';
  if ($up_date == $test_date)
  {
      $set_up_date = 'NULL';
  }
  else {
      $set_up_date = $up_date;
  }
  $par_field = $reach['product_id'];
  $opt = '<td><a class="opt_but" href="edit.php?id='.$par_field.'">Edit</a>&nbsp&nbsp&nbsp&nbsp<a class="opt_but" href="delete.php?id='.$par_field.'">Delete</a></td>';
  echo '<td>'.$reach['product_id'].'</td>'.'<td>'.$reach['name'].'</td>'.'<td>'.$reach['description'].'</td>'.'<td>'.$reach['sku'].'</td>'.'<td>'.$reach['price'].'</td>'.'<td>'.$reach['quantity'].'</td>'.'<td>'.$reach['status'].'</td>'.'<td>'.$cr_date.'</td>'.'<td>'.$set_up_date.'</td>'.$opt;
         print_r('</tr>');  
} 
?>

</div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 1970-01-01
    • 2017-11-21
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多