如何在其他php文件中访问数据库类的方法mysql_fetch_array?

时间:2014-08-27 07:24:49

标签: php

下面是database.php文件的代码。

     <?php

    class Database
{
var $con;
var $db;
var $result;
var $data;


public function connect($server="localhost",$uname="cccsql",$pword="ccc123",$db="trainees_jinal")
{
    try
    {
        $con=mysql_connect($server,$uname,$pword);
        if(!$con)
        {
            throw new Exception("Error: Cannot connect to the server");
            return;
        }

        $db=mysql_select_db($db,$con);
        if(!$db)
        {
            throw new Exception("Error: Cannot connect to the database");
            return;
        }
        return $this;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

public function insert($sql)
{
    try
    {
        $res=mysql_query($sql);
        if(!$res)
            throw new Exception("record insertion failed...");

        if($res)
        {
            return mysql_insert_id();    
        }

    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

public function update($sql)
{
    try
    {
        $res=mysql_query($sql);
        if(!$res)
            throw new Exception("record updation failed...");
        return $res;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}
public function delete($sql)
{
    try
    {
        $res=mysql_query($sql);
        if(!$res)
            throw new Exception("record deletion failed...");
        return $res;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

public function fetchAll($sql)
{
    try
    {
        $res=mysql_query($sql);
        if(!$res)
            throw new Exception("error on query execution record...");

        $arr=array();


        while($row=mysql_fetch_assoc($res))
        {
            array_push($arr,$row);

        }
        return $arr;

    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}
public function fetchRow($sql)
{
    try
    {
        $res=mysql_query($sql);
        if(!$res)
            throw new Exception("error on query execution record...");

        if($row=mysql_fetch_array($res))
        {
            return $row;
        }
        return NULL;            
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }

}
public function num_fields($result)
{
    return mysql_num_fields($result);
}
public function field_name($result,$i)
{
    return mysql_field_name($result,$i);
}


private function wrap_with_quotes($data)  
{  
    return sprintf('"%s"', $data);  
}  
 ?>

现在下面是我的display.php文件代码。

   <br/>
   <a href="index.php"><button>New Registration</button></a>

       <br><br><br/>
    <form method="post">
     <table width="" border="1" cellspacing="0" cellpadding="" align="center" >
            <tr>
             <th>Check</th>
             <th> ID</th>
             <th>Name</th>
             <th>Age</th>
             <th>Gender</th>
             <th>Contact</th>
             <th>Email</th>
             <th>UserName</th>
             <th>Password</th>
             <th>Billing Address </th>
             <th>Shipping Address </th>
             <th>Action</th>
        </tr>
        <?php

         require_once 'database.php';
          $database = new Database;
        $q="SELECT * FROM personalinfo INNER JOIN addrsinfo ON personalinfo.id=addrsinfo.id";


while($rows=$database->fetchRow($q))
{
?>
 <tr>
        <td align="center"><input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>"></td>
        <td ><? echo $rows['id']; ?></td>
        <td width="40%" nowrap="2"><? echo $rows['name'];?> </td>
       <td width="10%"><? echo $rows['age']; ?></td>
       <td width="20%"><? echo $rows['gender']; ?></td>
        <td width="30%"><? echo $rows['contact']; ?></td>
        <td width="30%"><? echo $rows['email']; ?></td>
        <td width="30%"><? echo $rows['uname']; ?></td>
        <td width="20%"><? echo $rows['paswrd']; ?></td>
        <td width="50%"><? echo $rows['billaddr']; ?></td>

         <td width="50%"><? echo $rows['shipaddr']; ?></td>
        <td><a href="edit.php?id=<?=$rows['id'] ?>">Edit</a></td>
   </tr>


   <?php  }?>
   </table>  


        <tr>
            <td>
            <input type="submit" name="submit" value="Multi Delete"> 
            </td>
        </tr>





      <?php

if(isset($_POST['submit']))
{
$checkbox[] = $_POST['checkbox'];

if(sizeof($checkbox[0])>1)
{
    $id=implode(",",$checkbox[0]);

    $query = "DELETE FROM personalinfo WHERE id IN($id)";
    $database->delete($query);
}
else
{

   $id=$checkbox[0][0];
   $query = "DELETE FROM personalinfo WHERE id='$id'";
   $database->delete($query);

}
echo"<script>window.location='display.php';</script>";
 }




if(isset($_POST['submit']))
{
$checkbox[] = $_POST['checkbox'];

if(sizeof($checkbox[0])>1)
{
    $id=implode(",",$checkbox[0]);

    $query = "DELETE FROM addrsinfo WHERE id IN($id)";
     $database->delete($query); 
}
else
{

   $id=$checkbox[0][0];
   $query = "DELETE FROM addrsinfo WHERE id='$id'";
    $database->delete($query); 

}
echo"<script>window.location='display.php';</script>";
}

 ?>


</form>      

      </table>  

所以我的问题是如何在display.php文件中使用FetchRow和FetchAll方法。我是初学者并尝试用PHP学习OOP。尽快帮助我。

请检查以下代码并帮助我,因为我的循环进入无限模式。

     <?php
     require_once 'database.php';
     $database = new Database;
     $database->connect();
     $q="SELECT * FROM personalinfo INNER JOIN addrsinfo ON  personalinfo.id=addrsinfo.id";
      ?>
      <?php
while($rows=$database->fetchRow($q))
{
?>
<tr>
        <td align="center"><input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>"></td>
        <td ><? echo $rows['id']; ?></td>
        <td width="40%" nowrap="2"><? echo $rows['name'];?> </td>
       <td width="10%"><? echo $rows['age']; ?></td>
       <td width="20%"><? echo $rows['gender']; ?></td>
        <td width="30%"><? echo $rows['contact']; ?></td>
        <td width="30%"><? echo $rows['email']; ?></td>
        <td width="30%"><? echo $rows['uname']; ?></td>
        <td width="20%"><? echo $rows['paswrd']; ?></td>
        <td width="50%"><? echo $rows['billaddr']; ?></td>

         <td width="50%"><? echo $rows['shipaddr']; ?></td>

   </tr>


   <?php  }?>

3 个答案:

答案 0 :(得分:1)

创建一个实例并使用它,例如:

include "database.php";
$database = new Database();

现在您可以按照您尝试的方式使用它:

$database->fetchRow();

另一种方法是,让您的课程static,以便您可以随时随地调用,而不得不一遍又一遍地实现它。这对数据库类非常有用,因为您通常在整个项目中需要它们。因此,您必须将类中的所有函数和属性设置为静态。之后,您可以像这样调用您的数据库类:

include "database.php";
Database::fetchRow();

此处有更多信息

http://php.net/manual/de/language.oop5.php

答案 1 :(得分:0)

在使用mysql fetch查询之前先连接数据库。将以下行添加到$database = new Database;

$database->connect();

答案 2 :(得分:0)

你不能在那段代码上使用..因为fetchall函数会返回一个数组变量..你必须使用“for”循环而不是while .. 创建函数来计算reccord的行号。 然后循环每个数组