SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do Javascript and MySQL mix?

    Hello All,
    I am going to be developing a dynamic dropdown menu for my site, using Javascript (which I know very little about).
    Now I need to populate the menu's with items from a MySQL DB.
    I was wondering how well Javascript and MySQL mix together, if at all.
    Does anybody have any handy links that might give me some more info on this?
    Thanks,

  2. #2
    SitePoint Guru quenting's Avatar
    Join Date
    Dec 2002
    Location
    Switzerland
    Posts
    735
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GirlsOnBikes
    Hello All,
    I am going to be developing a dynamic dropdown menu for my site, using Javascript (which I know very little about).
    Now I need to populate the menu's with items from a MySQL DB.
    I was wondering how well Javascript and MySQL mix together, if at all.
    Does anybody have any handy links that might give me some more info on this?
    Thanks,
    javascript is client side while mysql is backend data. javascript runs in the browser and database on the server, so they basically don't have anything to do with each other.

    You need to use some server-side scripting language (likely php) to interface and get data from the database and then dinamically generate your js tree before sending it to the client.

    Quentin

  3. #3
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have actually already written my script, I forgot to edit/delete this post.
    It was quite amazing to see how php could work together with JS. It definitely took some counter-intuitive thinking to pull it off, but I managed to learn a ton from working through it.
    Thanks for the reply though. Sorry I didn't edit/delete it earlier.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by quenting
    javascript is client side while mysql is backend data. javascript runs in the browser and database on the server, so they basically don't have anything to do with each other.

    You need to use some server-side scripting language (likely php) to interface and get data from the database and then dinamically generate your js tree before sending it to the client.

    Quentin
    This is incorrect, you can use javascript to interface with mysql, although I have never tried this with mysql... I have used msaccess to do this. Basically it means creating a system dsn, to make this possible.


    ps couldn't see mysql on the drivers list



    but i'm sure theres some way to interface with it via this method.

  5. #5
    SitePoint Guru quenting's Avatar
    Join Date
    Dec 2002
    Location
    Switzerland
    Posts
    735
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Andrew-J2000
    This is incorrect, you can use javascript to interface with mysql, although I have never tried this with mysql... I have used msaccess to do this. Basically it means creating a system dsn, to make this possible.
    ps couldn't see mysql on the drivers list
    http://andrewj.no-ip.com/sitepoint/images/sys_dsn.png
    but i'm sure theres some way to interface with it via this method.
    i disagree, javascript is run in the client's browser, there's no way it can access directly a remote database located on the server. Java however, through the use of java server pages or servlets, can access a database using jdbc, but this is an "equivalent" to php, and has nothing to do with javascript.
    javascript code can't even access extensively the machine it is run on (for obvious security reasons), i don't really imagine how it could connect to a remote database.

    Quentin

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I was regarding this as being run on the local computer not remotely, anyway i know this stuff is kind of outdated, but what about netscapes server side additions such as Connection.execute(); / database.Connect();

    also what happened to active x or using hta to run commands on the filesystem?

  7. #7
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I'm not sure about particular database API's but Javascript can access server-side databases although this is currently only available in IE5+.

  8. #8
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by quenting
    javascript code can't even access extensively the machine it is run on (for obvious security reasons), i don't really imagine how it could connect to a remote database.

    Quentin
    Well, as long as you accept an active x or running it from a hta application javascript/ jsript can pretty much do anything... heres a quick sample for you.

    place in a text file and rename to test.hta

    Code:
    <HTA:APPLICATION>
    <html>
     <head>
      <title>ping</title>
      <script type="text/javascript">
    	var WSHShell = new ActiveXObject("WScript.Shell");
    	var fs = new ActiveXObject("Scripting.FileSystemObject");
    	function runcmd(cmd, args)
    	{
    	  document.all['ret'].innerHTML = 'please wait executing command';
    	  var forReading = 1, forWriting = 2, forAppending = 8;
    	  var objShell = new ActiveXObject('WScript.Shell');
    	  var objFSO = new ActiveXObject('Scripting.FileSystemObject');
    	  var sCommand = 'cmd /c ' + cmd + ' ' + args;
    	  var sCommand = sCommand + ' > C:\\temp2.txt';
    	  objShell.run (sCommand, 0, true);
    	  f = objFSO.GetFile('C:\\temp2.txt');
    	  objTempFile = f.OpenAsTextStream( forReading, 0 );
    	  var ret = '';
    	  document.all['ret'].innerHTML = 'retreiving response from command';
    	  while( !objTempFile.AtEndOfStream )
    	  {
    		var sReadLine = objTempFile.ReadLine();
    		ret+= sReadLine + '\n<br />';
    	  }
    	  return ret;
    	} 
      </script>
      </head>
      <body>
    	command <input type="text" name="cmd" value="" />
    	args <input type="text" name="args" value="" />
    	<input type="button" value="fetch" onclick="document.all['ret'].innerHTML = runcmd(document.all['cmd'].value, document.all['args'].value);" />
    	<pre id="ret">
    	</pre>
      </body>
    </html>
    By using active x and the windows host control (wscript) you can do a lot of things.

    Anyway I'm just doing an example of connecting to a database via javascript...

    I lost my last post pc crashed

  9. #9
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I did not have Javascript directly access the DB's, I had to do a php page with echo's that would output any javacode I needed to execute in the browser. Here is a little snippet. This file is called test.php, and it runs the javascript:

    PHP Code:
    for ($j=0$j $make_count$j++) {
      
    $model_query "SELECT model FROM ".$table_array[$j]." ORDER BY model";
      
    $model_result mysql_query($model_query,$db);
      for (
    $r=0$row mysql_fetch_row($model_result); $r++) {
       
    $model_array[$r] = $row[$r];
      }
      
    $model_count count($model_array);
      for (
    $k=0$k $model_count$k++) {
       echo 
    "group[$j][$k]=new Option(\"$model_array[$k]\",\"[url=http://www.girls-on-bikes.com/search/search.php?make=$table_array[$j]&model=$model_array[$k]\]http://www.girls-on-bikes.com/search/search.php?make=$table_array[$j]&model=$model_array[$k]\")\n[/url]";
      }
     } 

  10. #10
    SitePoint Wizard silver trophy TheOriginalH's Avatar
    Join Date
    Aug 2000
    Location
    Thailand
    Posts
    4,810
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't having Javascript accessing a local database through active-x be a little pointless? You may as well crack open access.

    I'm really quite confused by this thread

    You'd only be able to access the db using ie on the actual pc holding the database right? hmmm....
    ~The Artist Latterly Known as Crazy Hamster~
    922ee590a26bd62eb9b33cf2877a00df
    Currently delving into Django, GIT & CentOS

  11. #11
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheOriginalH
    Wouldn't having Javascript accessing a local database through active-x be a little pointless? You may as well crack open access.

    I'm really quite confused by this thread

    You'd only be able to access the db using ie on the actual pc holding the database right? hmmm....
    Well no not particulary, quite often server admins use javascript / vbscript to automate tasks... Have a look at some of the microsoft examples with asp especially, you will find a few for adding subdomains etc to iis or what not.. These are normally bundled with a batch file which normally executes a variety of js/vbs files to configure / setup the enviroment.

    ps heres a quickie...

    if you want to connect to mysql, you will need to add a system dsn, however there seems to a problem with me adding mysql as a dsn, but what I found out was its something to do with the install on mysql.

    Code:
      function query(dsn, sql)
      {
    	try
    	{
    	  objdb = WScript.CreateObject('ADODB.Connection');
    	  objdb.Open(cnnx);
    	  objCmd = WScript.CreateObject('ADODB.Command');
    	  objCmd.ActiveConnection = objdb;
    	  objCmd.Commandtext = sql;
    	  rs = objCmd.Execute;
    	  while (!rs.EOF)
    	  {
    		for (i=0; i<rs.Fields.count;i++)
    		{
    		  WScript.echo( rs(i) );
    		}
    		rs.MoveNext();
    	  }
    	}
    	catch(e)
    	{
    	  WScript.echo ('Error Number: ' + (e.number &0xFFFF) + '\nError Description: ' + e.description);
    	  WScript.Quit(1);
    	}
      }
      dsn = 'DSN=GlobalCar'; //DSN=database  (preferably use a system dsn)
      sql = 'select * from tbl';
      query(dsn, sql);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •