For my new project, I want this modern approach not to require a page reload for each database request. :) I want the script to query the database and create a table with the information about the request.
I tried different scripts that I found on the Internet. The one below was closest to my needs.
index.php
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Display Page</title> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script> </head> <body> <button type='button' name='getdata' id='getdata'>Get Data.</button> <div id='result_table'> </div> <script type='text/javascript' language='javascript'> $('#getdata').click(function(){ $.ajax({ url: 'getdata.php', type:'POST', dataType: 'json', success: function(output_string){ $('#result_table').append(output_string); } </script> </body> </html>
getdata.php
<?php include('conn.inc.php'); //Query of facebook database $facebook = mysql_query('SELECT * FROM users') or die(mysql_error()); //Output results if(!$facebook) { mysql_close(); echo json_encode('There was an error running the query: ' . mysql_error()); } elseif(!mysql_num_rows($facebook)) { mysql_close(); echo json_encode('No results returned'); } else { $output_string = ''; $output_string .= '<table border="1">'; while($row = mysql_fetch_assoc($facebook)) { $output_string .= '<tr>'; foreach($row as $value) { $output_string .= '<td>{$value}</td>'; } $output_string .= '</tr>'; } $output_string .= '</table>'; } mysql_close(); // This echo for jquery echo json_encode($output_string); ?>
But I get a table with a relationship {$ value} inside the table. I tried only $ value but got a bunch of zeros.
I tried a simple script
$query = "SELECT users_name, users_password FROM users WHERE users_name = 'name'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); echo $row['users_name'];
And then I get som results, but with this script I need to refresh the page with every search. To be clear, I want to be able to create a table with information from the mysql database and display it on the screen with a page reload.
Any ideas?
source share