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

13 Apr 2014

Display results in html table using php and mysql

                       Normally, you displayed data results without order in php. If you want to display single data, then you can easily display it using ' <?php echo $name; ?> '. But if you have to display large amount of data from mysql table, then you should be display it properly. So only, you can understand the displayed results. So the best option for display data is table. You can display results orderly in table using php. The following mysql query is used to select the values from mysql table.

                        SELECT * FROM table_name


Display results in table using php and mysql:

                       

                           You can retrieve data from mysql table using php. The php script for display data in table as follows as:


<html>
<body>
<style type="text/css">
th,td{
border-width:0px 1px 1px 0px;
}
</style>
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new')  or die(mysql_error());
$query=mysql_query("select * from table1 limit 0,10")  or die(mysql_error());
echo'<table border="1" ><th >Reg.Id</th><th>Name</th><th>Category</th>';
while($res=mysql_fetch_array($query))
{
  echo'<tr><td>'.$res['game_ID'].'</td><td>'.$res['game_Title'].'</td><td>'.$res['category_Name'].'</td></tr>';
}
echo'</table>';
?>
</body>
</html>


Now you'll get output like this:


Display data in table using php

                         Now you can get your results in table.

9 Apr 2014

Get database list in mysql using php

                       You can retrieve databases in your mysql server using php. On mysql server, you can use mysql query in order to get databases in mysql.

Get database list using mysql query:

                                   The following mysql query is used to display database list.

             SHOW DATABASES

It displays database list in mysql as like below:

show databases in mysql using php


Get database list in mysql using php:


                         You can display database list in mysql using php in two ways.
           1.  Display database list using php and mysql query
           2.  Display database list using php and mysql_list_tables()

Display database list using php and mysql:


                                      Now you can find the database list in mysql server using php scripts:


<?php
$link=mysql_connect('localhost','root','')  or die(mysql_error());
$result = mysql_query("SHOW DATABASES")  or die(mysql_error());     
while ($row = mysql_fetch_array($result)) {      
    echo $row[0]."<br>";      
}
?>

                                                                 OR


Display databases in mysql using php and mysql_list_dbs():


                           The php code for display database list:


<?php
$link=mysql_connect('localhost','root','') or die(mysql_error());
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list))
{
     echo $row->Database . "<br>";
}

?>

Now you'll get output like below:

display databases available in mysql using php

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.

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:

24 Mar 2014

mysql_fetch_assoc in php and mysql

                       Mysql_fetch_assoc() return row as an associative array. Here, we can use numeric indices to get values from mysql data using php.

Consider following example:

                     mysql_fetch_assoc in php and mysql at phponwebsites

Retrieve data from table using mysql_fetch_assoc() in php:

                       You can retrieve data from mysql table using mysql_fetch_assoc() in php. The following php script is used for retrieve data.

<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:

mysql_fetch_assoc in php


Where,
            the result of 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 at phponwebsites

Where,
              $res[0] represents the first column of mysql table. But you can't use this to retrieve data from table. Because you used mysql_fetch_assoc() mysql function to retrieve data.

Related Post:

19 Mar 2014

Avoid duplicate values storing in table using php and mysql

                       We can avoid duplicate values storing in table when we insert data to table. Because the duplicate entry is one of the main problem, while insert data into mysql table . We removed duplicate values using 'DISTINCT' key in mysql query, if it is present in table. We can check whether the value is present in table or not using PHP and Mysql before add data to table.

                        We can also avoid duplicate values storing in mysql table while inserting by primary key.

Consider following example. You've a database named with 'new' and table named with 'user'. You try to add names in user table. Now you've to check the values is present or not while you inserting. The table look like this:

Avoid duplicat values sotring in table using PHP at phponwebsites

Now you try to add name 'guru' again. You can check whether the name 'guru' is present in table or not using below PHP script.

<?php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','');
mysql_select_db('new');
$name=$_POST['name'];
$query=mysql_query("select * from user where name='".$name."' ") or die(mysql_error());
$duplicate=mysql_num_rows($query);
   if($duplicate==0)
    {
      $query1=mysql_query("insert into user values('".$name."')")  or die(mysql_error());
    }
    else
    {
      echo'The name '.$name.' is already present in the user table';
    }
}
?>
<html>
<body>
<form method='post' action='#'>
name: <input type='text' name='name'>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>

                           
     Where, 
               mysql_query() - execute the query and select if any similar values are present in table.                                    mysql_num_rows() - returns numeric value of selected rows. If its value is 0, then there is no similar values present in table. Otherwise the value is already present in table.
     Where, 
               you can also use  mysql_fetch_row() instead of mysql_num_rows().                       
                        If the value is present in table, it could not be stored in table. Now you can avoid duplicate entries in table using PHP and Mysql.
                
Related Post:

How to store values into mysql table using php

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: