{"id":102,"date":"2020-12-05T11:04:31","date_gmt":"2020-12-05T05:34:31","guid":{"rendered":"https:\/\/selfimagination.in\/tips\/?p=102"},"modified":"2021-12-24T17:45:57","modified_gmt":"2021-12-24T12:15:57","slug":"dbcmd","status":"publish","type":"post","link":"https:\/\/selfimagination.in\/tips\/dbcmd\/","title":{"rendered":"DbCmd"},"content":{"rendered":"\n<h2 class=\"has-black-color has-text-color wp-block-heading\">PHP Database Connection&nbsp; Example<\/h2>\n\n\n\n<p class=\"wp-block-zozuk-wphindi\">\u092f\u0926\u093f \u0906\u092a PHP \u092a\u0930 \u0915\u094b\u0908 \u090f\u092a\u094d\u0932\u0940\u0915\u0947\u0936\u0928 \u092c\u0928\u093e\u0928\u093e \u091a\u093e\u0939\u0924\u0947 \u0939\u0948\u0902 \u0924\u094b \u0906\u092a\u0915\u094b \u0938\u092c\u0938\u0947 \u092a\u0939\u0932\u0947 \u092f\u0939 \u0926\u094b PHP \u092b\u093e\u0907\u0932 \u0905\u092a\u0928\u0940 \u0930\u0941\u091f \u0921\u093e\u092f\u0930\u0947\u0915\u094d\u091f\u0930\u0940 \u092e\u0947\u0902 \u092c\u0928\u093e\u0928\u0940 \u0939\u094b\u0917\u0940, \u0907\u0938\u0938\u0947 \u092f\u0939 \u092b\u093e\u092f\u0926\u093e \u0939\u0948 \u0915\u0940 \u091c\u092c \u092d\u0940 \u0915\u093f\u0938\u0940 \u092d\u0940 \u092a\u094d\u0930\u0915\u093e\u0930 \u0915\u093e \u0935\u0930\u094d\u0936\u0928 \u091a\u0947\u0902\u091c \u0939\u094b\u0928\u0947 \u092a\u0930 \u0938\u093f\u0902\u091f\u0947\u0915\u094d\u0938 \u092e\u0947\u0902 \u091a\u0947\u0902\u091c \u0906\u0924\u0947 \u0939\u0948\u0902 \u0924\u094b \u0939\u092e\u0947\u0902 \u092c\u0939\u0941\u0924 \u0938\u0940 \u092b\u093e\u0907\u0932 \u092e\u0947\u0902 \u091a\u0947\u0902\u091c \u0928 \u0915\u0930\u0924\u0947 \u0939\u0941\u090f \u0938\u093f\u0930\u094d\u092b \u090f\u0915 \u0939\u0940 \u092b\u093e\u0907\u0932 \u092e\u0947\u0902 \u091a\u0947\u0902\u091c \u0915\u0930\u0928\u0947 \u0938\u0947  Changes \u090f\u092a\u094d\u0932\u0940\u0915\u0947\u092c\u0932 \u0939\u094b \u091c\u093e\u0924\u0947 \u0939\u0948\u0902 \u0964<\/p>\n\n\n\n<p class=\"wp-block-zozuk-wphindi\"><\/p>\n\n\n\n<pre title=\"dbcmd.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\nclass DBCmd \n{\n   var $con;\n   var $ret;\n   \n   function __construct() \n   {\n        \n   }\n   \n   function connect()\n   {\n\t   include(\"conn.php\");\n\t\t$this-&gt;con=mysqli_connect($ls_lh,$ls_un,$ls_pw,$ls_db);\n\t\tif (mysqli_connect_errno())\n\t\t{\n\t\t\techo \"Failed to connect to MySQL: \" . mysqli_connect_error();\n\t\t}\n\t\t\/\/mysqli_set_charset( $this-&gt;con, 'utf8');\n\t\treturn $this-&gt;con;\n   }\n\n   function connect_other($ls_nm)\n   {\n\t   include(\"conn_\".$ls_nm.\".php\");\n\t\t$this-&gt;con=mysqli_connect($ls_lh,$ls_un,$ls_pw,$ls_db);\n\t\tif (mysqli_connect_errno())\n\t\t{\n\t\t\techo \"Failed to connect to MySQL: \" . mysqli_connect_error();\n\t\t}\n\t\t\n\t\treturn $this-&gt;con;\n   }\n   \n   function query($sql)\n   {\n\t   $ret = mysqli_query($this-&gt;con, $sql);\n\t   return $ret;\n   }\n   \n   function query_id($sql)\n   {\n\t   mysqli_query($this-&gt;con, $sql);\n\t   return mysqli_insert_id($this-&gt;con);\n   }\n   \n   function query_cnt($sql)\n   {\n\t\t$ret = mysqli_query($this-&gt;con, $sql);\n\t\t\n\t\tif (mysqli_num_rows($ret) &gt; 0)\n\t\t{\t\n\t\t\twhile ($row = mysqli_fetch_array($ret)) \n\t\t\t{\n\t\t\t\treturn $row[0];\n\t\t\t}\n\t\t}\n\t\treturn 0;\n   }\n\n   function query_if_error($sql, $sql2)\n   {\n\t    if (mysqli_query($this-&gt;con, $sql))\n\t\t{\n\t\t\treturn true;\n\t\t}\t\n\t\telse\n\t\t{\n\t\t\treturn mysqli_query($this-&gt;con, $sql2);\n\t\t}\n   }\n\n   \n   function close()\n   {\n\t\tmysqli_close($this-&gt;con);\n   }\n\t\n}\t\n?&gt;\n\n<\/code><\/pre>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-zozuk-wphindi\">\u092f\u0939 \u0921\u093f\u095e\u0949\u0932\u094d\u091f \u0915\u0928\u0947\u0915\u094d\u0936\u0928 \u092b\u093e\u0907\u0932 \u0939\u0948\u0902, \u0907\u0938\u092e\u0947\u0902 \u0906\u092a MySQL \u0915\u0947 \u0921\u0947\u091f\u093e\u092c\u0947\u0938 \u0915\u094d\u0930\u0947\u0921\u0947\u0902\u0936\u093f\u092f\u0932 \u0938\u0947\u091f \u0915\u0930 \u0938\u0915\u0924\u0947 \u0939\u0948\u0902, \u092f\u0926\u093f \u0906\u092a \u092e\u0932\u094d\u091f\u0940\u092a\u094d\u0932 \u0921\u0947\u091f\u093e\u092c\u0947\u0938 \u0938\u0947 \u0915\u0928\u0947\u0915\u094d\u091f \u0915\u0930\u0928\u093e \u091a\u093e\u0939\u0924\u0947 \u0939\u0948\u0902, \u0924\u094b \u0906\u092a\u0915\u094b conn_ \u092a\u094d\u0930\u0940\u092b\u093f\u0915\u094d\u0938 \u0938\u0947 \u090f\u0915 \u0914\u0930 \u0915\u0928\u0947\u0915\u094d\u0936\u0928 \u092b\u093e\u0907\u0932 \u092c\u0928\u093e\u0928\u093e \u0939\u094b\u0917\u0940 \u0914\u0930 \u0909\u0938\u0947 connect_other(&#8216;name&#8217;)  \u0935\u093e\u0932\u0947 \u092b\u0902\u0915\u094d\u0936\u0928 \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930\u0915\u0947 \u0939\u092e \u0926\u0942\u0938\u0930\u0947 \u0921\u0947\u091f\u093e\u092c\u0947\u0938 \u0915\u094b \u092d\u0940 \u0915\u0928\u0947\u0915\u094d\u091f \u0915\u0930 \u0938\u0915\u0924\u0947 \u0939\u0948\u0902 <\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre title=\"conn.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\n\t$ls_lh = \"hostname\";\n\t$ls_un = \"username\";\n\t$ls_pw = \"password\";\n\t$ls_db = \"databasename\";\n\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Local Machine Setting<\/h2>\n\n\n\n<p>\u091c\u092c \u0906\u092a Local System \u092a\u0930 \u0915\u0928\u0947\u0915\u094d\u0936\u0928 \u0915\u0930\u0928\u093e \u091a\u093e\u0939\u0924\u0947 \u0939\u0948\u0902 \u0924\u094b \u0907\u0938 Connection Setting \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930 \u0938\u0915\u0924\u0947 \u0939\u0948\u0902 \u0964 <\/p>\n\n\n\n<pre title=\"conn.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\n\t$ls_lh = \"localhost\";\n\t$ls_un = \"root\";\n\t$ls_pw = \"\";\n\t$ls_db = \"databasename\";\n\n?&gt;<\/code><\/pre>\n\n\n\n<div style=\"height:57px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-zozuk-wphindi\">\u092f\u0939\u093e\u0901 \u092a\u0930 \u0939\u092e\u0928\u0947 \u091c\u094b \u092a\u0939\u0932\u0947 \u092b\u0902\u0915\u094d\u0936\u0928 \u092c\u0928\u093e\u092f\u0947 \u0925\u0947 \u0907\u0928\u0915\u0947 \u0909\u092a\u092f\u094b\u0917 \u0915\u093f\u092f\u0947 \u0939\u0948\u0902,  \u091c\u094b \u092a\u0939\u0932\u093e \u0935\u093e\u0932\u093e \u092b\u0902\u0915\u094d\u0936\u0928 \u0939\u0948\u0902 \u0909\u0938\u092e\u0947 \u0939\u092e\u0947\u0902 \u0932\u0942\u092a \u0932\u0917\u093e\u0928\u0947 \u0915\u0940 \u091c\u0930\u0941\u0930\u0924 \u0928\u0939\u0940\u0902 \u0939\u094b\u0924\u0940 \u0915\u094d\u092f\u0941\u0915\u0940 \u0907\u0938\u092e\u0947\u0902 \u0938\u093f\u0902\u0917\u0932 Row \u0914\u0930 \u0938\u093f\u0902\u0917\u0932 \u0915\u0949\u0932\u092e \u0915\u0940 \u0935\u0948\u0932\u094d\u092f\u0942 Return \u0939\u094b\u0924\u0940 \u0939\u0948\u0902 \u0964<\/p>\n\n\n\n<div style=\"height:52px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre title=\"Uses\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\ninclude(\"dbcmd.php\");\n\n$dbc = new DBCmd();\n$con = $dbc-&gt;connect();\n\n\/\/ Return Single Row and Single Column Value\n$ls_SQL1 = \"SELECT ColumnName FROM TableName WHERE whereclause limit 0,1\";\n$val = $dbc-&gt;query_cnt($ls_SQL1);\necho 'Value of Column' . $val;\n\n\/\/ Return Multiple Rows and Columns Values\n$ls_SQL2 = \"SELECT List of Columns FROM TableName WHERE whereclause\"; \n$result = $dbc-&gt;query($ls_SQL2);\nwhile ($row = mysqli_fetch_array($result)) \n{\n   echo 'Value 1:' . $row[\"column1\"] ;\n   echo 'Value 2:' . $row[\"column2\"] ;\n}\n\n\/\/ Return Auto Increment Column Values After Saving \n$ls_SQL3 = \"INSERT INTO TableName (`Column1`, `Column2`) \nVALUES ('Value1','Value2')\";\n$id = $dbc-&gt;query_id($ls_SQL3);\necho \"Value of Auto Increment Column \" . $id;\n\n\n$dbc-&gt;close();\n\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP Database Connection&nbsp; Example Local Machine Setting \u091c\u092c \u0906\u092a Local System \u092a\u0930 \u0915\u0928\u0947\u0915\u094d\u0936\u0928 \u0915\u0930\u0928\u093e \u091a\u093e\u0939\u0924\u0947 \u0939\u0948\u0902 \u0924\u094b \u0907\u0938 Connection Setting \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930 \u0938\u0915\u0924\u0947 \u0939\u0948\u0902 \u0964<\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/selfimagination.in\/tips\/wp-content\/uploads\/2020\/12\/DBConnnection.png?fit=568%2C590&ssl=1","_links":{"self":[{"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":54,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":5455,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/posts\/102\/revisions\/5455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/selfimagination.in\/tips\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}