Page 1 of 1

Function Help

Posted: Sun Nov 22, 2009 1:06 am
by spec36
I (newb) am having trouble trying to understand the following return statement;

Code: Select all

 
function addNewUser($username, $password){
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password')";
   return mysql_query($q,$conn);
 
I get this so far

1.

Code: Select all

function addNewUser($username, $password){
This creates a function called addNewUser and has two parameters passed to the function, $username and $password.

2.

Code: Select all

global $conn;
This is a global variable that is used to connect to a Mysql database.

3.

Code: Select all

$q = "INSERT INTO users VALUES ('$username', '$password')";
The variable q is equal to the mysql command to insert $username and $password into the users table of the database.

4.

Code: Select all

return mysql_query($q,$conn);
Can someone explain how this line is working? I know it is adding the variables to the table, but I do not fully understand it. Like, what does "mysql_query($q,$conn)" mean in english?

Any help would be greatly appreciated.

Spec36

Re: Function Help

Posted: Sun Nov 22, 2009 4:26 am
by papa
You can read about it here: http://us3.php.net/manual/en/function.mysql-query.php

In your case, you could do like this:

Code: Select all

 
if(addNewUser("papa", "mypass)) echo "User added.";
else echo "Could not add user.";
 

Though it's not recommended to use global vars like that.