使用PHP连接类到MySQL数据库,插入,更新,删除

时间:2014-04-18 17:51:37

标签: php mysql

我即将开始一个新项目,使用MySQL,javascript等在PHP中构建一个网站...我的问题是我可以创建一个“使用PHP连接MySQL数据库”一次,并且每次我都调用该类想要插入,更新或删除某些内容。我是php的新手,所以简单一点,你可以给我发送示例代码或链接到教程。在我想要建立连接的那一刻,我将连接放在php的顶部,但是在很多页面上都有很多重复的代码。

category.php

<?php

    // connect to mysql db
    $con = mysqli_connect("localhost", "root", "");

    // use a mysql database
    mysqli_select_db($con, "gaa2013");

    // run a sqql query
    $result = mysqli_query($con, "select * from categories");

    print("<form method='POST' action=\"home.php?page=category\">");

        print("<select name = 'cat'>");
        print("<option selected=\"selected\" style=\"background-color: blue\">All</Option>");
            //print fields from each row
        while($row = mysqli_fetch_array($result)){
            $curr = $row['description'];
                print("<option value='$curr'>$curr</option>");

        }
        print("</select>");
            print("<input type=\"submit\" name=\"submit\" value=\"Select a Category\">");
        print("<hr>");

more code....

的login.php

//If there are input validations, redirect back to the login form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: index.php");
    exit();
}

// connect to mysql db
$con = mysqli_connect("localhost", "root", "");
// use a mysql database
mysqli_select_db($con, "gaa2013");
// run a sqql query
$result = mysqli_query($con,"select * from users WHERE username='$username'");
//$num_rows = mysql_num_rows($result);
    //Check whether the query was successful or not
if($result) {
    while($row = mysqli_fetch_array($result)){
        //echo "$num_rows Rows\n";
        $id = $row['id'];
        $userp = $row['username'];
        $pass = $row['password'];
        $_SESSION['SESS_MEMBER_ID'] = $id;
        $_SESSION['SESS_USERNAME'] = $userp;
        $_SESSION['SESS_PASSWORD'] = $pass;
        session_write_close();
        header("location: home.php");
        exit();
    }
more code.....

提前致谢

GMAN

1 个答案:

答案 0 :(得分:1)

免责声明:这只是一个介绍。我不建议经常使用。

你的目标是避免这种情况:

// connect to mysql db
$con = mysqli_connect("localhost", "root", "");

// use a mysql database
mysqli_select_db($con, "gaa2013");

......每次你想建立联系。

为此,一种方法可以是设计模式:单身人士。

Check its explanation out here.

现在:

class Database {
    private static $connection = null;

    public static function getInstance()
    {
        static $connection = null;
        if (null === $connection) {
            $connection = new Database();
        }
        return $connection;
    }

    /**
     * Protected constructor to prevent creating a new instance of the
     * *Singleton* via the `new` operator from outside of this class.
     */
    protected function __construct()
    {
        // connect to mysql db
        $con = mysqli_connect("localhost", "root", "");

        // use a mysql database
        mysqli_select_db($con, "gaa2013");

        return $con;
    }
}

然后,您将获得并运行如下数据库连接:

// Get DB connection
$connection = Database::getInstance();
$result = mysqli_query($connection, "select * from categories");

如何运作?

  1. 您使用Database :: getInstance();
  2. 请求新的数据库连接
  3. Database :: getInstance检查它是否为null,因此尚未请求连接。如果是这样,构建一个。否则,使用已打开的连接。
  4. 数据库构造函数在Database对象外部不可用,因此您无法请求新的Database();来自外面。
  5. 但是,我想让你知道,现在讨论Singleton模式是否合适。我不认为你应该关注它,只要你在学习。 我建议你自己玩这些技术:测试,尝试,看看会发生什么,如果你觉得它是对的。