我试图在php中使用oop显示所有数据库行,但无法使其正常工作

时间:2014-07-16 06:28:29

标签: php mysql database oop fetchall

我在数据库中有一个名为" product"这保存了产品的所有细节...... 我想用class显示产品表的所有行 我已经完成了编码,但没有得到理想的结果......希望尽快得到帮助 这是我的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>

1 个答案:

答案 0 :(得分:0)

这很容易:

$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>