AJAX ADD & RETRIEVE MYSQL RECORDS USING JQUERY & PHP

Leave a Comment
I love jQuery framework, feels like we can do some awesome things easily. If you are familiar with the basic of jQuery framework too, the next thing you have to do is learn to use jQuery Ajax to add and retrieve records from MySql database. Let’s take a look at how we implement real time add and retrieve records using Ajax.

Database
sample database comment table columns id, name and message



comment.php


process.php

Try to input some comments using that form, here’s what i got



The record i just submitted is successfully added to the database. Although we can’t see right now but it can be checked directly in your MySql.
What is the value of the comment feature of web app if visitors can’t see their own comment. Now, this is our time to be a hero… :D
Edit both files that we have created to add the ability to retrieve records from database.


comment.php
process.php


Now it is more cool with visitor’s comment directly show up after Send Button is clicked



C# and MySql : Retrieve Data using MySqlDataReader

Leave a Comment
C# and MySql : Retrieve Data using MySqlDataReader

       
  
 String str = @"server=localhost;database=yourDBname;userid=root;password=yourDBpassword;";
MySqlConnection con = null;
//MySqlDataReader Object
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open(); //open the connection
//We will need to SELECT all or some columns in the table
//via this command
String cmdText = "SELECT * FROM myTable";
MySqlCommand cmd = new MySqlCommand(cmdText,con);
reader = cmd.ExecuteReader(); //execure the reader
/*The Read() method points to the next record It return false if there are no more records else returns true.*/
while (reader.Read())
{
/*reader.GetString(0) will get the value of the first column of the table myTable because we selected all columns using SELECT * (all); the first loop of the while loop is the first row; the next loop will be the second row and so on...*/
     Console.WriteLine(reader.GetString(0));
}
}
catch (MySqlException err)
{
Console.WriteLine("Error: " + err.ToString());
}
finally
{
if (reader != null)
{
     reader.Close();
}
if (con != null)
{
     con.Close(); //close the connection
}
} //remember to close the connection after accessing the database
       
 

C# and MySql : SELECT , UPDATE , DELETE

Leave a Comment

To Execute Some MySql Statements:
SELECT:
       
  
 //This is the simple code of executing MySql Commands in C#
String cmdText = "SELECT id,name,contact FROM myTable"; //This line is the MySql Command
MySqlCommand cmd = new MySqlCommand(cmdText, con);
cmd.ExecuteNonQuery(); //Execute the command
       
 

UPDATE:
       
  
 //example on how to use UPDATE
cmd = new MySqlCommand("UPDATE ab_data SET banned_from='" + from + "' , banned_until='" + until + "' WHERE name='" + name + "'", con);
cmd.ExecuteNonQuery();
       
 

DELETE:
       
  
 //example on how to use DELETE
cmd = new MySqlCommand("DELETE FROM tbName WHERE colName = someValue",con);
cmd.ExecuteNonQuery();
       
 

*Note: before executing some Sql statements or accessing/manipulating the database, make sure that the connection to the MySql Database server is opened via con.Open(); Remember also to safely close it using con.Close();

Accessing the MySql database is made easier in .NET using MySQL Connector/NET. It should be installed in your system and should be an added reference in your project. Also, Do not forget to import or use it in your code using the statement in C#:
 
       
  
 using MySql.Data.MySqlClient;
       
 

Attached is a sample project that you may try and learn from it.
Attached File  CSharpMySql.rar   23.21KB  

Questions and Comments are welcome!
Thanks!

C# : To Connect to a MySql Database

Leave a Comment
Things Needed:
You should have installed MySQL and MySQL Connector/NET. You can download installers from http://dev.mysql.com/downloads
Microsoft Visual C# or Visual Studio.
Download and install MySql for Windows.
DownloadHere

To use the methods in the MySQL Connector/NET you should add a reference to it. Right click your project in the Solution Explorer and click Add Reference… In the .NET tab, chose MySql.Data and click ok.

       
  
using System;
using MySql.Data.MySqlClient;

public class Example
    {

    static void Main()
        {
       
        String str = @"server=localhost;database=project;userid=root;password=;";
        MySqlConnection con = null;
        try
            {
            con = new MySqlConnection(str);
            con.Open(); //open the connection
            Console.Write("Sucess");
            }
        catch (MySqlException err) //We will capture and display any MySql errors that will occur
            {
            Console.WriteLine("Error: " + err.ToString());
            }
        finally
            {
            if (con != null)
                {
                con.Close(); //safely close the connection
                }
            }
        Console.ReadLine();
        }
    }