blakesmoore Posted January 24, 2012 Share Posted January 24, 2012 Hi, I'm trying to create a form which will test database connection but I'm getting an error. I'm new to php so any help would be appreciated. form.php <html> <form action="dbconnection.php" method="post"> <table width="400" border="0" cellspacing="2" cellpadding="0"> <tr> <td width="29%" class="bodytext">Host Name</td> <td width="71%"><input name="server" type="text" id="name" size="32"></td> </tr> <tr> <td class="bodytext">Username</td> <td><input name="username" type="text" id="email" size="32"></td> </tr> <tr> <td class="bodytext">Password</td> <td><input name="password" type="text" id="email" size="32"></td> </tr> <tr> <td class="bodytext"> </td> <td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td> </tr> </table> </form> dbconnection.php <?php $server = $_POST['server']; $username = $_POST['username']; $password = $_POST['password']; $link = mysql_connect ('$server', '$username', '$password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> When submitting the form you get the below error Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '$server' (1) in /website/public_html/dbconnection.php on line 6 Could not connect: Unknown MySQL server host '$server' (1) If you connect successfully if should echo 'Connected Successfully' If you cannot connect if should say 'Could not connect' any advice would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/ Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Variables aren't interpolated within single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310791 Share on other sites More sharing options...
blakesmoore Posted January 24, 2012 Author Share Posted January 24, 2012 Thanks for the help. If i enter invalid credentials i still get the warning error, do you know how i could just make this say connection failed? Also would you know how to add the time it takes to connect to the database? Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310795 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 You could suppress the error returned from mysql_connect() by preceding it with a @. To time it, you can store the value of microtime in a variable before the connection call, and then find and echo the difference between it and microtime() after the connection call. Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310799 Share on other sites More sharing options...
blakesmoore Posted January 24, 2012 Author Share Posted January 24, 2012 Thanks again, have added the @ before the mysql_connect The connection error message is shorter, but would it be possible to literally just make it say 'Could not connect?' I don't really understand the timing bit, would it be possible to explain in simpler terms? Sorry about this but i'm only just learning Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310802 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Which part of the code do you think would produce an error message from MySQL? Just remove that part . . . I linked to the microtime() function manual entry above, so that will have everything you need to know about microtime() itself. Just assign it to a variable before connecting, then find the difference between the ending microtime(), and the stored starting microtime() by subtracting, then echo the result. $start = microtime(TRUE); $link = mysql_connect('blah', 'blah', 'blah'); $end = microtime(TRUE); $elapsed = $end - $start; echo "Elapsed time: $elapsed seconds. Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310813 Share on other sites More sharing options...
blakesmoore Posted January 24, 2012 Author Share Posted January 24, 2012 Thank you so much Pikachu Quote Link to comment https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.