Solved

Mysqli_query() & Ajax

Posted on 2016-10-20
2
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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 57

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

734 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