Twister
Twister

Reputation: 69

Using mysql code in a javascript?

I'm stumped I've looked all over the internet and found no real way to access mysql within javascript, therefore I can't do what I need to.

Basically my javascript code is loading random websites using Iframes and I would like those list of sites to be pulled from a mysql database, but I see no way in doing this. I've tried using php's include function in javascript with no luck.

Any ideas?

More information:

Im using a script I have on my server called sitelist.js for the source of the script. All that script contains is the following:

targetURLs = [
'http://www.aol.com',
'http://www.yahoo.com',
'http://www.bing.com'
]; 

But i would like it to load websites from the mysql database instead of having those websites.

Upvotes: 2

Views: 172

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

Try something along these lines, with $sites being your array of urls from the database...

<script type="text/javascript">
    var sites = JSON.parse("<?php echo addslashes(json_encode($sites)); ?>");
    // now do stuff with sites.
</script>

Upvotes: 4

Chris Eberle
Chris Eberle

Reputation: 48775

This can't be done. Javascript (at the moment) doesn't have raw sockets. As a result, you can't call off to non-http network services like mysql. You could however wrap it using some horrible ajax API (SQL from javascript, no matter how it's accomplished, is a horrible idea).

Upvotes: 1

Related Questions