Category: PHP

Varnam, An Editor To Easily Type Malayalam & Other Indian Languages

Today, I’m introducing Varnam Editor, an easy software for you to write Malayalam and other Indian languages. It is easily installable for GNU/Linux distros like Ubuntu, Linux Mint, Debian, Archlinux etc.
Technically, it’s an editor for transliterating Indian languages. Varnam Editor uses an existing library called Varnam and makes it easy to install & use.
Transliteration From Wikipedia :
Transliteration is a type of conversion of a text from one script to another that involves swapping letters (thus trans- + liter-) in predictable ways (such as α → a, д → d, χ → ch, ն → n or æ → ae).

How to connect to a MySQL database with PHP

This tutorial will require a mysql database and a web development environment using mysql, apache and php and a simple text editor.

The tutorial takes you through establishing a mysql connection using php on a web page, connecting to a mysql table and retrieving the results and displaying them back on the web page.

This tutorial uses the PHP MySQL commands:

  • mysqli_connect
  • mysqli_query
  • mysqli_fetch_array
  • mysqli_close

The Plan

  • make the connection and select the database
  • perform the query on the table
  • print out the data
  • close the connection

First Up – Connecting to a MySQL database

You need your MySQL server address (if the database is on the same server as the web server it will most likely be localhost or, username, password and  database name. Create a filenamehere.php file and open and close the php code with tags before the html, you can put regular html after it. Open the file in a browser and you should see nothing apart from the title tag, if you see the error the username/password or database name may be wrong.

PHP will require that mysqli is enabled (it is on most PHP set ups).


PHP connect to MySQL


The variable $db is created and assigned as the connection string, it will be used in future steps. If there is a failure then an error message will be displayed on the page. If it is successful you will see PHP connect to MySQL.


Performing a database query

The mysql query is actually performed in the body of the html page, so additional php opening and closing tags will be required. For the query we are going to specify a read of all fields from a given table. The  $query variable selects all rows in the table. You just need to use your table name.


PHP connect to MySQL

Again the returned page in the browser should be blank and error free, if you do receive the error – ‘Error querying database..’ check the table name is correct.

Put the data on the page

Here we are taking the making a $result variable which stores the query we just made above, now we just need to go through all the rows of that query which we need mysqli_fetch_array which stores the rows in an array, so now we are storing the $result in mysqli_fetch_array and passing that into a variable called $row.

The $row now can be output in a while loop, here the rows of data will be echoed and displayed on the page to when there is no longer any rows of data left, my example uses 4 fields in the table first_name, last_name, email and city.


PHP connect to MySQL

'; } ?>

Here you should see the all data as output from your table.

Closing off the connection

Closing the connection will require another set off opening and closing php tags after the closing html tag. It is good practice to close the database connection when the querying is done.


PHP connect to MySQL

'; } //Step 4 mysqli_close($db); ?>

Database connections should always be closed off. You do not need to keep the connection variable $db after the initial connection but is considered best practice.

The post How to connect to a MySQL database with PHP appeared first on Coolest Guides on the Planet.