Tuesday, July 8, 2014

PHP CRUD operation on BlueMix

When you want do to an application to initialize the database (including create table and date),I think use PHP is a quick solution to do that.I was trying to find this kind of sample,fortunate I find one in jazzHub,so you can follow it to do your mysql solution on BlueMix.

The project  url is at https://hub.jazz.net/project/andrewhujazz/PHPMySQLProject/overview.You can apply a jazzhub account to fork his application to deploy on the BlueMix,or use git clone command to clone the project on your local side.The git url is https://hub.jazz.net/git/andrewhujazz/PHPMySQLProject.

The detail function you can see below screenshot,it provide create table,insert table,drop table,

When you write code you need to notice ,when you open the mysql connection,you also need to provide port,since the port might be not default port..I list some code for your reference.

<div id="content2">
<h2>MySQL Service</h2><hr>
<b>MySQL Information</b>
<hr>
<?php
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql-5.5"][0]["credentials"];
$db = $mysql_config["name"];
$host = $mysql_config["host"];
$port = $mysql_config["port"];
$username = $mysql_config["user"];
$password = $mysql_config["password"];
if ($host=="") {
echo "<b> There is no mysql service.</b><br>";
} else {
?>
MySQL DB Name : <?php echo $db ?> <br>
MySQL Host : <?php echo $host ?> <br>
MySQL Port : <?php echo $port ?> <br>
MySQL username : <?php echo $username ?> <br>
MySQL password : <?php echo "********" ?><br>
<hr>
<b> Create Table</b><br>
<?php
$conn = mysql_connect($host . ':' . $port, $username, $password);
if(! $conn ) {
 echo('Could not connect: ' . mysql_error());
}
mysql_select_db($db);
 if ($cmd=="create") {
$sql_create = 'CREATE TABLE SAMPLE (name CHAR(30), comment CHAR(255), time DATETIME)';
$retval = mysql_query($sql_create, $conn );
if(! $retval ) {
 echo('Could not create database table: ' . mysql_error() . '<br/>');
} else {
    echo "<script>alert('Created database table successfully')</script><script>window.location='mysql.php'</script>";
    }
 } ?>
<a href="mysql.php?cmd=create">[Create] </a>
<hr>
<b> Insert Data</b><br>
<?php
$sql_read = 'SELECT * FROM SAMPLE ';
$retval = mysql_query($sql_read, $conn );
if(! $retval )
{
  echo('Could not read SAMPLE table: ' . mysql_error());
}
echo "<table><tr><td>name</td><td>comment</td><td>Date</td>";
while ($dbfield = mysql_fetch_assoc($retval)) {
    echo "<tr><td>".$dbfield['name'] . '</td>';
    echo "<td>".$dbfield['comment'] . '</td/>';
    echo "<td>".$dbfield['time'] . '</td></tr>';
}
echo "</table>";
?>

<?php
if ($cmd=="insert") {
$sql_insert = 'INSERT INTO SAMPLE (name, comment, time) VALUES ( "name",   "comment", NOW() )';
$retval = mysql_query($sql_insert, $conn );
if(! $retval ) {
 echo('Could not enter data: ' . mysql_error() . '<br/>');
} else {
    echo "Entered data successfully<br/><script>window.location='mysql.php'</script>";
    }
 } ?>
<a href="mysql.php?cmd=insert">[Insert] </a>
<hr>
<b> Drop Table</b><br>
<?php
 if ($cmd=="drop") {
$sql_drop = 'DROP TABLE SAMPLE';
$retval = mysql_query($sql_drop, $conn );
if(! $retval ) {
 echo('Could not drop database table: ' . mysql_error() . '<br/>');
} else {
    echo "<script>alert('Dropped database table successfully')</script><script>window.location='mysql.php'</script>";
    }
 } ?>
<a href="mysql.php?cmd=drop">[Drop] </a>
<?php }?>
</div>

Also when you deploy your php application,you had better provide a manifest.yml,it describe the deployment info on the BlueMix.See below sample manifest.yml

---
applications:
- name: myphpsample
  memory: 128M 
  instances: 1
  host: myphpsample
  domain: ng.mybluemix.net
  buildpack: https://github.com/dmikusa-pivotal/cf-php-build-pack.git
  path: .

After deployment,you need to bind your php application on existing my sql service.

1 comment:

  1. Great Post, Actually PHP is a beautiful source for developing a database driven web application, I love this post, thanks for spending your time for discussing about this topic.
    Regards,
    PHP Training in Chennai|PHP Course in Chennai

    ReplyDelete