doupu1949 2014-08-27 07:24
浏览 49

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

Below is code of database.php file.

     <?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);  
}  
 ?>

And now below is my code for display.php file.

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

So my question is how can i use FetchRow and FetchAll method in my display.php file.I am a beginner and trying to learn OOP with PHP.Help me as soon as possible.

Please check the below code and help me as my loop goes in infinite mode.

     <?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条回答 默认 最新

  • doulangbi6869 2014-08-27 07:29
    关注

    Create an instance of it and use it, for example:

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

    Now you can use it the way you tried already:

    $database->fetchRow();

    Another method would be, to make your class static so you can call it everywhere without having to instanciate it over and over again. This is very useful for database classes because you normally need them in your whole project. So you have to turn all your functions and attributes in your class static. Afterwards you can call your database class like this:

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

    More informations here

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

    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单