Solved

Mysqli_query() & Ajax

Posted on 2016-10-20
2
41 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 108

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now