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

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:

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:

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:

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:

9 Dec 2013

Create mysql database in cpanel

                       1.You have to login to your cpanel to create mysql database.
                       2.Then you need to select mysql databases.

Mysql database and cpanel at phponwebsites

                       3.Type your database name. Then click create database button. Here, my database name is 'new'.

create Mysql database at phponwebsites

Now you'll get successful message as created database successfully as follows as:

create database in cpanel
                   
                          4.Then you have to create new user. Type your user name here.

create Mysql user in database and cpanel at phponwebsites

Then click password generator to get password for your user name in mysql. The pop up box is opened as follows as.


create Mysql user at phponwebsites


Select the checked box and click use password button. Then you the strength password is loaded. Then click create user button.

Mysql database and cpanel at phponwebsites

Now the user name is created.
                   5. Then you need to add your user name to mysql database. Select your user name and database as follows as. then click Add button.

add user to Mysql database and cpanel at phponwebsites

                  6. Now you select all the privileges. Then click Make changes button.

all previleges to mysql user


                     7. Finally you will get a message like this:

Mysql database created in cpanel at phponwebsites

Now you created a mysql database , user in cpanel and add user to mysql database. Then you back to home and click phpmyadmin.


create Mysql database in cpanel


Then you can create table in your mysql database.