-2

Does anyone have any advice on how to run mysql queries using JQuery?

I am attempting to implement a system using jquery mobile with a mysql database as the backend. Are there any useful tutorials that I could follow to learn how to use ajax to call php functions that would contact a mysql database?

  • Look up some documentation on AJAX. This subject has been answered a bazillion times before on and off SO. – Cerbrus Feb 24 '14 at 15:34
  • A google search returns page after page with tutorials and guides. [First one I found](http://www.sanwebe.com/2012/04/ajax-add-delete-sql-records-jquery-php) – aludvigsen Feb 24 '14 at 15:35
  • 1
    @lxndr: w3fools... don't link to them. their code is at best bad, and overall badly written and dangerous. – Marc B Feb 24 '14 at 15:37
  • "Google it" responses bug me. Usually when I'm in this position, I'm asking because I don't know what to google. At least recommend a specific search. In this case, I googled "jquery mysql" and got a few promising hits. Here's a few: http://stackoverflow.com/questions/8598659/how-can-i-use-jquery-to-run-mysql-queries, http://openenergymonitor.org/emon/node/107 – GridDragon Feb 24 '14 at 15:46
  • 1
    @gridDragon: The problem is that often, the first Google result when Googleing the title of the question is a relevant resource already. Programmers need to learn how to research instead of asking for the solution. – Cerbrus Feb 24 '14 at 15:49
  • @Cerbrus: I tend to agree, if the first page of results already includes a question on SO. (Which, in this case, it does.) If it doesn't, though -- and if the question asks about something useful -- it may well be a legitimate question, even if there are a thousand useful results elsewhere already. The stated goal of this site is to be a source of answers for programming-related questions, to the point where a half-decent Google search would send you to SO. – cHao Feb 24 '14 at 16:03

2 Answers2

0

You won't run MySQL queries with JQuery directly. Set up a PHP file on your server that reads the needed information from $_REQUEST, $_POST, or $_GET (depends on how you want to implement it), then inserts that information into your query and handles the database call.

Once the database responds, you can send the information back to your jQuery app with

echo php_jsonencode($data); //Data is what ever value you got from the database.

That will send your database data back to jQuery as JSON so it's easier to parse.

The details on how you accomplish these steps are very dependent on the information your sending, and what you need to send back. So I can't answer more specifically without more details.

GridDragon
  • 2,857
  • 2
  • 33
  • 41
0

If you want to call PHP functions from jQuery (JavaScript) itself, it is impossible as JavaScript is client side and PHP is server side. Still there are ways to communicate with your database using JavaScript. The most common way is using AJAX where you call a PHP file or another server side script with some code to communicate.

$("form").submit(function(event) {
  event.preventDefault();

  $.ajax({
    type:     "POST",
    url:      $(this).attr("action"),
    data:     $(this).find("input[name='needed']").val()
  });

  return false;
});
Sietse
  • 623
  • 1
  • 8
  • 23