Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday, January 30, 2016

Fetch multiple rows using PHP, MySQL

<?php    

      $dbc = mysqli_connect("localhost""my_user""my_password""my_database")  or die ("Error connecting to the mysql server. ");

      $query = "select * from my_table";

      $result = mysqli_query($dbc, $query)or die ("Could not connect to mysql server. ");
             
        $myArray = array();
    
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
             $myArray[] = $row;
        }

        $result->free(); 
    
        mysqli_close($dbc);

        //TEST TO SEE THE RESULT OF THE ARRAY 
        echo '<pre>';
             print_r($myArray);
        echo '</pre>';


?>

Sunday, November 22, 2015

A simple PHP insert code

<?php
if ($_SERVER["REQUEST_METHOD"] == 'POST'){

$firstname = $_POST["firstname"];

$dbc = mysqli_connect('localhost','root','','mydatabase') or die ("connection Error");

$query = "insert into my_table(firstname) values('$firstname')";

$myArray = array();

if (mysqli_query($dbc, $query)){
$myArray["msg"] = "happened";
}else {
$myArray["msg"] = "notHappened". mysqli_error($dbc);
}
mysqli_close($dbc);

echo json_encode($myArray);
}

?>


Note: Use your own database name in place of mydatabase and table name where my_table is specified.

Monday, January 19, 2015

Sample Ajax using jQuery & PHP

I started searching for a simple example that covers "PHP, jQuery, Ajax" and I found this web site which is simple and awesome: http://brian.staruk.me/php/2013/sample-jquery-php-ajax-script/. Thanks to Brian Staurk!

Wednesday, October 1, 2014

Learning PHP

PHP or Ruby on Rails? Which would be the easy server side language to pick up in no time? And how difficult to install it on Windows 8?

Well, these were the questions that I asked myself when I thought about a simple server side scripting language that I can use to explain some basics to a novice web developer.

I decided to go with PHP for the following reasons:

  • I read couple of chapters from a book (Head First PHP and MySQL) and went through couple of lessons in W3C Schools and felt it was easy.
  • I checked in dice.com and found more job opportunities for a PHP developer than a Ruby on Rails developers.
So....
  1. I followed this web site http://www.c-sharpcorner.com/UploadFile/47548d/how-to-install-wamp-server-on-windows-8-1/ and insalled WAMP (Windows, Apache, MySQL, PHP) server on my winodows 8.1 machine. Surprisingly, it was simple and straight forward. NOTE: Steps 1 through 8 were good enough for me and the WAMP server was up and running like an Arabian horse.
  2. From localhost index page, I opened the sqlbuddy and created a sample table in test database.
  3. I wrote the following simple program that talked to the database and provided me the response:

<?php // Connecting to a database using parameters... // mysqli_connect(hostname, username, password, database name) $dbc = mysqli_connect('localhost','root','','test') or die('Error connecting to MySQL server.'); // Select statement to get data from 'person' table $query = "select firstname, lastname from person"; // Executing the query $result = mysqli_query($dbc,$query) or die("Error querying database"); // Accessing the data that is obtained while($row = mysqli_fetch_array($result)) { echo $row['firstname'] . " " . $row['lastname']; echo "<br>"; } // Closing the database mysqli_close($dbc);
?>

So much for a day, huh? I am happy because I learnt something today!