Get Data From MySQL Using PHP

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

Up Next
    Ebook Download
    View all
    Learn
    View all