Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mysqli_query() & Ajax

Posted on 2016-10-20
2
Medium Priority
?
106 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 111

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 60

Accepted Solution

by:
Julian Hansen earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

972 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