Solved

Mysqli_query() & Ajax

Posted on 2016-10-20
2
67 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 55

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do uses indexes to maximize MySQL Searches 14 55
Restricting the scope of variables to private in OOP 4 22
Help with PHP 13 27
Ajax and PHP 4 31
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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 …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

828 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