1

在 MySQL 上连接 PHP 应用程序的最佳方法是什么。

到目前为止,我有以下连接类。

class Connection{
    private static $server = "127.0.0.1";
    private static $catalog = "schemadb";
    private static $username = "rootuser";
    private static $password = "password";
    public static $current = null;

    public static function Open(){
        self::$current = mysqli_init();
        if(!self::$current){
            die("Failed to initialize connection");
        }
        if(!self::$current->real_connect(self::$server,self::$username,self::$password,self::$catalog)){
            die("Cannot connect to server");
        }
        return self::$current;
    }
    public static function Close(){
        self::$current->close();
    }
}

我也有

abstract class abstractDAO
{
    protected function getConnection()
    {
        $mysqli = new mysqli("127.0.0.1","rootuser","password","schemadb");
        return $mysqli;
    }

}

或者是否有任何其他最佳方法可以在 MySQL 上连接 PHP 应用程序。请指教谢谢。。

4

4 回答 4

3

您可以尝试使用 PDO 对象:

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

查看PHP PDO 文档页面

于 2012-11-22T08:04:08.197 回答
0

尝试使用 php 框架,如 codeigniter、Yii、cake php。如果你在这个框架中实现任何一个,不需要编写 php mysql 查询它会自动生成。您只需要输入您的数据库配置,如下所示

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'sample';
于 2013-06-05T13:17:41.007 回答
0

您可以使用 PDO 通过数据连接,这是一个示例

<?php
$servername = "localhost";
$username = "root";
$password = "nopass";

try {
    $conn = new PDO("mysql:host=$servername;dbname=wireframe", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 

    $stmt = $conn->prepare("SELECT * FROM todolist"); 
    $stmt->execute();

    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>

<table border="1" align="center">
<tr>
  <th>name</th>
  <th>type</th>
  <th>status</th>
</tr>




<?php

foreach($stmt->fetchAll() as $k=>$v){
     echo
   "<tr>
    <td>{$v['name']}</td>
    <td>{$v['type']}</td>
    <td>{$v['status']}</td>
   </tr>\n";

}


?>
</table>
</body>
</html>
于 2019-08-27T18:48:50.050 回答
-4

试试这个

<?php

  $user = $_POST["username"];//if this doesnt work try the next line
  $user = (isset($_POST["username"]) ? $_POST["username"] : "");

  $host = "localhost";//mysql password
  $username = "";//mysql username
  $password = "";//mysql password
  $db_name = "database";//database name
  $tbl_name ="test";//table name
  //make the connection
  $con = mysql_connect("$host","username","password")or die("Could not connect.");
  $conn = mysql_select_db("$db_name")or die("Could not select database.");

  $sql = "SELECT * FROM $tbl_name WHERE username='$username'";
  //query mysql
  $result = mysql_query($sql);

  if($result){
  //if it works show this

  }else{

  //if it doesnt work show this

  }

?>

我尝试了很多次来建立与数据库的连接,我终于找到了一个。

于 2013-01-05T00:26:58.967 回答