mysql到mysqli类的函数转换

时间:2016-07-12 07:43:28

标签: php mysql mysqli

目前我在functions.php

中有以下类的mysql函数结构

constants.php包含数据库名称,用户密码等。

我想将此课程用作 mysqli

网上有很多课程示例。但我无法做到......

如果我可以将其转换为mysqli,我可以节省很多时间在其他所有页面上更改班级名称等。

我在 functions.php 文件中提供示例代码。如何将其转换为mysqli?

我已经尝试Mysqli Converter但它对我帮助不大......

<?
ob_start();
include_once("constants.php");
//create class 
Class bsq
{

function connect_db()
  {
    mysql_connect(HST,USR,PWD) or die(mysql_error()); //die("Failed to Connecting MYSQL");
    mysql_select_db(DBN) or die(mysql_error()); //die("Failed to connect database");
  }

function webdreamselect($table,$where,$start,$end,$orderby,$orderbyvalue)
  {

$sql="select * from ".$table."";
        if($where!="") {
        $sql.=" where ".$where."";
        }
        if($orderby!="" && $orderbyvalue!="" ) {
            $sql.=" order by ".$orderby." ".$orderbyvalue."";
        }
        if($end > 0 ) {

        $sql.=" limit ".$start.",".$end." ";
        }
        //echo $sql;
    $seldata=@mysql_query($sql);
    return $seldata;
 }

function webdreamupdate($tbl, $sf, $sv, $wf, $wv, $prn)
    {
    $query.=" UPDATE ".$tbl." SET " ;            

    /* Here updating fields and values are composed */

    if(is_array($sf))
    {
        if(sizeof($sf) > 0)
        {
            for($j=0; $j<sizeof($sf); $j++)
            {           
                $update_vars.= " $sf[$j] = '$sv[$j]' ";

                if($j<sizeof($sf)-1)
                $update_vars .= ", "; 
            }
        }
    }
    else
    {
        $update_vars.= " $sf = '$sv' ";
    }

    $query.= $update_vars;

    /*Here condition is created*/

    if(is_array($wf))
    {
        if(sizeof($wf) > 0)
        {
            for($k=0; $k<sizeof($wf); $k++)
            {           
                $condition.= " $wf[$k] = '$wv[$k]' ";

                if($k<sizeof($wf)-1)
                $condition .= " and "; 
            }
        }
    }   
    else
    {
        if($wf)
            $condition = $wf." = '$wv' ";
        else
            $condition="1";
    }
    $query.= " WHERE $condition ";
    if($prn==1)
    {
        //echo $query;
    }
        //echo $query;
    $result = @mysql_query($query) or die(mysql_error());
    return $result;
}

2 个答案:

答案 0 :(得分:1)

  

如何将其转换为mysqli?

直截了当地回答:

  1. 打开manual page for mysqli并阅读。
  2. 创建一个测试脚本并练习一段时间运行各种查询,以熟悉mysqli。
  3. 打开您喜欢的代码编辑器,加载此文件并设置为重写。
  4. 完成后,尝试测试一下。
  5. 如果您在此过程中遇到某些特定错误,欢迎您使用Google进行操作,如果没有运气,请在Stack Overflow上提问。

答案 1 :(得分:-1)

像这样使用你的functions.php(所有mysql_都替换为mysqli _)

<?
ob_start();
include_once("constants.php");
//create class 
Class bsq
{

function connect_db()
  {
    mysqli_connect(HST,USR,PWD) or die(mysqli_error()); //die("Failed to Connecting MYSQL");
    mysqli_select_db(DBN) or die(mysqli_error()); //die("Failed to connect database");
  }

function webdreamselect($table,$where,$start,$end,$orderby,$orderbyvalue)
  {

$sql="select * from ".$table."";
        if($where!="") {
        $sql.=" where ".$where."";
        }
        if($orderby!="" && $orderbyvalue!="" ) {
            $sql.=" order by ".$orderby." ".$orderbyvalue."";
        }
        if($end > 0 ) {

        $sql.=" limit ".$start.",".$end." ";
        }
        //echo $sql;
    $seldata=@mysqli_query($sql);
    return $seldata;
 }

function webdreamupdate($tbl, $sf, $sv, $wf, $wv, $prn)
    {
    $query.=" UPDATE ".$tbl." SET " ;            

    /* Here updating fields and values are composed */

    if(is_array($sf))
    {
        if(sizeof($sf) > 0)
        {
            for($j=0; $j<sizeof($sf); $j++)
            {           
                $update_vars.= " $sf[$j] = '$sv[$j]' ";

                if($j<sizeof($sf)-1)
                $update_vars .= ", "; 
            }
        }
    }
    else
    {
        $update_vars.= " $sf = '$sv' ";
    }

    $query.= $update_vars;

    /*Here condition is created*/

    if(is_array($wf))
    {
        if(sizeof($wf) > 0)
        {
            for($k=0; $k<sizeof($wf); $k++)
            {           
                $condition.= " $wf[$k] = '$wv[$k]' ";

                if($k<sizeof($wf)-1)
                $condition .= " and "; 
            }
        }
    }   
    else
    {
        if($wf)
            $condition = $wf." = '$wv' ";
        else
            $condition="1";
    }
    $query.= " WHERE $condition ";
    if($prn==1)
    {
        //echo $query;
    }
        //echo $query;
    $result = @mysqli_query($query) or die(mysqli_error());
    return $result;
}