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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resource id number

Status
Not open for further replies.

gs99

Programmer
Oct 7, 2001
40
Local testing on Windows XP, PHP5, Apache

<?php
$link = mysql_connect("localhost", "root", "");
echo $link;
?>

This prints Resource id #2
What happened to Resource id #1?

Where can I find info about connection options, like putting username/pw into a separate file?

Thanks,
George


 
Resource ID #1 may have been used by some other internal operation of the engine. Resource IDs are used in lots of places in PHP, so I wouldn't lose any sleep over it. Particularly since outside of code debugging you'll never output its value.


You have to specify the userid and password in the invocation of mysql_connect(). That function cannot read the connection info from anywhere else.

You can, of course, use include() or require() to place the connection invocation outside the script. Or use something like parse_ini_file() or your own code to fetch the login credentials from some outside source and pass the values to mysql_connect(). PHP's filesystem functions are not constrained to operate within the current document root.

Why place the credentials in another file?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks,

It appears that "Resource id" is really not an accurate term? it refers to more than Connections?

When I first wrote the code I'm sure that it showed Resource id #1. I've restarted Apache, pc, etc. but it only shows #2 now. Comparing to an array, I thought I was missing the First item somehow.

If I add a connection with different value:
<?php
$link = mysql_connect("localhost", "root", "");
echo $link;
$link = mysql_connect("localhost", "test", "");
echo $link;
?>

It prints Resource id #2 Resource id #3

In this case, is Resource id #2 Replaced by the #3?
Can you only have one "connection" within a script/session?

Re: the login, I've read many warnings about "If you do something this way or that way it allows the reader to hack your system." The login code is perhaps the first thing the programmer does regarding security. (I'm referring to the mysql_connect() as the login. If this is wrong, please advise.)

Is there a simple recommended way to code the mysql_connect that is secure?
If you know any website that has examples, I'd appreciate it.

George
 
If you are storing the resource IDs in variables, you can have more than one open. You can also, however, invoke mysql_connect() without storing the return to a variable. In that case, I don't know whether you get a new connection or if the old one is overwritten.


Re: logins. Again, PHP's filesytem functions (fopen(), include(), require(), etc) are not constrained to that part of the server's filesystem used by the current web site. You could put the connection information outside of the web root altogether and use the data from within your scripts. This makes it harder, but not impossible, for hackers to find your login credentials.

There are other things you can do. Create another login and use that one instead of root. Make sure the new login is required to supply a password. Make sure the new login can only log into MySQL from certain IP addresses.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top