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

31 Mar 2014

mysql_fetch_array in php and mysql

                        Mysql_fetch_array() returns row as an associative, numeric array or both based on parameters passed in it. Three types of parameters can be passed in mysql_fetch_array function.
                 
  1. MYSQL_BOTH - return both associative and numeric array
  2. MYSQL_ASSOC - return associative array
  3. MYSQL_NUM - return numeric array

Retrieve data from table using mysql_fetch_array() in php:


Consider following example:

                     mysql_fetch_array in php and mysql at phponwebsites

Mysql_fetch_array() with MYSQL_BOTH in php:

                       It returns  result as both associative and numeric array. You can retrieve data using both associative and numeric indices 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, MYSQL_BOTH))
{
  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:

 mysql_fetch_array with MYSQL_BOTH in php

Where,
          the array should be like this:

     Array(
                  [game_ID] => 1
                  [category_Name] => Adventures
                  [game_Title] => Achilles
               )
                        
                         OR

     Array(
                  [0] => 1
                  [1] => Adventures
                  [2] => Achilles
               )

mysql_fetch_array() with MYSQL_NUM in php:

                              It returns result as numeric array. ie, indices are numeric like 0,1,2...

<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, MYSQL_NUM))
{
  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:

mysql_fetch_array with MYSQL_NUM in php

In this type, the array of result should be like below:

     Array(
                  [0] => 1
                  [1] => Adventures
                  [2] => Achilles
               )


          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:

mysql, php at phponwebsites

mysql_fetch_array() with MYSQL_ASSOC in php:

                             It returns result as an associative array.


<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,MYSQL_ASSOC))
 {
   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:

mysql_fetch_array with MYSQL_ASSOC in php

Where,
            the array should be like this:
     Array(
                  [game_ID] => 1
                  [category_Name] => Adventures
                  [game_Title] => Achilles
               )


 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:

mysql_fetch_assoc,php, mysql at phponwebsites


Note:
          mysql_fetch_array() without array_type means return row as an associative, numeric array or both

Related Post:

13 Mar 2014

Insert data into mysql table using php

                      You can insert values to mysql table using php. The following mysql query is used for insert data into table.
                        INSERT INTO table_name VALUES(values datatype(length))


Store values into mysql table using php:

                    The following php script is used to store values into mysql table.

<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$query=mysql_query("insert into table1 values(1,'sample1', 'sample1.jpg')") or die(mysql_error());
if($query)
{
 echo 'Vaules are inserted into table';
}
else
{
 echo'Vaules are not inserted';
}
?>


Note:
          If you declared field name as varchar, then you must insert values between single quotation marks ( 'sample1').

                  Now the values are inserted into table. You can see it in your mysql table at phpmyadmin. Your mysql table look like this:

insert data into mysql tables in php

You inserted values to your table using php.

Get values from user and insert into mysql table using php:

                              Suppose you want to get values from user. First you have to know how to get values from text boxes.

How to get values from textbox using php:


$name=$_POST['name']; if(method is post)
        or
$name=$_GET['name']; if(method is get)
        or
$name=S_REQUEST['name']; if(method is either post or get)

where,
        S_POST[] is used to get values from textbox, If you use POST method.
        S_GET[] is used to get values from textbox, If you use GET method.
        S_REQUEST[] is used to get values from textbox, If you use either GET or POST method.

Then the following php code is used to insert values into table.

 <html>
 <body>
 <?php
      if(isset($_POST['submit']))
       {
            mysql_connect('localhost','root','') or die(mysql_error());
            mysql_select_db('new') or die(mysql_error());
            $id=$_POST['id'];
            $name=$_POST['name'];
            $img=$_POST['img'];
            if($id!='' && $name!='' && $img!='' )
            {
                $query=mysql_query("insert into table4 values('".$id."','".$name."', '".$img."')") or die(mysql_error());
                if($query)
                {
                   echo 'Vaules are inserted into table';
                } 
               else
               {
                   echo'Vaules are not inserted';
               }
            }
           else
           {
                echo'Insert all values';
           }
         }
?>
<form action="#" method='post'>
<table align='center' cellspacing='5'>
<tr><td>ID</td><td><input type='text' name='id'></td></tr>
<tr><td>Name</td><td><input type='text' name='name'></td></tr>
<tr><td>img</td><td><input type='text' name='img'></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='submit'></td></tr>
</table>
 </form>
</body>
</html>

Where, 
           the values will get from users using post method like as below:
            $_POST['textbox_name']

Now you run this file. Your display look like this:

insert form values into mysql table

Then you can get values, if user type anything in textbox. Now i added values as like this:

Get values from user and stored into mysql table using php

Now you'll get output like this:

store values into mysql table using php

Your mysql table have user inserted values.

Related Post:

21 Feb 2014

Create mysql database using php

                       All of you know, create mysql database in phpmyadmin. Now we are going to know how to create database using php.

The mysql query is:
            CREATE DATABASE db_name

 Then you use the particular database. The mysql query is:
                      USE db_name

The php coding as follows as:

<?php
mysql_connect('localhost','root','');
$query=mysql_query('create database new') or (mysql_error());
$qu=mysql_query('use new')  or (mysql_error());
if($query)
 {
   echo'Database is created';
 }
  else
 {
   echo'Database is not created';
  }
?>

Where,
           - mysql_connect() is used to connect the database.
           - 'localhost' is server name.
           - 'root' is user name of database.
           - '' represent the password of database.
           - mysql_query() is used for execute the mysql query.
           - 'new' is the database new.
           - mysql_error() give error message while the mysql query is execute.
           - die() terminate the excution.

       Now you'll get output as " Database is created ". Then you can see the new database in your phpmyadmin.

create database in mysql using php at phponwebsites

      The database 'new' is created and ready for use.

Related Post: