Jump to content

Test MySQL Connection


iEthan

Recommended Posts

I'm attempting to make a script that takes form data (which happens to be MySQL host, username and password information) and tests the connection. I'm failing miserably. It needs to be a form that would, once [the button was] clicked, would test the connection and return the results, without changing pages. I think you could do the last part with Ajax, but I don't know much of it.Help?

Link to comment
Share on other sites

Write a simple PHP file (I'll call it testConnection.php) to test the connection which outputs some text indicating success/failure. Make it take the necessary parameters from GET (because you're not modifying the database at all). Here's a script to request that file via GET and display the result in a div. (With a little extra PHP effort, this can work without JavaScript.) See the AJAX tutorial for how it works.

<html>    <head>        <script type="text/javascript">            function getAjax(){                var ajax = null;                if (window.XMLHttpRequest){// code for Firefox, Opera, IE7, etc.                    ajax = new XMLHttpRequest();                }else if(window.ActiveXObject){                    // Internet Explorer                    try{                        ajax = new ActiveXObject("Msxml2.XMLHTTP");                    }catch(e){                        try{                            ajax = new ActiveXObject("Microsoft.XMLHTTP");                        }catch(e){}                    }                }                return ajax;            }                        function stateChanged(){                if (this.readyState == 4){// 4 = "loaded"                    if (this.status == 200){// 200 = "OK"                        document.getElementById('result').innerHTML = this.responseText;                    }else{                        alert('Problem retrieving data:' + this.statusText);                    }                }            }            window.onload = function(){                document.getElementById('testConnection').onsubmit = function(){                    var ajax = getAjax();                    if (ajax != null){                        ajax.onreadystatechange = stateChanged;                        var query = [];                        for(var i = 0; i < this.elements.length; i++){                            if(this.elements[i].name){                                query.push(this.elements[i].name + '=' + this.elements[i].value);                            }                        }                        ajax.open(this.method, this.action + '?' + query.join('&'), true);                        ajax.send(null);                    }else{                        alert("Your browser does not support AJAX.");                    }                    return false;                };            };        </script>    </head>    <body>        <div id="result"></div>        <form action="testConnection.php" method="GET" id="testConnection">            <input type="text" name="host">            <input type="text" name="name">            <input type="password" name="pass">            <input type="submit" value="Test">        </form>    </body></html>

Link to comment
Share on other sites

Note that I've edited the HTML.Honestly, I was hoping you had an idea of how to check the connection. My code always validates it, and I think the PHP manual is incorrect about what mysql_connect returns on failure.

<?php$conn = mysql_connect($_GET['host'], $_GET['user'], $_GET['pass']) or die(mysql_error());if($conn){	echo 'The connection is valid.';}else{	echo 'The connection is invalid.';}?>

So I await a PHP database expert.

Link to comment
Share on other sites

Aha... I was experimenting with passing the query string via send(), but PHP wasn't getting any input - and MySQL apparently calls the connection valid if any one of the arguments is empty. I've updated the JS accordingly. New problem: I'm always told that www-data is an invalid user, even when ($_GET['user'] == 'root').

array(3) { ["host"]=> string(9) "localhost" ["name"]=> string(4) "root" ["pass"]=> string(10) "abc123" }Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /home/chris/www/projects/testConnection/testConnection.php on line 3Access denied for user 'www-data'@'localhost' (using password: YES)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...