Page 1 of 1

[SOLVED]Odd problem

Posted: Tue Jul 20, 2004 2:15 pm
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... :(

Posted: Tue Jul 20, 2004 2:24 pm
by feyd
maybe $db isn't set right?

Posted: Tue Jul 20, 2004 2:53 pm
by evilmonkey
Check my db_conect() function...

when I do echo db_connect();, it returns a 1, everything should be fine... :?

Posted: Tue Jul 20, 2004 2:55 pm
by feyd
except $db in that function never leaves the function scope.

Posted: Tue Jul 20, 2004 2:58 pm
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.

Posted: Tue Jul 20, 2004 3:08 pm
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

Posted: Tue Jul 20, 2004 3:13 pm
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?

Posted: Tue Jul 20, 2004 3:16 pm
by evilmonkey
Yeah, return $db on success is what I need to do then...

Thanks nigma. :)

Posted: Tue Jul 20, 2004 3:17 pm
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...

Posted: Tue Jul 20, 2004 3:17 pm
by nigma
Yea no problem, let me know how things work out

Posted: Tue Jul 20, 2004 3:20 pm
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.

Posted: Tue Jul 20, 2004 3:20 pm
by evilmonkey
Everything works if I return $db; on success.