Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get mysql username and password interactively? 1

Status
Not open for further replies.

phn737

Technical User
Nov 4, 2002
31
US
Instead of hard coding the user and password into a php script with the command mysql_connect, is it possible to provide this when the php page is requested? If so, how can this be done?

<html>
<body>
<?php
$db = mysql_connect(&quot;localhost&quot;, &quot;user&quot;, &quot;mypassword&quot;);
...
?>
<body>
<html>

Thank you.
 
I use an include file that defines variables that are used in the mysql_connect function. This will solve the problem if you're wanting to be able to change the password quickly - you can just change the include file.

B
 
bgreenhouse,

Can you expand?

How to call the include file from within the PHP file?

Example...

Syntax of include file? A script that prompts for user ID and password? Can the user ID and password be prompted for inside of the PHP script?

Examples please...

TIA

JCF
 
Sure!

I actually thought that you wanted to not hard code the password into the .php file because it was changing often. If you define all the variables in one include and just include it when you're connecting to the database, all you need to do is change that one include.

If you want to enter it from a prompt you would have to either use a form or a prompt to get you to input it, then somehow process those into the connection string. I could figure it out, but I'm quite busy right now.

B
 
This sample script should do what you want.
Code:
<?php
if (isset($_POST['host']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['database'])) // If the form was sent
{
    $conn = mysql_connect($_POST['host'], $_POST['username'], $_POST['password']) or die(&quot;Couldn't connect: &quot; . mysql_error());
    mysql_select_db($_POST['database']) or die(&quot;Couldn't select database: &quot; . mysql_error());
    // Do whatever you want to do here
    mysql_close($conn);
}
else
{
?>
<form action=&quot;<?=$_SERVER['PHP_SELF']?>&quot; method=&quot;POST&quot;>
Username: <input type=&quot;text&quot; name=&quot;username&quot; /><br />
Password: <input type=&quot;password&quot; name=&quot;password&quot; /><br />
Host: <input type=&quot;text&quot; name=&quot;host&quot; /><br />
Database: <input type=&quot;text&quot; name=&quot;database&quot; /><br />
<input type=&quot;submit&quot; value=&quot;Do it&quot; />
</form>
<?php
}
?>
Note that the use of this script is stronly discouraged, as it sends the MySQL info in plaintext. //Daniel
 
Thanks Daniel,

That was a very good example, and a great &quot;disclaimer&quot; at the bottom... ;^))

Does anyone have an example of what the include file should look like?

Does anyone have an example of how the php file calls the include file...

Please forgive me... I have a lot of computer experience, and I know what I want to do...

But... I'm a newbie, when it comes to PHP, and high-level languages... (I'm a senior HP-UX Administrator / Systems Engineer, but I've never done much outside of alot of KSH, some basic HTML, some basic Perl, basic Awk / Sed, etc...)

Any help would be greatly appreciated... ( I work best with examples... )

TIA

JCF
 
database.inc.php
Code:
<?php
define(&quot;MYSQL_HOST&quot;, &quot;localhost&quot;);
define(&quot;MYSQL_USER&quot;, &quot;username&quot;);
define(&quot;MYSQL_PASS&quot;, &quot;password&quot;);
define(&quot;MYSQL_DB&quot;, &quot;database&quot;);
?>
connect.php
Code:
<?php
include('./database.inc.php');
$conn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die(&quot;Couldn't connect: &quot; . mysql_error() . &quot;<br />&quot;);
mysql_select_db(MYSQL_DB) or die(&quot;Couldn't select database: &quot; . mysql_error() . &quot;<br />&quot;);
// Do quering and other things
mysql_close($conn);
?>
//Daniel
 
Thanks Daniel,

That tells me &quot;exactly&quot; what I need to know, and how to implement it...

Thanks, and have a great weekend...

JCF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top