{"id":2733,"date":"2015-10-05T11:55:42","date_gmt":"2015-10-05T09:55:42","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=2733"},"modified":"2016-10-05T17:47:14","modified_gmt":"2016-10-05T15:47:14","slug":"deprecated-mysql-to-mysqli-mapping","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=2733","title":{"rendered":"Deprecated Mysql to Mysqli mapping"},"content":{"rendered":"<h1 id=\"paragraph_introduction\">Introduction<\/h1>\n<p>Since PHP 5.4, the original MySQL extension is obsolete and will generate alerts E_DEPRECATED level when connecting to a database. Instead, we can use the MySQLi extension or PDO_MySQL extension.<\/p>\n<p>If like me, you have sites with the MySQL extension, here are some small examples to switch to MySQL MySQLi (which I find easier to use on my little creations).<\/p>\n<p><strong>Database connection<\/strong><br \/>\nPreviously, MySQL, connect to the database was thus:<\/p>\n<p>Code PHP :<\/p>\n<div>\n<pre>\u00a0\r\n\/\/ we connect to MySQL\r\n$conn = <a href=\"http:\/\/www.php.net\/mysql_connect\">mysql_connect<\/a>('$host', '$user', '$passwd');\r\n\r\n\/\/ we select  database\r\n<a href=\"http:\/\/www.php.net\/mysql_select_db\">mysql_select_db<\/a>('mabase',$conn);<\/pre>\n<\/div>\n<p><strong>Now, with MySQLi :<\/strong><\/p>\n<p>&lt;?php<\/p>\n<p>\/\/ Connection variables<br \/>\n$host = &#8220;localhost&#8221;; \/\/ MySQL host name eg. localhost<br \/>\n$user = &#8220;user1&#8221;; \/\/ MySQL user. eg. root ( if your on localserver)<br \/>\n$password = &#8220;&#8221;; \/\/ MySQL user password\u00a0 (if password is not set for your root user then keep it empty )<br \/>\n$database = &#8220;zzz&#8221;; \/\/ MySQL Database name<\/p>\n<p>\/\/ Connect to MySQL Database<br \/>\n$link = mysqli_connect($host, $user, $password, $database);<\/p>\n<p>\/\/ Check connection<br \/>\nif (mysqli_connect_errno())<br \/>\n{<br \/>\necho &#8220;Failed to connect to MySQL: &#8221; . mysqli_connect_error();<br \/>\n}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1 id=\"paragraph_les-requetes\">Les requ\u00eates<\/h1>\n<div>\n<h2>Migrating to MySQLi Procedural Methods<\/h2>\n<\/div>\n<div>\n<p>MySQLi procedural methods use a parameter that references either an object link or a result object. We have seen the reference to the object link when we dealt with mysqli_select_db. The result object is similar to a MySQL result returned from a query, for example.<\/p>\n<\/div>\n<div>\n<p>Many of the methods in MySQL have very similar procedural methods in MySQLi, and are as simple to migrate as adding the i to mysql and adding or moving the link or result to the first parameter. Remember that MySQLi requires the link for those methods that reference a link. In the following list, the MySQL statement is followed by the replacement MySQLi procedural method.<\/p>\n<\/div>\n<div>\n<p><b>mysql_affected_rows<\/b> -&gt; mysqli_affected_rows($link)<br \/>\n<b>mysql_close<\/b> -&gt; mysqli_close($link)<br \/>\n<b>mysql_data_seek<\/b> -&gt; mysqli_data_seek( $result, $offset)<br \/>\n<b>mysql_errno<\/b> -&gt; mysqli_errno( $link)<br \/>\n<b>mysql_error<\/b> -&gt; mysqli_error( $link)<br \/>\n<b>mysql_fetch_array<\/b> -&gt; mysqli_fetch_array( $result, $type)<br \/>\n<b>mysql_fetch_assoc<\/b> -&gt; mysqli_fetch_assoc( $result)<br \/>\n<b>mysql_fetch_lengths<\/b> -&gt; mysqli_fetch_lengths( $result )<br \/>\n<b>mysql_fetch_object<\/b> -&gt; mysqli_fetch_object( $result, $class, $params)<br \/>\n<b>mysql_fetch_row<\/b> -&gt; mysqli_fetch_row( $result)<br \/>\n<b>mysql_field_seek<\/b> -&gt; mysqli_field_seek( $result, $number)<br \/>\n<b>mysql_free_result<\/b> -&gt; mysqli_free_result(result)<br \/>\n<b>mysql_get_client_info<\/b> -&gt; mysqli_get_client_info( $link)<br \/>\n<b>mysql_get_host_info<\/b> -&gt; mysqli_get_host_info( $link)<br \/>\n<b>mysql_get_proto_info<\/b> -&gt; mysqli_get_proto_info( $link)<br \/>\n<b>mysql_get_server_info<\/b> -&gt; mysqli_get_server_info( $link)<br \/>\n<b>mysql_info<\/b> -&gt; mysqli_info( $link)<br \/>\n<b>mysql_insert_id<\/b> -&gt; mysqli_insert_id( $link)<br \/>\n<b>mysql_num_rows<\/b> -&gt; \u00a0mysqli_num_rows( $result)<br \/>\n<b>mysql_ping<\/b> -&gt; mysqli_ping( $link)<br \/>\n<b>mysql_query<\/b> -&gt; mysqli_query( $link, $query)<br \/>\n<b>mysql_real_escape_string<\/b> -&gt; mysqli_real_escape_string( $link)<br \/>\n<b>mysql_select_db<\/b> &#8211; &gt; mysqli_select_db( $link, $database)<br \/>\n<b>mysql_set_charset<\/b> -&gt; mysqli_set_charset( $link, $charset)<br \/>\n<b>mysql_stat<\/b> -&gt; mysqli_stat( $link)<br \/>\n<b>mysql_thread_id<\/b> -&gt; mysqli_thread_id( $link)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Since PHP 5.4, the original MySQL extension is obsolete and will generate alerts E_DEPRECATED level when connecting to a database. Instead, we can use the MySQLi extension or PDO_MySQL extension. If like me,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2845,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35,25],"tags":[],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2733"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2733"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2845"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}