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.

31 comments:

  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
  2. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me
    best rpa training in chennai
    rpa training in chennai |
    rpa online training
    rpa course in bangalore
    rpa training in pune
    rpa training in marathahalli
    rpa training in btm

    ReplyDelete
  3. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me
    best rpa training in chennai
    rpa training in chennai |
    rpa online training
    rpa course in bangalore
    rpa training in pune
    rpa training in marathahalli
    rpa training in btm

    ReplyDelete
  4. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    python course in pune | python course in chennai | python course in Bangalore

    ReplyDelete
  5. Thanks you for sharing this unique useful information content with us...
    Informatica Bangalore

    ReplyDelete
  6. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

    data analytics courses

    data science interview questions

    business analytics courses

    data science course in mumbai

    ReplyDelete
  7. Thanks for sharing such knowledgeable information on AWS. Looking forward to see more of such informative posts.
    AWS Training In Hyderabad

    ReplyDelete
  8. I like viewing websites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

    Correlation vs Covariance

    ReplyDelete
  9. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.

    artificial intelligence course

    ReplyDelete
  10. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.

    ReplyDelete
  11. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community. data science courses

    ReplyDelete
  12. Glad to chat your blog,I seem to be forward to more reliable articles and i think we all wish to thank so many good articles,blog to share with us.data scientist courses

    ReplyDelete
  13. You can take pride in the way you write such brilliant articles. It is reflected in the pattern of writing when a writer does the required homework to write quality material. Thank you so much for sharing this magnificent content.
    SAP training in Mumbai
    SAP course in Mumbai

    ReplyDelete
  14. I have truthfully never come across such overwhelmingly good piece of content. I agree with your valid points and your ideas. This piece of information is really great.
    Thank you so much.
    SAP training in Kolkata
    SAP course in kolkata

    ReplyDelete
  15. Your site is genuinely cool and this is an unprecedented moving article and If it's not all that much difficulty share more that way. Much thanks to you…

    AI Training in Hyderabad

    ReplyDelete
  16. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best.
    data analytics course in hyderabad

    ReplyDelete
  17. This post is incredibly helpful for those looking to perform CRUD operations with PHP on BlueMix. The detailed code examples, explanations, and provided links make it a valuable resource for developers. Thank you for sharing this insightful guide!
    Data Analytics Courses in Nashik

    ReplyDelete
  18. Hello Blogger,
    This article provides a clear and detailed guide on using PHP for CRUD operations on BlueMix, making it easier for developers to work with MySQL databases in the cloud. Good job.
    Data Analytics Courses In Dubai

    ReplyDelete
  19. Hey! Very interesting blog, and post is a valuable addition to the resources available for developers looking to leverage BlueMix for their projects! Thank you!
    Data Analytics Courses In Chennai

    ReplyDelete
  20. The use of PHP for CRUD operations on BlueMix is explained in precise detail in this article, making it simpler for developers to interact with MySQL databases in the cloud. Well done.
    Data Analytics Courses in Agra

    ReplyDelete
  21. This blog could be a game-changer for those looking to master the basics of Create, Read, Update, and Delete operations in PHP on the BlueMix platform.
    Digital marketing courses in illinois

    ReplyDelete
  22. Thanks for sharing valuable and insightful blog on PHP CRUD operation on BlueMix.
    Digital Marketing Courses in Italy

    ReplyDelete
  23. I found the blog post on PHP CRUD operation on BlueMix very interesting and informative.
    data analyst courses in limerick

    ReplyDelete
  24. Thank you for sharing knowledgeable post on PHP CRUD operation on BlueMix.
    Investment banking training Programs

    ReplyDelete
  25. The content of your blog is precise and straightforward, which makes it easy to understand.
    Data analytics framework

    ReplyDelete