Function Help

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
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Function Help

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Function Help

Post 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.
Post Reply