Solved

Mysqli_query() & Ajax

Posted on 2016-10-20
2
61 Views
Last Modified: 2016-10-24
Hi,

If I'm using an Ajax command to run a Mysqli_query in another PHP file - can I return the results of the Mysqli_query into the page I'm working in?

J
0
Comment
Question by:Ridgejp
2 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41852253
Yes!  It's done all the time and it's quite easy.

Here is the most basic and simplified example I can find for a jQuery "Hello World" script.
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html

Here is an article about MySQL, and while it's mostly about how to get off the obsolete MySQL extension, it gives examples of how to run a query and retrieve a results set.  You can send the results to the client browser by using techniques shown in the jQuery Hello World.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

When you get started building the server-side script that responds to the AJAX request, you probably want to use GET-method requests.  If you do that, you can test your scripts from the browser, using URL parameters and seeing the browser output (use "view source").  This makes testing and debugging very easy.  Once the server-side script works correctly and gets the right information from the database, you can add the client-side script that makes the AJAX request and adds the returned data to the browser display.
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41852427
Think of it like this
There are two scripts
PHP script
HTML script
This PHP script reads the database and outputs values to the browser. You should be very familiar with this.
If you create a script that accepts a URL parameter ($_GET) - uses that to access a database and output the results to the screen you can test that page by entering the url and the parameter
Example
http://www.yourserver.tld/greet.php?id=1
Your script does something like
greet.php
<?php
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id)  {
  $mysqli = new mysqli('localhost','username','password','database');
  // Error checking here
  $query = "SELECT * FROM user WHERE id={$id}";	
  $result = $mysqli->query($query);
  if ($result) {
        $row = $result->fetch_object();
        if ($row) {
            echo "Hello, {$row->FirstName}";
        }
  } 
}

Open in new window

You can test it here
http://www.marcorpsa.com/ee/t1747.php?id=1
http://www.marcorpsa.com/ee/t1747.php?id=2
http://www.marcorpsa.com/ee/t1747.php?id=3
You can run that in the browser and see the results - test it all works the way you want by changing the id in the URL.

When it works you do this
<html>
<body>
<input type="radio" value="1" name="user" /> User 1
<input type="radio" value="2" name="user" /> User 2
<input type="radio" value="3" name="user" /> User 3
<input type="radio" value="4" name="user" /> User 4
<div id="greet"></div>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $(':radio').click(function() {
    $('#greet').load('greet.php?id=' + $(this).val());
  });
});
</script>
</body>
</html>

Open in new window


You can see it working here

EDIT: Use F12 to view the console while testing the above and take a look at what is being sent to and received from the server
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question