MySQL connects to PHP

What is the best way to connect a PHP application to MySQL.

So far, I have had the following connection classes.

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

and

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

or if there is any other better approach for connecting a PHP application to MySQL. Please advise thanks.

+1
php mysql mysqli connect
source share
4 answers

You can try using the PDO object:

 <?php try { $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } ?> 

Check out the PHP PDO documentation page

+3
source share

Try using php frameworks like codeigniter, Yii, cake php. If you implement in any one of these frameworks, you do not need to write php mysql query. It will be automatically generated. You just need to enter the database configuration, for example, give below

 $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'sample'; 
0
source share

You can connect via data using PDO, here is an example

 <?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> 
0
source share

try it

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

ive tried many times to connect to the database, and I finally found it.

-4
source share

All Articles