ARTICLE

Get Data From MySQL Using PHP

Posted by Vinod Kumar Articles | PHP March 06, 2013
In this article I explain Get data from MySQL using jQuery with PHP.
Reader Level:

Introduction

In this article I explain Get data from MySQL using jQuery with PHP. I am using a simple HTML form and a JavaScript function with a MySQL table for creating this phenomenon. Such that the user enters a name and fetch your table user enter name correct or not.  

image2.jpg

First of all, create a new database and table in your MySQL and insert some data. Such as:

image1.jpg

Then create a HTML form for displaying the data, such as:

<html>

<head>

<title>Untitled Document</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript">

    function get() {

        $('#age').hide();

        $.post('data.php', { name: form.name.value },

        function (output) {

            $('#age').html(output).fadeIn(1000);

        });

    }

</script>

</head>

<body bgcolor="#B096FE">

</p>

<form name="form" method="post">

<input type="text" name="name">

<input type="button" value="get" onclick="get();">

//show your data in div section though 'id'

<div id="age"></div>

</form>

</p>

</body>

</html>

 

In this form I provided a JavaScript function in the head section.

 

This is a PHP script file. In this file is your database connection.

   

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

$name= mysql_real_escape_string($_POST['name']);

$age= mysql_query("select age from vinod.people where name='$name'");

$age_num_rows=mysql_num_rows($age);

if($name==null)

echo "please enter name!";

else

{

if($age_num_rows==0)

echo "name does not exist!";

else

{

$age=mysql_result($age,0);

echo "$name age is $age";

}}

?>

You can use this article just like that. When enter a blank text box then a message like this is shown:

image3.jpg

When the user enters the wrong name then a message like this is shown:

image4.jpg

When the user enters the correct name then a message like this is shown:

image5.jpg

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
Infragistics is experts in technology and design, and passionate about helping you build highly performant and stylish applications that solve problems, deliver inspiration, and maximize results.
HTML 5 + JQUERY CONTROLS
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
HTML 5 + JQUERY CONTROLS