Yanick is correct. Unless there's a script somewhere that let's a
javascript connect directly to port 3306 on your server (the mysql
port) there's no way to let the web browser talk directly to the mysql
server using javascript. And even if there were, you don't want to
let the world have access to your mysql server. A lot of problems
with that.
However, you can use jQueries Ajax methods to communicate to your
server and run php/perl/ruby/python/ or whatever your favorite server
side script is to act as an intermediary between the client browser
and the mysql server.
What you would do is something like this. And this is pseudocode, so
I can tell you right now it won't run, but it should be pretty close.
Go through the jquery docs on ajax to get the right syntax.
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript>
function loaddata()
{
$("#mydata").load("/myscripts/mydatascript.php");
}
</head>
<body>
<input type="button" name="mybutton" id="mybutton" onclick="loaddata()"></input>
<br>
<div id="mydata"></div>
</body>
</html>
Now here's the php page:
<?php
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("mydatabase", $connection);
$mydata = mysql_query("select * from mytable", $connection);
while ($myrow = mysql_fetch_array($mydata))
{
$name = $myrow["name"];
$email = $myrow["email"];
print "<p>Email $name at <a href=\"$email\">$email</a></p>\n";
}
?>
So what happens is that that when the html page is opened in a
browser, all you see is a button. When you click the button, the
loaddata function is called. The loaddata function uses jquery's ajax
load method and calls the php page. The php script on the server gets
data from the mysql database and prints it out. Jquery accepts that
printout and inserts it into the div with the id "mydata". You don't
have to use php, any server side application will work. And of course
you can get really fancy and pass data to the script with get and post
commands. You could do two buttons:
<input type="button" name="mybutton" id="mybutton"
onclick="loaddata(a)" value="Names starting with A"></input>
<input type="button" name="mybutton" id="mybutton"
onclick="loaddata(a)" value="Names starting with B"></input>
Then loaddata would become:
function loaddata(letter)
{
$("#mydata").load("/myscripts/mydatascript.php?startswith=" + letter);
}
the php would add this line and change the query like this:
$letter = $_GET["letter"];
"select * from names where name like '$letter%'"
Make sense?
On Sun, Nov 8, 2009 at 7:59 AM, Yanick <[email protected]> wrote:
> Hi,
> I believe you are confusing MySQL queries with JQuery;
>
> while MySQL is server side, JQuery is client side. The client should
> never be aware of what's going on at the server side less than it
> sends a request through a URL, and receives some response from it.
>
> For security's sake, a thumb rule is to let the client know as little
> as possible about how data is treated and even stored in your
> application.
>
> A rough diagram would show the layers as such (from what the user sees
> to how data is stored):
>
> [UI] <--> [JQuery] <--> [DOM] <--> [WebBrowser] <--> *Internet*
>
> *Internet* <--> [WebServer] <--> [Server scripts] <-->
> [ScriptingEngine] <--> [Database]
>
>
> I would suggest that you take a look at some server frameworks, mainly
> made in PHP (I recommend Zend Framework), for your server side
> manipulations. They usually have everything set up for you to start
> developping your web site/application.
>
>
>
> On Nov 7, 1:30 pm, Josh <[email protected]> wrote:
>> Hi everyone.
>>
>> This is my first post. I was hoping to see if anyone has done much
>> with jQuery to manipulate MySQL data. Here's the scenario: I am making
>> a leaderboard that has race competitors, their times, and position
>> (among other attributes). The data is being fed into the MySQL
>> database and I want to display the data on a leaderboard with live
>> updates and position changes. I figured jQuery would be a good way to
>> get to animation and effects I want. Has anyone done anything similar
>> to this or have any ideas which functions would be useful for this
>> project? Thanks.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "jQuery UI" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/jquery-ui?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"jQuery UI" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jquery-ui?hl=en.