[SOLVED]Odd problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED]Odd problem

Post by evilmonkey »

Okay, this takes me right back to square one with php, no understanding what the heck is wrong with the MySQL querty I just wrote. (this is so 2 years ago...) :oops: Anyway, here is the code...

Code: Select all

db_connect(); //connect to database...
		//damn, this is one long query....
		$registeration_query = "INSERT INTO `users` (`username`, `pass`, `email`, `age`, 
		`religion`, `smoke`, `drink`, `sex_pref`, `status`, `occupation`,`website`, `phys_height`,
		`phys_body_type`, `phys_eye_colour`, `phys_hair_colour`, `from_country`,
		`gender`, `about_self`, `user_status`) VALUES ('".mysql_escape_string($_POST['username'])
		."', '".mysql_escape_string(md5($_POST['pass']))."', '".mysql_escape_string($_POST['email'])
		."', '".$_POST['age']."', '".$_POST['religion']."', '".$_POST['smoke']."', '".$_POST['drink']
		."', '".$_POST['pref']."', '".$_POST['status']."', '".mysql_escape_string($_POST['occupation'])
		."', '".mysql_escape_string($_POST['website'])."', '".mysql_escape_string($_POST['height'])
		."', '".mysql_escape_string($_POST['bodytype'])."', '".mysql_escape_string($_POST['eye_colour'])
		."', '".mysql_escape_string($_POST['hair_colour'])."', '".mysql_escape_string($_POST['from_country'])
		."', '".$_POST['gender']."', '".mysql_escape_string($_POST['about_self'])."', 'normal')";
		//whew
		echo $registeration_query;
		$result = mysql_query($registeration_query, $db) or die(mysql_error());
		echo "Success!";
The db_connect() function:

Code: Select all

function db_connect() {
	$host="localhost";
	$username="";
	$password="";
	$database = "my_db";
	if ($db = mysql_connect($host, $username, $password)){
		mysql_select_db($database, $db);
		return true; }
	else {
		return mysql_error(); }
}
The problem is, it displays the query (echo $registeration_query;), but does not display "Success" (echo "Success"). The really hilarious thing is that I took the oputput of echo $registeration_query;, and punced it into phpmyadmin as a custiom query...and it bloody worked...So I really don't know what the heck is wrong...

Please help... :(
Last edited by evilmonkey on Tue Jul 20, 2004 3:19 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe $db isn't set right?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Check my db_conect() function...

when I do echo db_connect();, it returns a 1, everything should be fine... :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

except $db in that function never leaves the function scope.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Bah, sniped again! What ~feyd said. You need to return $db from your function. Maybe have it return $db on working, false on not working, and just have it dump the error.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

************!!!!!!!! I'm an idiot.....happens.

However, that raises another problem. $db = db_connect() won't help the situation. How can I make use of db_connect();?

Would return $db; on successful connection do the trick?

Thanks everyone! :D
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Why won't that help the situation? If you tell your function db_connect() to return the variable $db on success then the statement:

Code: Select all

$db = db_connect();
Should allow you to use $db as your mysql link identifier later on when you query the database.

If you don't want to change what your function returns you could just declare $db globally and then alter it's value once inside the function db_connect().

Do you kinda understand what i'm saying?
Last edited by nigma on Tue Jul 20, 2004 3:16 pm, edited 1 time in total.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Yeah, return $db on success is what I need to do then...

Thanks nigma. :)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

i dont think you have to return $db... as long as the server has made a connection, it will use the open conncection. so if mysql_connect is called within the scope of a private function, it shouldt matter. same with mysql_select_db. i could be wrong...
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Yea no problem, let me know how things work out
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Liljester: if you don't provide a mysql link identifier when querying a database with mysql_query() than mysql_query() will just use the last opened connection.
Last edited by nigma on Tue Jul 20, 2004 3:20 pm, edited 1 time in total.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Everything works if I return $db; on success.
Post Reply