Tag Archives: mysql

How to fill a dropdown combo box in PHP from MySQL database table

This is an easy to use code to select records from MySQL database table and display in dropdown combo box using PHP.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
mysql_select_db($db_name,$cn) or die(mysql_error());
$sql = "SELECT field_name FROM table_name";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["field_name"]."'>".$row["field_name"]."</option>";
}mysql_free_result($rs);
echo "</select>";

How to select and display MySQL records data in PHP

This is the simplest code to select and display records from MySQL database table and display in PHP.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
mysql_select_db($db_name,$cn) or die(mysql_error());

$sql = “SELECT field_name FROM table_name”;
$rs = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($rs)){

echo $field_name = $row["field_name"];
echo “</br>”;

}mysql_free_result($rs);

How to make PHP and MySQL support Arabic, Urdu and charactersets or other foreign languages

To enable your PHP/MySQL script support Arabic or Urdu or other foreign languages, you need to do the following.

In database connection, do the following.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
//Below is an additional line to enable supportĀ for foreign languages.
mysql_query(“SET NAMES utf8″) ;
mysql_select_db($db_name,$cn) or die(mysql_error());

In database, set collation of fields to utf8_general_ci. If you can set collation of databas and tables to utg8_general_ci as well, that will even be better. A sample database script is given below.


– Database: `mytest`

CREATE DATABASE `mytest` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mytest`;

– ——————————————————–


– Table structure for table `mytable`

CREATE TABLE IF NOT EXISTS `mytable` (
`my_id` int(11) NOT NULL auto_increment,
`my_title` varchar(100) NOT NULL,
PRIMARY KEY (`my_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Now you need to set the charset of the page. Add the following meta tag to the head of you web page. If a charset meta tag already exists, then change it to as below.

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>

That’s it!