{"id":2132,"date":"2014-11-02T15:07:23","date_gmt":"2014-11-02T13:07:23","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=2132"},"modified":"2015-05-19T21:57:26","modified_gmt":"2015-05-19T19:57:26","slug":"mysql-mini-guide-useful-instructions","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=2132","title":{"rendered":"Mysql user guide &#8211; useful instructions \/ fetch count rows"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong>Renum\u00e9roter une table avec auto increment<\/strong><\/span><br \/>\nset @i=0;<br \/>\nupdate myvideos set id=(@i:=@i+1);<br \/>\nALTER TABLE myvideos AUTO_INCREMENT = 1;<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Arrangement de zones :<\/strong><\/span><\/p>\n<p>ALTER table Auteurs<br \/>\nMODIFY COLUMN AuteurID\u00a0 mediumint(8)<br \/>\nAFTER notice_ref<\/p>\n<p>ALTER TABLE Auteurs<br \/>\nMODIFY COLUMN notice_ref mediumint( 8 )<br \/>\nAFTER `AuteurID`<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nRENAME TABLE `jarid`.`a030` TO `jarid`.`Auteurs` ;<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>D\u00e9tecter les doublons :<\/strong><\/span><\/p>\n<p>SELECT * , count( * )<br \/>\nFROM tisn<br \/>\nGROUP BY isn<br \/>\nHAVING count( * )&gt;1<\/p>\n<p>SELECT * , count( * )<br \/>\nFROM a220<br \/>\nGROUP BY notice_ref<br \/>\nHAVING count( * ) &gt;1<br \/>\nORDER BY notice_ref DESC<\/p>\n<p>SELECT DISTINCT a220<br \/>\nFROM `a220`<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Synchronisation de strutures de tables champs par champs :<\/strong><\/span><\/p>\n<p>INSERT INTO jarid (notice_id) SELECT isn FROM tisn;<\/p>\n<p>UPDATE jarid j, tia000 t<br \/>\nSET j.a020 = t.A000<br \/>\nWHERE j.notice_id = t.notice_id<\/p>\n<p>SELECT\u00a0\u00a0\u00a0\u00a0 ISN, Ville, Occ, Diffuseur, Editeur<br \/>\nFROM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TABLE_000_A041<br \/>\nWHERE\u00a0\u00a0\u00a0\u00a0 Editeur is null<\/p>\n<p>===== Variantes sur cr\u00e9ation et jointure ====<\/p>\n<p>CREATE TABLE result AS<br \/>\n(SELECT first.*,<br \/>\nsecond.f1,<br \/>\nsecond.f2,<br \/>\nsecond.f3<br \/>\nFROM\u00a0\u00a0 first<br \/>\nINNER JOIN second<br \/>\nON first.id = second.id);<\/p>\n<p>CREATE TABLE events AS<br \/>\n(SELECT e010.*,<br \/>\ne110.summary<br \/>\nFROM\u00a0\u00a0 e010<br \/>\nINNER JOIN e110<br \/>\nON e010.isn = e110.col1);<\/p>\n<p>SELECT a.isn, a.nom<br \/>\nFROM e010duplicate a<br \/>\nINNER JOIN e010duplicate b ON a.nom = b.nom<br \/>\nWHERE a.isn &lt;&gt; b.isn<\/p>\n<p>============= VALEURS NULL =========================<br \/>\nSELECT *<br \/>\nFROM `jarid`<br \/>\nWHERE A0302 IS NOT NULL<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Injections Autorit\u00e9s par occurrence<\/span><\/strong><br \/>\nUPDATE jarid j, A030 t<br \/>\nSET j.a0302 = t.A030<br \/>\nWHERE j.notice_id = t.notice_ref and t.occ = 2<\/p>\n<p>==============================================<\/p>\n<p><strong>Restore a mysql database under ubuntu :<\/strong><\/p>\n<p>Type in console<\/p>\n<pre>$ mysql -h adresse_ip -u user -p -D nom_de_la_base_de_donn\u00e9es &lt; nom_du_fichier_sql<\/pre>\n<p><strong>For example<\/strong><\/p>\n<pre>$ mysql -h localhost -u root -ap -D mesvideos &lt; mesvideos.sql<\/pre>\n<p><strong>Backup one mysql Database under ubuntu :<\/strong><br \/>\nType in console<\/p>\n<pre>$ mysqldump -h adresse_ip -u user -p nom_de_la_base_de_donnees &gt; nom_du_fichier_sql<\/pre>\n<p><strong>For example<\/strong><\/p>\n<pre>$ mysqldump -h localhost -u root -p mesvideos &gt; mesvideos.sql<\/pre>\n<p>====================================================================<\/p>\n<h1>MySQL Fetch using PHP<\/h1>\n<p>PHP provides huge list of MySQL functions to access database from front end. Among those functions, we are going to discuss about some of them that are used to fetch data from database. Such functions are differed with the type of results they are expected to return.<\/p>\n<p>In PHP, MySQL fetch results can be obtained by the following functions. All of these functions will fetch only one row per function call. If required, we should call such functions with a loop for getting subsequent MySQL fetch results row by row.<\/p>\n<ul>\n<li>mysql_fetch_row()<\/li>\n<li>mysql_fetch_assoc()<\/li>\n<li>mysql_fetch_array()<\/li>\n<li>mysql_fetch_object()<\/li>\n<li>mysql_fetch_lengths()<\/li>\n<li>mysql_fetch_field()<\/li>\n<\/ul>\n<h2>mysql_fetch_row()<\/h2>\n<p>This function will fetch data about single row with which the row pointer is currently exist. After fetching the entire row details, it will be returned as an array with number indices corresponding to the MySQL field offset.<\/p>\n<p>For getting such resultant array, mysql_fetch_row() requires a resource data that is returned by executing query appropriate with MySQL fetch operations. If no results found for the query, then mysql_fetch_row() will return nothing as expected.<\/p>\n<p>Let us consider the <em>Users<\/em> table, which we had taken for example while seeing about MySQL Left JOIN and MySQL Right JOIN. And the table data and structure is as follows.<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/11\/mysql_user_table.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2615\" alt=\"mysql_user_table\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/11\/mysql_user_table.png\" width=\"344\" height=\"163\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/11\/mysql_user_table.png 344w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/11\/mysql_user_table-300x142.png 300w\" sizes=\"(max-width: 344px) 100vw, 344px\" \/><\/a><\/p>\n<p>The following PHP program is for fetching MySQL data using mysql_fetch_row(). Before that, the database connections are made with first two lines as usual.<\/p>\n<p>$conn = mysql_connect(&#8220;localhost&#8221;,&#8221;root&#8221;,&#8221;&#8221;);<br \/>\nmysql_select_db(&#8220;phppot_examples&#8221;,$conn);<br \/>\n$query = &#8220;SELECT * from Users&#8221;;<br \/>\n$result = mysql_query($query) or die(mysql_error());<br \/>\n$row = mysql_fetch_row($result);<br \/>\nprint &#8221;<\/p>\n<pre>\";\r\nprint_r($row);\r\nprint \"<\/pre>\n<pre>\";<\/pre>\n<p>This program will return values of the entire first row, since the row pointer is at the beginning. So the output will be as shown below that can be cross checked with the table data shown already.<\/p>\n<p>Array<br \/>\n(<br \/>\n[0] =&gt; 1<br \/>\n[1] =&gt; admin<br \/>\n[2] =&gt; admin123<br \/>\n[3] =&gt; student<br \/>\n)<\/p>\n<p>If we want to get all the row information, we should do the same process recursively by iterating through a loop until end of record. For example,<\/p>\n<p>while($row = mysql_fetch_row($result)) {<br \/>\nprint &#8221;<\/p>\n<pre>\";\r\nprint_r($row);\r\nprint \"<\/pre>\n<pre>\";\r\n}<\/pre>\n<h2>mysql_fetch_assoc()<\/h2>\n<p>This functions is similar to the mysql_fetch_row(), except that, it will return array of row information containing column values are indexed with column name. So the result type is of an associative array where each column name and values of a single row is associated together as name,value pairs.<\/p>\n<p>Let us replace mysql_fetch_row() with mysql_fetch_assoc() in the above program which will return the following array.<\/p>\n<p>Array<br \/>\n(<br \/>\n[user_id] =&gt; 1<br \/>\n[user_name] =&gt; admin<br \/>\n[password] =&gt; admin123<br \/>\n[user_type] =&gt; student<br \/>\n)<\/p>\n<p>By using this type of MySQL fetch, we can access the data by its name instead of its offset. Since, remembering the order of fields is too tough for huge list of columns, accessing by field name is easy and there by this function could be preferred in such scenario.<\/p>\n<h2>mysql_fetch_array()<\/h2>\n<p>This MySQL fetch method returns resultant array with both indices, that is, field offset and field name. So, it would be used most probably by having both option of indexing.<\/p>\n<p>Unlike above two functions, mysql_fetch_array() accepts an optional argument for specifying resultant array index type and its possible values are,<\/p>\n<ul>\n<li><em>MYSQL_BOTH<\/em> \u2013 It is the default value that would be taken if no second argument is provided for this function. It will provide resultant array with both indices.<\/li>\n<li><em>MYSQL_NUM<\/em> \u2013 With this option, mysql_fetch_array() will return array with offset indices as same as mysql_fetch_row().<\/li>\n<li><em>MYSQL_ASSOC<\/em> \u2013 With this option, mysql_fetch_array() will return array with name indices as same as mysql_fetch_assoc().<\/li>\n<\/ul>\n<p>By replacing mysql_fetch_row() with mysql_fetch_array(), the output array will be,<\/p>\n<p>Array<br \/>\n(<br \/>\n[0] =&gt; 1<br \/>\n[user_id] =&gt; 1<br \/>\n[1] =&gt; admin<br \/>\n[user_name] =&gt; admin<br \/>\n[2] =&gt; admin123<br \/>\n[password] =&gt; admin123<br \/>\n[3] =&gt; student<br \/>\n[user_type] =&gt; student<br \/>\n)<\/p>\n<h2>mysql_fetch_object()<\/h2>\n<p>mysql_fetch_object() function will return MySQL data with same structure as returned by mysql_fetch_assoc(), but its type is different. mysql_fetch_object() returns object where as mysql_fetch_assoc() returns array. So, the way of accessing these data will also be differed. For example, if we are required to access <em>user_name<\/em>, after array fetch, it will be done by,<\/p>\n<p>echo $row[&#8220;user_name&#8221;];<\/p>\n<p>Or else after object fetch, it would be,<\/p>\n<p>echo $row-&gt;user_name;<\/p>\n<h2>mysql_fetch_lengths()<\/h2>\n<p>This PHP function is used to return the string length each column value of the recently fetched row. So, before calculating the string length, any one of the above MySQL fetch functions need to be invoked.<\/p>\n<p>For example, the following program is to fetch single row data using mysql_fetch_object() and to print the corresponding length array to the browser.<\/p>\n<p>$conn = mysql_connect(&#8220;localhost&#8221;,&#8221;root&#8221;,&#8221;&#8221;);<br \/>\nmysql_select_db(&#8220;phppot_examples&#8221;,$conn);<br \/>\n$query = &#8220;SELECT * from Users&#8221;;<br \/>\n$result = mysql_query($query) or die(mysql_error());<br \/>\n$row = mysql_fetch_object($result);<br \/>\n$student_length = mysql_fetch_lengths($result);<br \/>\nprint &#8221;<\/p>\n<pre>\";\r\nprint_r($row);\r\nprint_r($student_length);\r\nprint \"<\/pre>\n<pre>\";<\/pre>\n<p>And the output is,<\/p>\n<p>stdClass Object<br \/>\n(<br \/>\n[user_id] =&gt; 1<br \/>\n[user_name] =&gt; admin<br \/>\n[password] =&gt; admin123<br \/>\n[user_type] =&gt; student<br \/>\n)<br \/>\nArray<br \/>\n(<br \/>\n[0] =&gt; 1<br \/>\n[1] =&gt; 5<br \/>\n[2] =&gt; 8<br \/>\n[3] =&gt; 7<br \/>\n)<\/p>\n<p>Obviously, the object properties array and the length array are returned as shown above that could be cross checked manually to ensure the property value length is correct as expected.<\/p>\n<h2>mysql_fetch_field()<\/h2>\n<p>Unlike above functions, mysql_fetch_field() is for getting MySQL Database table\u2019s field information instead of record data. And, this function also fetch one field per call and need loop implementation for getting more fields. These information array will be returned as an object which includes properties like, <em>table name<\/em>, <em>field name<\/em>, <em>field maximum length<\/em>, <em>primary key flag offset<\/em> and etc. For example, the user_id field details of <em>Users<\/em> table is returned as follows.<\/p>\n<p>stdClass Object<br \/>\n(<br \/>\n[name] =&gt; user_id<br \/>\n[table] =&gt; Users<br \/>\n[def] =&gt;<br \/>\n[max_length] =&gt; 1<br \/>\n[not_null] =&gt; 1<br \/>\n[primary_key] =&gt; 1<br \/>\n[multiple_key] =&gt; 0<br \/>\n[unique_key] =&gt; 0<br \/>\n[numeric] =&gt; 1<br \/>\n[blob] =&gt; 0<br \/>\n[type] =&gt; int<br \/>\n[unsigned] =&gt; 0<br \/>\n[zerofill] =&gt; 0<br \/>\n)<\/p>\n<p><em>Note:<\/em><em>mysql<\/em> extenstion we have seen here are deprecated as of PHP version 5.5. So the alternative extension <em>mysqli<\/em> is used instead for later version of PHP. So for later version, the above functions should be changed by replacing its prefix <em>mysql<\/em> as <em>mysqli<\/em><\/p>\n<div id=\"example-1779\">\n<p><strong>Example #1 <strong>mysql_num_rows()<\/strong> example<\/strong><\/p>\n<p>Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set.<\/p>\n<div>\n<div><code><code> &lt;?php<\/code><\/code>$link\u00a0=\u00a0mysql_connect(&#8220;localhost&#8221;,\u00a0&#8220;mysql_user&#8221;,\u00a0&#8220;mysql_password&#8221;);<br \/>\nmysql_select_db(&#8220;database&#8221;,\u00a0$link);$result\u00a0=\u00a0mysql_query(&#8220;SELECT\u00a0*\u00a0FROM\u00a0table1&#8221;,\u00a0$link);<br \/>\n$num_rows\u00a0=\u00a0mysql_num_rows($result);echo\u00a0&#8220;$num_rows\u00a0Rows\\n&#8221;;?&gt;<strong>Different type of fetching function is given below:<\/strong><br \/>\n<strong>1.\u00a0\u00a0 \u00a0MYSQL_FETCH_ROW()<\/strong><br \/>\n<strong>2.\u00a0\u00a0 \u00a0MYSQL_FETCH_ARRAY()<\/strong><br \/>\n<strong>3.\u00a0\u00a0 \u00a0MYSQL_FETCH_ASSOC()<\/strong><br \/>\n<strong>4.\u00a0\u00a0 \u00a0MYSQL_FETCH_OBJECT()<\/strong><\/p>\n<p>1.\u00a0\u00a0 \u00a0MYSQL_FETCH_ROW():\u00a0 It is used to fetch data in numeric array. It gives result row as a numeric array.<\/p>\n<p>$result=mysql_query(&#8220;select * from user_table&#8221;);<br \/>\n$result_row=mysql_fetch_row($result);<br \/>\necho $result_row[0];<br \/>\necho $result_row[1];<br \/>\necho $result_row[2];<\/p>\n<p>?&gt;<br \/>\n2.\u00a0\u00a0 \u00a0MYSQL_FETCH_ARRAY(): It is used to fetch data in numeric array as well as associative array . It gives result row as a numeric array as well as associative array.<br \/>\n<!--?php<br \/-->$result=mysql_query(&#8220;select * from user_table&#8221;);<br \/>\n$result_row=mysql_fetch_row($result);<br \/>\necho $result_row[0];<br \/>\necho $result_row[1];<br \/>\necho $result_row[2];<br \/>\n\/* numeric array as well as associative array *\/<br \/>\necho $result_row[id];<br \/>\necho $result_row[first_name];<br \/>\necho $result_row[last_name];<\/p>\n<p>?&gt;<\/p>\n<p>3.\u00a0\u00a0 \u00a0MYSQL_FETCH_ASSOC(): It is used to fetch data in associative array. It gives result row as a associative array.<br \/>\n<!--?php<br \/-->$result=mysql_query(&#8220;select * from user_table&#8221;);<br \/>\n$result_row=mysql_fetch_row($result);<br \/>\necho $result_row[id];<br \/>\necho $result_row[first_name];<br \/>\necho $result_row[last_name];<\/p>\n<p>?&gt;<\/p>\n<p>4.\u00a0\u00a0 \u00a0MYSQL_FETCH_OBJECT(): It is used to fetch data as object. It gives result row as a object.<\/p>\n<p>$result=mysql_query(&#8220;select * from user_table&#8221;);<br \/>\n$result_row=mysql_fetch_row($result);<br \/>\necho $result_row-&gt;id;<br \/>\necho $result_row-&gt;first_name;<br \/>\necho $result_row-&gt;last_name;<\/p>\n<p>?&gt;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Renum\u00e9roter une table avec auto increment set @i=0; update myvideos set id=(@i:=@i+1); ALTER TABLE myvideos AUTO_INCREMENT = 1; Arrangement de zones : ALTER table Auteurs MODIFY COLUMN AuteurID\u00a0 mediumint(8) AFTER notice_ref ALTER TABLE Auteurs&#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":[313],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2132"}],"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=2132"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/2132\/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=2132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}