What is to be done, to add JSON CODE to php code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyamisno1
    New Member
    • Oct 2012
    • 1

    #1

    What is to be done, to add JSON CODE to php code?

    Here in my code data is coming directly from SQL to my page. but i want data should be accessed through JSON, in between MYSQL & PHP file.. so what i have to change in following code???

    PHP Code:

    Code:
    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("test", $con);
    
    $sql="SELECT * FROM artist_details WHERE name = '".$q."'";
    
    $result = mysql_query($sql) ;
    
    $num=mysql_numrows($result);
    if($num!=0)
    {
    echo "<table border='5'>
    <tr>
    <th>n</th>
    <th>a</th>
    <th>s</th>
    <th>p</th>
    </tr>";
    
    
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['n'] . "</td>";
    echo "<td>" . $row['a'] . "</td>";
    echo "<td>" . $row['s'] . "</td>";
    echo "<td>" . $row['p'] . "</td>";
    echo "</tr>";
    }
    
    
    echo "</table>";
    }
    else
    die('record not found');
    
    mysql_close($con);
    ?>

    HTML CODE:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function showUser(str)
    {
    
    if (str=="")
    {
    document.getElementById("txtHint").inn…
    return;
    } 
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").inn…
    }
    }
    xmlhttp.open("GET","http://localhost/n…
    xmlhttp.send();
    }
    </script>
    </head>
    <body bgcolor="ffcccc">
    <b>
    <form align='center'>
    Select a person:
    <select name="users" onchange="showUser(this.value)">
    <option value="i">i</option>
    <option value="d">d</option>
    <option value="r">r</option>
    <option value="a">a</option>
    <option value="a">a</option>
    <option value="a">a</option>
    </select>
    </form>
    <div id="txtHint" align='center'><font color="3333ff">Person info will be listed here.<font></div>
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    but i want data should be accessed through JSON, in between MYSQL & PHP file
    that doesn’t make sense. PHP and MySQL are already talking to each other, why making it more complicated than that?

    where you would be using JSON here is the AJAX Response. therfore you have some neat little functions like json_encode().

    Comment

    Working...