{"id":859,"date":"2018-03-03T22:55:45","date_gmt":"2018-03-03T22:55:45","guid":{"rendered":"https:\/\/webhostingplus.uk\/hosting\/?p=859"},"modified":"2018-03-03T22:55:45","modified_gmt":"2018-03-03T22:55:45","slug":"connect-mysql-database-using-cgi","status":"publish","type":"post","link":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/","title":{"rendered":"How do I connect to my MySQL database using CGI?"},"content":{"rendered":"<p>You can also choose to use Perl and the DBI Perl Module to access your database. A commented example: <\/p>\n<p>#!\/usr\/bin\/perl <\/p>\n<p>use DBI; <\/p>\n<p># Connect To Database<br \/>\n# * The DBI interface to MySQL uses the method &#8220;connect&#8221; to make a<br \/>\n# * connection to the database. It takes as it&#8217;s first argument<br \/>\n# * the string &#8220;DBI:mysql:database:hostname&#8221;, where database is equal<br \/>\n# * to the name of your database, and hostname to the server that it&#8217;s<br \/>\n# * located on. The second and third arguments, respectively, should<br \/>\n# * be your account username and password. The connection is assigned.<br \/>\n# * to a variable that is used by most other methods in the module.<br \/>\n$database = &#8220;your database name&#8221;;<br \/>\n$username = &#8220;your database username&#8221;;<br \/>\n$password = &#8220;your database password&#8221;;<br \/>\n$hostname = &#8220;your database hostname&#8221;;<br \/>\n$db = DBI->connect(&#8220;DBI:mysql:$database:$hostname&#8221;, $username, $password); <\/p>\n<p># Execute a Query<br \/>\n# * executing a query is done in two steps. First,<br \/>\n# * the query is setup using the &#8220;prepare&#8221; method.<br \/>\n# * this requires the use of the variable used to<br \/>\n# * initiate the connection. Second, the &#8220;execute&#8221;<br \/>\n# * method is called, as shown below.<br \/>\n$query = $db->prepare(&#8220;SELECT * FROM test&#8221;);<br \/>\n$query->execute; <\/p>\n<p># How many rows in result?<br \/>\n# * the &#8220;rows&#8221; method using the variable name the<br \/>\n# * query was executed under returns the number<br \/>\n# * of rows in the result.<br \/>\n$numrows = $query->rows; <\/p>\n<p># Display Results<br \/>\n# * the fetchrow_array method executed on the<br \/>\n# * query returns the first row as an array.<br \/>\n# * subsequent calls return the other rows in<br \/>\n# * sequence. It returns zero when all rows have<br \/>\n# * been retrieved.<br \/>\nwhile (@array = $query->fetchrow_array) { ($field1, $field2, $field3) = @array; print &#8220;field1 = $field1, field2 = $field2, field3 = $field3 \\n&#8221;; } <\/p>\n<p># Cleaning Up<br \/>\n# * with the DBI module, it is a good idea to clean up by<br \/>\n# * explicitly ending all queries with the &#8220;finish&#8221; method,<br \/>\n# * and all connections with the &#8220;disconnect&#8221; method.<br \/>\n$query->finish;<br \/>\n$db->disconnect; <\/p>\n<p>exit(0); <\/p>\n<p>The DBI Module provides other methods you might find useful. More information is available by running &#8220;perldoc DBI&#8221; while logged on to your account via SSH. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can also choose to use Perl and the DBI Perl Module to access your database. A commented example: #!\/usr\/bin\/perl use DBI; # Connect To Database # * The DBI interface to MySQL uses the method &#8220;connect&#8221; to make a # * connection to the database. It takes as it&#8217;s first argument # * the &#8230; <a title=\"How do I connect to my MySQL database using CGI?\" class=\"read-more\" href=\"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/\" aria-label=\"Read more about How do I connect to my MySQL database using CGI?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-859","post","type-post","status-publish","format-standard","hentry","category-mysql-databases"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do I connect to my MySQL database using CGI? - Web Hosting Plus<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I connect to my MySQL database using CGI? - Web Hosting Plus\" \/>\n<meta property=\"og:description\" content=\"You can also choose to use Perl and the DBI Perl Module to access your database. A commented example: #!\/usr\/bin\/perl use DBI; # Connect To Database # * The DBI interface to MySQL uses the method &#8220;connect&#8221; to make a # * connection to the database. It takes as it&#8217;s first argument # * the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting Plus\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-03T22:55:45+00:00\" \/>\n<meta name=\"author\" content=\"webhostingplus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webhostingplus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/\"},\"author\":{\"name\":\"webhostingplus\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#\\\/schema\\\/person\\\/ac818f1a9ee343e82657049f7c97c9dc\"},\"headline\":\"How do I connect to my MySQL database using CGI?\",\"datePublished\":\"2018-03-03T22:55:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/\"},\"wordCount\":330,\"publisher\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#organization\"},\"articleSection\":[\"MySQL Databases\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/\",\"url\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/\",\"name\":\"How do I connect to my MySQL database using CGI? - Web Hosting Plus\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#website\"},\"datePublished\":\"2018-03-03T22:55:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/connect-mysql-database-using-cgi\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I connect to my MySQL database using CGI?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#website\",\"url\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/\",\"name\":\"Web Hosting Plus\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#organization\",\"name\":\"Web Hosting Plus\",\"url\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/wp-content\\\/uploads\\\/Web-Hosting-Plus-Logo.png\",\"contentUrl\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/wp-content\\\/uploads\\\/Web-Hosting-Plus-Logo.png\",\"width\":339,\"height\":78,\"caption\":\"Web Hosting Plus\"},\"image\":{\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/webhostingplus.uk\\\/hosting\\\/#\\\/schema\\\/person\\\/ac818f1a9ee343e82657049f7c97c9dc\",\"name\":\"webhostingplus\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I connect to my MySQL database using CGI? - Web Hosting Plus","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/","og_locale":"en_GB","og_type":"article","og_title":"How do I connect to my MySQL database using CGI? - Web Hosting Plus","og_description":"You can also choose to use Perl and the DBI Perl Module to access your database. A commented example: #!\/usr\/bin\/perl use DBI; # Connect To Database # * The DBI interface to MySQL uses the method &#8220;connect&#8221; to make a # * connection to the database. It takes as it&#8217;s first argument # * the ... Read more","og_url":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/","og_site_name":"Web Hosting Plus","article_published_time":"2018-03-03T22:55:45+00:00","author":"webhostingplus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webhostingplus","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/#article","isPartOf":{"@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/"},"author":{"name":"webhostingplus","@id":"https:\/\/webhostingplus.uk\/hosting\/#\/schema\/person\/ac818f1a9ee343e82657049f7c97c9dc"},"headline":"How do I connect to my MySQL database using CGI?","datePublished":"2018-03-03T22:55:45+00:00","mainEntityOfPage":{"@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/"},"wordCount":330,"publisher":{"@id":"https:\/\/webhostingplus.uk\/hosting\/#organization"},"articleSection":["MySQL Databases"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/","url":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/","name":"How do I connect to my MySQL database using CGI? - Web Hosting Plus","isPartOf":{"@id":"https:\/\/webhostingplus.uk\/hosting\/#website"},"datePublished":"2018-03-03T22:55:45+00:00","breadcrumb":{"@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webhostingplus.uk\/hosting\/connect-mysql-database-using-cgi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webhostingplus.uk\/hosting\/"},{"@type":"ListItem","position":2,"name":"How do I connect to my MySQL database using CGI?"}]},{"@type":"WebSite","@id":"https:\/\/webhostingplus.uk\/hosting\/#website","url":"https:\/\/webhostingplus.uk\/hosting\/","name":"Web Hosting Plus","description":"","publisher":{"@id":"https:\/\/webhostingplus.uk\/hosting\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webhostingplus.uk\/hosting\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/webhostingplus.uk\/hosting\/#organization","name":"Web Hosting Plus","url":"https:\/\/webhostingplus.uk\/hosting\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/webhostingplus.uk\/hosting\/#\/schema\/logo\/image\/","url":"https:\/\/webhostingplus.uk\/hosting\/wp-content\/uploads\/Web-Hosting-Plus-Logo.png","contentUrl":"https:\/\/webhostingplus.uk\/hosting\/wp-content\/uploads\/Web-Hosting-Plus-Logo.png","width":339,"height":78,"caption":"Web Hosting Plus"},"image":{"@id":"https:\/\/webhostingplus.uk\/hosting\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/webhostingplus.uk\/hosting\/#\/schema\/person\/ac818f1a9ee343e82657049f7c97c9dc","name":"webhostingplus"}]}},"_links":{"self":[{"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/posts\/859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":0,"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/posts\/859\/revisions"}],"wp:attachment":[{"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/media?parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/categories?post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostingplus.uk\/hosting\/wp-json\/wp\/v2\/tags?post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}