PDA

View Full Version : [PHP/mySQL] Question



Trigs
11-03-2009, 09:40 PM
When selecting a database using mysql_select_db(), can you use the function more than once and still retain the previous database selection? For example, can I do:

mysql_select_db('abc');
mysql_select_db('def');

Would that select both databases or would it select abc then select def?

Protege
11-03-2009, 09:43 PM
I believe you would need two instances of a mysql connection active in order to connect to two databases simultaneously.

Fehm
11-03-2009, 09:53 PM
Ya know what i'd do, have 2 config files and have

include ('config.php');
include ('config2.php');

Lol
Dunn of it would work though

Agnostic Bear
11-03-2009, 09:54 PM
When selecting a database using mysql_select_db(), can you use the function more than once and still retain the previous database selection? For example, can I do:

mysql_select_db('abc');
mysql_select_db('def');

Would that select both databases or would it select abc then select def?

abc then def, if you wanted only 1 mysql connection, you could do it with a bit of overhead, you have 2 functions, i.e "mysql_select_2" "mysql_select_1" and you put this in each:



mysql_query('USE `databasename`');
then run the query.

Trigs
11-03-2009, 10:07 PM
So wouldn't I just be better off doing something like:

mysql_query('USE db1 SELECT * blah blah blah');
and
mysql_query('USE db2 SELECT * blah blah blah');

Agnostic Bear
11-03-2009, 10:18 PM
So wouldn't I just be better off doing something like:

mysql_query('USE db1 SELECT * blah blah blah');
and
mysql_query('USE db2 SELECT * blah blah blah');

No, however you can simply omit the mysql_select_db and do this:

SELECT * FROM `databasename`.`tablename`

Trigs
12-03-2009, 03:12 AM
Thats much better. Thanks.

Trigs
12-03-2009, 04:45 AM
Also, 2 more questions.

1. I created a function. Heres the code:


function clean($str) {
if(!get_magic_quotes_gpc()){
$str = addslashes($str);
}
$str = strip_tags(htmlspecialchars($str));
return $str;
}

Thats in config.php. If I include index.php and call that function I get an error saying that I can't redeclare it. I've never gotten that error before and all the other functions I created work.

And lets say I have these files

-MainFolder
--config.php
-SecondaryFolder
--index.php

How should I include config.php into index.php? Should I spell out the whole url?

Trinity
12-03-2009, 09:10 AM
And lets say I have these files

-MainFolder
--config.php
-SecondaryFolder
--index.php

How should I include config.php into index.php? Should I spell out the whole url?



include '../config.php';

Trigs
12-03-2009, 08:03 PM
tried that already. didnt work. maybe theres something in php.ini or apache's settings?

Excellent2
12-03-2009, 09:37 PM
tried that already. didnt work. maybe theres something in php.ini or apache's settings?You tried just 1 dot?

Trigs
12-03-2009, 09:41 PM
You posted 2 dots?

Excellent2
12-03-2009, 09:42 PM
You posted 2 dots?That was Trinity :P

Trigs
12-03-2009, 09:46 PM
Oh lol. So is it 1 or 2 dots?

Excellent2
12-03-2009, 09:59 PM
Oh lol. So is it 1 or 2 dots?Try include './config.php'; to see if it works :)

GeekSRV
12-03-2009, 10:48 PM
Maybe Try:



<?php
define('DOCUMENT_ROOT', '/path/to/directory');
include(DOCUMENT_ROOT."/config.php");
?>


Remember to edit path to directory :)

Trigs
13-03-2009, 03:09 AM
BTW, I'm actually using require();

and thanks guys, ill see

Blinger1
13-03-2009, 04:28 AM
Can't you just use include

GeekSRV
13-03-2009, 07:17 AM
BTW, I'm actually using require();

and thanks guys, ill see

Just change include to require - and if using require use require_once() - Unless you require it more than once :)

Agnostic Bear
13-03-2009, 09:00 AM
You are all very silly and should learn to read:



require_once '../MainFolderName/config.php';

Sameer!
13-03-2009, 02:34 PM
Lol Jewish Bear got you there! He's the one that posted the correcdt code for it.

Trigs
14-03-2009, 01:07 AM
Jewish Bear's code worked. Thanks once again.

Is there anyways to resend header info? I need a way to set cookie info if a user has checked a remember me box but I can't because I can't find a way to do it without getting the headers already sent error.

Trinity
14-03-2009, 04:26 AM
Jewish Bear's code worked. Thanks once again.

Is there anyways to resend header info? I need a way to set cookie info if a user has checked a remember me box but I can't because I can't find a way to do it without getting the headers already sent error.

Make sure there's no output before you attempt to send headers.

Dentafrice
14-03-2009, 02:26 PM
Jewish Bear's code worked. Thanks once again.

Is there anyways to resend header info? I need a way to set cookie info if a user has checked a remember me box but I can't because I can't find a way to do it without getting the headers already sent error.
Headers don't work like that.
Post your entire code, that gives you the headers already sent error.

Trigs
14-03-2009, 04:43 PM
It's pretty much something like:

<?php

session_start();

if($_POST['rememberme']) {
setcookie(blah blah blah)
}

?>

Dentafrice
14-03-2009, 05:08 PM
Well there has to be output before that, or it wouldn't give you that error.

Trigs
14-03-2009, 05:10 PM
Yeah there is.

Dentafrice
14-03-2009, 05:27 PM
Well that's what I want to see.. you can't have that outputting before you set it..

Trigs
14-03-2009, 05:41 PM
Well then how would I set the cookie? How do other sites use a remember me feature?

Dentafrice
14-03-2009, 05:45 PM
Well then how would I set the cookie? How do other sites use a remember me feature?
They don't output before they set it maybe?

Trigs
14-03-2009, 05:47 PM
Nevermind. It's pretty unsafe anyways.

Dentafrice
14-03-2009, 05:48 PM
No it's not.. not if you do it correctly.

What makes you think it is "unsafe"?

Trigs
15-03-2009, 12:46 AM
I've never used cookies before. If I do it, it will be unsafe because I don't know how to make it secure. I was planning on storing the username and password in the cookie because that's the only way I knew how to do it.

Protege
15-03-2009, 12:48 AM
I've never used cookies before. If I do it, it will be unsafe because I don't know how to make it secure. I was planning on storing the username and password in the cookie because that's the only way I knew how to do it.
*slaps*

Want to hide these adverts? Register an account for free!