{"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

\"how-to-use-mysqli_connect-in-php\"<\/p>\n

Example of mysqli connection with the object method:<\/p>\n

<?php\r\n$sql = new mysqli('localhost','root','password','dbname');\r\n?><\/pre>\n

Example of mysqli connection with the procedural method:<\/p>\n

<?php\r\n$con = mysqli_connect('localhost','root','password','dbname');\r\n?><\/pre>\n

Connection error checking with object method:<\/p>\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

Connection error checking with\u00a0procedural method:<\/p>\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

Escaping characters with mysqli real escape:<\/p>\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

Simple procedure with complete code with procedural method:<\/p>\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

Hope you like this tutorial love to reply on your comments.<\/p>\n

Facebook<\/a><\/blockquote><\/div><\/div>
Tutorial Categories:<\/strong>
\n \"Database\"<\/a><\/div>
\n \"MySQL\"<\/a><\/div>
\n \"MySQLi\"<\/a><\/div>
\n \"PHP\"<\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"

As you all knows that MySQLi is improved PHP extension of MySQL which is introduced in PHP 5. MySQL 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 (PHP Data Objects 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.<\/p>\n

\"how-to-use-mysqli_connect-in-php\"<\/p>\n","protected":false},"author":1,"featured_media":338,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":"","footnotes":""},"categories":[164,5,4,190,3],"tags":[166,631,191,632,195,623,193,22,194,23,25,21,20,24],"_links":{"self":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":0,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media\/338"}],"wp:attachment":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}