PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: select
select - phponwebsites.com
Showing posts with label select. Show all posts

13 Dec 2015

Get multiple selected values from drop down box using PHP

                           You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Now the doubt is rise. How to select multiple values from drop down box and how to get multiple selected input data from drop down box in PHP. This post describes retrieve multiple selected values from drop down box using PHP.

How to select multiple values from drop down box in HTML


                           You know select single value in drop down box. Then how to select multiple values in drop down box. You have to add attribute "multiple" and set name as array in drop down box in order to select multiple values from drop down box. If you want to select more than one values in drop down box, then press "ctrl" while selecting values in drop down box. Let consider below example to make HTML formwith select multiple values in drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
</body>
</html>

Retrieve multiple selected values from drop down box using PHP


                                 You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Then display each selected items through foreach loop in PHP. The PHP script describes how to select multiple selected values from drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
<?php
 if(isset($_POST['submit']))
 {
  $country=$_POST['country'];
  foreach($country as $val)
  {
    echo $val.'<br>';
  }
 }
?>
</body>
</html>

               
                                      While submitting form with selected multiple values from drop down box, you can get multiple selected values using PHP.

13 Aug 2015

Get value from drop down box using PHP

                        The drop down box allows you to select one or more values from it. The HTML tag "select" is also known as "drop down" or "pul down" box.You can get value from drop down box using PHP while form submitting.

HTML form with drop down box


                        The below example describes how to create HTML form with drop down box.

<html>
<body>
<form action="#" method="post">
  <select name="country">
     <option value="USA">USA</option>
     <option value="United Kingdom">United Kingdom</option>
     <option value="France">France</option>
     <option value="Russia">Russia</option>
     <option value="India">India</option>
  </select>
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>

Retrieve input data from drop down box using PHP


                       You can retrieve input data from drop down box using following types in PHP.
             $_POST['drop_down_name'] (if method is post)
             $_GET['drop_down_name'] (if method is gett)
             $_REQUEST['drop_down_name'] (if method is either post or get)
                         The following PHP script describes how to retrieve input data from drop down box while form submitting.

<html>
<body>
<form action="#" method="post">
   <select name="country">
      <option value="USA">USA</option>
      <option value="United Kingdom">United Kingdom</option>
      <option value="France">France</option>
      <option value="Russia">Russia</option>
      <option value="India">India</option>
   </select>
   <input type="submit" name="submit" value="Submit">
</form>
<?php
      if(isset($_POST['submit']))
      {
         $country=$_POST['country'];
         echo'You selected '.$country;
       }
?>
</body>
</html>

                             While submitting form with select country from drop down box, you can get selected country as output.

Related Post:

21 Mar 2015

Retrieve images from mysql database using php

                       Already you know  how to upload images to mysql database using php. Otherwise, visit upload images to mysql database using php.

Retrieve images from database:

                        Normally you can retrieve data from mysql table using php. Then you print the result. Similarly you retrieve image name from mysql database and print image name with located folder in '<img>' tag.

Consider following mysql table.


Retrieve images from mysql database

 The php script for retrieve images from mysql database is:


<?php
        mysql_connect('localhost','root','') or die(mysql_error());
        mysql_select_db('new')  or die(mysql_error());
        $q=mysql_query("select * from image where no=1 ")  or die(mysql_error());
        $res=mysql_fetch_array($q);
?>
    <img src="<?php echo 'http://localhost/upload/image/'.$res['img_name'];?>">

   
      where,
                 mysql_query() select the row which is no '1' from mysql database.
                 mysql_fetch_array() return the result an associated array. You want to know about  mysql fetch array().
                 In your img src print image name with location of image. Now you''ll get output like this:


phponwebsites

Related Post:

2 Apr 2014

Retrieve data from mysql table using php

                       We are going to see how can fetch the result from mysql table using php.
             1. First you need to connect your Mysql database using php script like below:

mysql_connect('server_name','username','password');
mysql_select_db('db_name');

where,
           - server_name means localhost,
           - username is username of your database
           - password is password of your database
           - db_name is name of your database

          2. Then select the values which is present is table using mysql query.
              " SELECT * FROM table_name "
Use above query, if you select all values in table. Otherwise you need to use 'WHERE' clause to retrieve desired data from table.

          3. Then you have to use MYSQL function to retrieve data from table such as mysql_fetch_array(), mysql_fetch_row(), and mysql_assoc.
          where, 
                   mysql_fetch_array() returns row as numeric array, associative array or both.
                   mysql_fetch_row() returns numeric array corresponds to fetched row.
                   mysql_fetch_assoc() returns row as an associative array.

Consider following example:


                     Fetch result from mysql table using php at phponwebsites

Fetch results from mysql table using mysql_fetch_array() in php:


<html>
<body>
<table>
<th>Game_ID</th><th>Category</th><th>Title</th>
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$query=mysql_query('select * from table1')  or die(mysql_error());
while($res=mysql_fetch_array($query))
{
  echo'<tr><td>'.$res['game_ID'].'</td><td>'.$res[1].'</td><td>'.$res['game_Title'].'</td></tr>';
}
echo'<table>';
?>
</body>
</html>

         Now you'll get output like this:


Fetch result from mysql table using mysql_fetch_array in php

Fetch result from mysql table using mysql_fetch_row() in php:


<html>
<body>
<table>
<th>Game_ID</th><th>Category</th><th>Title</th>
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$query=mysql_query('select * from table1') or die(mysql_error());
while($res=mysql_fetch_row($query))
{
  echo'<tr><td>'.$res[0].'</td><td>'.$res[1].'</td><td>'.$res[2].'</td></tr>';
}
echo'<table>';
?>
</body>
</html>

           Now you'll get output like this:

Fetch result from mysql table using mysql_fetch_row in php

       
      Suppose you'll use
echo'<tr><td>'.$res[0].'</td><td>'.$res[1].'</td><td>'.$res['game_Title'].'</td></tr>';

That means you use associative index instead numerical, then you will get undefined index error like as follows as:

retrieve data from mysql table using mysql_fetch_row in php

Fetch results from mysql table using mysql_fetch_assoc() in php:


<html>
<body>
<table>
<th>Game_ID</th><th>Category</th><th>Title</th>
<?php
mysql_connect('localhost','root','')  or die(mysql_error());
mysql_select_db('new')  or die(mysql_error());
$query=mysql_query('select * from table1')  or die(mysql_error());
while($res=mysql_fetch_assoc($query))
{
  echo'<tr><td>'.$res['game_ID'].'</td><td>'.$res['category_Name'].'</td><td>'.$res['game_Title'].'</td></tr>';
}
echo'<table>';
?>
</body>
</html>

           Now you'll get output like this:

Fetch result from mysql table using mysql_fetch_assoc in php

 Suppose you'll use
echo'<tr><td>'.$res[0].'</td><td>'.$res['category_Name'].'</td><td>'.$res['game_Title'].'</td></tr>';

That means you use numeric index instead associative, then you will get undefined offset error like as follows as:

Retrieve data from mysql table using mysql_fetch_assoc in php


          Finally you can retrieve data from mysql table using php.

3 Feb 2014

Select distinct values in mysql

                       If your column in mysql table contains duplicate values, then you need to display distinct values by mysql command 'DISTINCT' and 'GROUP BY'.


Select unique values in Mysql by distinct:


                       The mysql command 'DISTINCT' is used to select distinct values. The mysql query for select distinct values as follows as:

                 SELECT DISTINCT column_name FROM table_name

Consider the following table. table1 is name of this table.

select distinct values in mysql

                              Now we are going to get unique values in column 'id' by 'DISTINCT' mysql command like this:

                      SELECT DISTINCT id FROM table1

Now you'll get output like this:

select unique values using distinct in mysql

                    You can see the table 'table1' which have unique values.

Select unique values in Mysql by 'Group by':

                   
                              You can also display unique values using 'GROUP BY' mysql command. If you want to select distinct values from one column and also other values from table, then you can use mysql command 'GROUP BY'.  The mysql query is:

                       SELECT * FROM table_name GROUP BY column_name 

 Now we are going to get unique values in column 'id' by 'GROUP BY' mysql command like this:

                       SELECT * FROM table1 GROUP BY id 

Now you'll get output like this:

display unique values using group by in mysql


                           You can see the column 'id' values in mysql table 'table1' are unique.

Related Post:

31 Jan 2014

Use order by before group by in mysql

                       Normally you can use order by before group by in mysql. Now the question is rise, can you use order by before group b in mysql? Yes you can use order by before group by in mysql. It is possible. You've to use subquery for this. ie,

      SELECT * FROM (
                   SELECT * FROM `table` ORDER BY column_name DESC 
                                    )    t
                                       GROUP BY column_name
Where,
               t is alias because you use subquery. If you don't add it, then you'll get error message "Every derived table must have its own alias".


Group by in Mysql:


                                      Consider the following table. table1 is name of this table.

use order by before group by in mysql

                              Now we are going to get unique values in column 'id' by 'GROUP BY' mysql command like this:

                       SELECT * FROM table1 GROUP BY id 

Now you'll get output like this:

display unique values using group by in mysql


                           You can see the column 'id' values in mysql table 'table1' are unique. The id value 1 are present 1st, 2nd and 5th row. But it takes only the first row.


Use order by before group by:


                            On above example, you want to select highest count of each id and id should be unique. Then you have to use subquery to select highest count values. The mysql query as follows as:

      SELECT * FROM (
                   SELECT * FROM `table1` ORDER BY count DESC 
                                    )    t
                                       GROUP BY id

where,
                 SELECT * FROM `table1` ORDER BY count DESC  run first. It ordered the data in descending order by count.
                 SELECT * FROM ( ... ) t GROUP BY id it takes unique values.
Now you'll get output like this:
               
order by before group by in mysql

                  Now you'll get id with highest count values.


Related Post:

5 Jan 2014

Group by in mysql table

                       If your column in mysql table contains similar values, then you will use 'GROUP BY' mysql command to display unique values. It is used to eliminate the duplicate values in mysql table. It takes only  first row value from top to bottom in mysql table. The mysql query as follows as:

                       SELECT * FROM table_name GROUP BY field_name


Group by in Mysql:


                                      Consider the following table. table1 is name of this table.

group by in mysql

                              Now we are going to get unique values in column 'id' by 'GROUP BY' mysql command like this:

                       SELECT * FROM table1 GROUP BY id 

Now you'll get output like this:

display unique values using group by in mysql


                           You can see the column 'id' values in mysql table 'table1' are unique. The id value 1 are present 1st, 2nd and 5th row. But it takes only the first row.


Group by multiple columns in mysql:


                           You can group the multiple columns in single mysql query.  The mysql query as follows as:

                       SELECT * FROM table1 GROUP BY id , count 

             Where,
                        - The column 'id' got the first preference to group.
                        - Then the column 'count' is grouped.
Your output like this:

group by multiple columns in mysql

                        You can see the mysql table 'table1' which is grouped by both id and count. But you can't the get unique values in both mysql columns.

Related Post:

3 Jan 2014

Order by in mysql table

                      If your column values in mysql table contains more rows, then you can't get the highest and lowest values. On that time, you can sort the column values ascending and descending by 'ORDER BY' mysql command with 'ASC' and 'DESC'. The mysql query for sort column values in mysql table as follwos as:
                       SELECT * FROM table_name ORDER BY field_name ASC( for ascending)
                       SELECT * FROM table_name ORDER BY field_name DESC( for descending)


Order by in Mysql:


                                      Consider the following table. table1 is name of this table.

order by in mysql

                              Now we are going to sort column 'id' in ascending order. The mysql query for this:

                       SELECT * FROM table1 ORDER BY id ASC

Now you'll get output like this:

sorting column values using OREDER BY in mysql

                           You can see the column 'id' values are sorting in ascending order.


Order by multiple columns in mysql:


                           You can sort the multiple columns in single mysql query. The mysql query as follows as:

                       SELECT * FROM table1 ORDER BY id ASC, date DESC 

             Where,
                        - The column 'id' got the first preference to sort
                        - Then the column 'date' is sort
Your output like this:

order by multiple columns in mysql

                        You can see the mysql table 'table1' which is sorted by id in ascending order and date in descending order.

Related Post:

1 Jan 2014

Distinct in mysql

                       The 'DISTINCT' mysql cmmand is used to eliminate duplicate values in a column. The mysql query as follows as:
                   
                      SELECT DISTINCT field_name FROM table_name


Distinct in Mysql:


                                     Consider the following table. table1 is name of this table.

distinct in mysql

                              Now we are going to get unique values in column 'id' by 'DISTINCT' mysql command like this:

                      SELECT DISTINCT id FROM table1

Now you'll get output like this:

unique values using distinct in mysql

                    You can see the table 'table1' which have unique values.


Distinct multiple column in Mysql:


                    You can use multiple column for 'DISTINCT' in a query. But you can't get answer as you want.
The mysql query as follows as:

                      SELECT DISTINCT id, count FROM table1

You'll get output like this:

distinct multiple columns in mysql

15 Dec 2013

Copy values from one database to another in mysql

                       All of you know, you can copy data within a table and between tables. But can you copy data between two database in mysql. Yes, you can done it in mysql. The following mysql query is used for do this.
Mysql Query:
                         INSERT INTO database2.table_name (column_name)
                              SELECT table_name.column_name FROM table_name


Copy data from one database to another in mysql:


                       Consider following example. You've two databases named with db1 and db2. Each database have a table. Table1 is presented in db1 and Table2 is present in db2.
The Table1 in db1 is look like this:

Mysql at phponwebsites

The Table2 in db2 look like this:

Mysql at phponwebsites

Now we copy data from database db1 to db2. The mysql query is:
                     
                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1

Now you will get output as follow as:

copy values from one database to another in Mysql at phponwebsites

                   Now you'll get values in Table2 as like as Table1 in db.

Copy data from one database to another with specific condition in mysql:


                  You can copy only specific values from one database to another in mysql using 'WHERE' condition in mysql. Consider a example: Now we are going to copy named with 'sample1' and 'sample2' from database db1. The mysql query is:

                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1
                              WHERE Game_name!='sample3'

Now you'll get output as follow as:

copy values from one database to another with specific condition in Mysql at phponwebsites

Note:
          You can copy the values from one database to another using  this method only if two databases are in same server.

Related Post:

11 Dec 2013

Copy values from one table to another in mysql

                       We can easily copy the values within a table. But can we copy the values from one table to another table?. Yes, you can done it in mysql. The mysql query is:

                       Insert into table2 (column_name1, column_name2) 
                               select column_name1, column_name2 from table1

Consider the example:
 Table1 is name of this table.

Mysql at phponwebsites


Table2 is name of this table.


Mysql at phponwebsites

Copy values from one table1 to table2 in mysql:

                         Now you have to add values from 'Table1' to 'Table2'. The following mysql query is used to do it.
                         Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1

The output of  'Table2' is:

copy values from one table to another in Mysql at phponwebsites

Now you'll get values in 'Table2' which is same as 'Table1'.

Copy values from one table to another except some values in mysql:


                        If you want to some coulmn values, then you will add only required column name only in mysql query. Suppose you need only particular values. Then you use 'where' clause to get your answer. The mysql query as follow as:
                        
                        Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1  
                                where  Game_Name='sample1' and Game_Name='sample2'

Then the output look like this:


copy specific values from one table to another in Mysql at phponwebsites


Now you will get values only named in sample1 and sample2.

Related Post: