{"id":336,"date":"2024-01-23T05:01:48","date_gmt":"2024-01-23T05:01:48","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=336---0045bbfe-3cc2-4fd5-9feb-91bf51d83256"},"modified":"2024-01-23T05:01:48","modified_gmt":"2024-01-23T05:01:48","slug":"how-to-use-mysqli_connect-in-php","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-use-mysqli_connect-in-php_336.html","title":{"rendered":"How to use MySQLi_connect in PHP"},"content":{"rendered":"
As you all knows that MySQLi is improved PHP<\/a> extension of MySQL which is introduced in PHP 5. MySQL<\/a> is deprecated in PHP 5.5 will be removed in future. We have millions of applications using PHP MySQL and needs to be updated on MySQLi or PDO (P<\/em>HP D<\/em>ata O<\/em>bjects is also a new extension you can use to use in your applications). So today i am going to give you a very simple integration guide of MySQLi how to connect to a database, how to \u00a0run a query and insert records etc hope you love this article<\/a>.<\/p>\n <\/p>\n Example of mysqli connection with the object method:<\/p>\n Example of mysqli connection with the procedural method:<\/p>\n Connection error checking with object method:<\/p>\n Connection error checking with\u00a0procedural method:<\/p>\n Escaping characters with mysqli real escape:<\/p>\n Simple procedure with complete code with procedural method:<\/p>\n Hope you like this tutorial love to reply on your comments.<\/p>\n<?php\r\n$sql = new mysqli('localhost','root','password','dbname');\r\n?><\/pre>\n
<?php\r\n$con = mysqli_connect('localhost','root','password','dbname');\r\n?><\/pre>\n
<?php\r\n$sql = new mysqli('localhost', 'root', 'password', 'dbname');\r\n\r\nif($db->connect_errno > 0){\r\n die('Unable to connect to database [' . $db->connect_error . ']');\r\n}\r\n?><\/pre>\n
<?php\r\n$con = mysqli_connect(\"HostName\",\"UserName\",\"password\",\"DBName\") or die(\"Some error occurred during connection \" . mysqli_error($con));\r\n?><\/pre>\n
<?php\r\n$db->real_escape_string('This is an un-escaped \"string\"');\r\n\r\n\/\/there is an alias function less typed\r\n\r\n$db->escape_string('This is an un-escape \"string\"');\r\n\r\n?><\/pre>\n
<?php \r\n\/\/Create the connection \r\n\r\n$con = mysqli_connect(\"HostName\",\"UserName\",\"password\",\"DBName\") or die(\"Some error occurred during connection \" . mysqli_error($con)); \r\n\r\n\/\/ Write query\r\n\r\n$strSQL = \"SELECT username FROM MyTable\";\r\n\r\n\/\/ Execute the query.\r\n\r\n$query = mysqli_query($con, $strSQL);\r\nwhile($result = mysqli_fetch_array($query))\r\n{\r\n echo $result[\"username\"].\"\r\n\";\r\n}\r\n\r\n\/\/ Close the connection\r\nmysqli_close($con);\r\n\r\n?><\/pre>\n