To be a function or not to be a function.

Ask about a PHP problem here.
Post Reply
wizzuriz
Posts: 53
Joined: Mon Jul 25, 2011 4:22 pm

To be a function or not to be a function.

Post by wizzuriz »

Hello All.

I just checked out some nice code on http://www.php.net/manual/en/function.m ... -assoc.php

This is the code, now my problem is I like to make this in to a function. I did change the code some to make variables for me so I can each them out on a other site. The problem is, when I make it into a function I lose the variables outside of the function.

I need to make this in to a function because I need to gather much more information from a total of 5 databases. ( I would like to use this function 5 times to create variables I can use later on a other page.

while ($row = mysql_fetch_assoc($result)) {
extract($row, EXTR_PREFIX_SAME, "info");
}

echo "This is my new variable from the database:" . " " . $example ."<br/>";
<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

mysql_free_result($result);

?>
If you know how this can work please let me know.

I also like to say that I know its not a easy one and don´t expect you all to know how to fix this problem, however if there is one that know how please let show me a example on how the function would look like.

best regards
Wizzuriz...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: To be a function or not to be a function.

Post by jacek »

The function should fetch the information and return it, it should not create any variables for use in the rest of the script.

so you should call it like
$result = function_name('something');
Image
wizzuriz
Posts: 53
Joined: Mon Jul 25, 2011 4:22 pm

Re: To be a function or not to be a function.

Post by wizzuriz »

Hello Jacek.

Thanks.

Can you please let me know if there should be any change to this function.
1.	<?php
2.	 Function get_information($email){
3.	$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
4.	 
5.	if (!$conn) {
6.	    echo "Unable to connect to DB: " . mysql_error();
7.	    exit;
8.	}
9.	  
10.	if (!mysql_select_db("mydbname")) {
11.	    echo "Unable to select mydbname: " . mysql_error();
12.	    exit;
13.	}
14.	 
15.	$sql = "SELECT id as userid, fullname, userstatus 
16.	       FROM   sometable
17.	       WHERE  `user_email` = {`$email`}";
18.	 
19.	$result = mysql_query($sql);
20.	 
21.	if (!$result) {
22.	    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
23.	    exit;
24.	}
25.	 
26.	if (mysql_num_rows($result) == 0) {
27.	    echo "No rows found, nothing to print so am exiting";
28.	    exit;
29.	}
30.	 
31.	// While a row of data exists, put that row in $row as an associative array
32.	// Note: If you're expecting just one row, no need to use a loop
33.	// Note: If you put extract($row); inside the following loop, you'll
34.	//       then create $userid, $fullname, and $userstatus
35.	$row = mysql_fetch_assoc($result) 
36.	Return $result;
37.	mysql_free_result($result);
38.	} 
39.	// function ends here 
40.	$result = get_information('user_emil');

I insert $email to the function to give the value for WHERE in the query.

Please let me know if if think I should change someting

Best regards
wizzuriz
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: To be a function or not to be a function.

Post by jacek »

Well mysql_free_result($result); will never get called because it is after the return line, so you can just remove that since it would happen automatically at the end of the function anyway.

The function should not really be connecting to the database, assuming you will use this more than once on each page, you don't want more than one connection open.

Other than that it looks good :D

You may want to look into SQL injection at some point though.
Image
wizzuriz
Posts: 53
Joined: Mon Jul 25, 2011 4:22 pm

Re: To be a function or not to be a function.

Post by wizzuriz »

Hi Jacek.

I did review your videos on htmlenteties & other safety stuff however, right now I just need to code to work before I pump it up with more secure code :)

Now I got all the functions to work and I believe I understand some of the basic for functions. Now I have a other question, you see, when I take information from a function like this §variable = function_name(§email); and then echo §variable['user_name'];

I can´t pass the into the next function where I want to look up a value where lets say user_age is prime key.

example: my_new_function(§§variable['user_name']){
}

And I can't make that in to a new variable like §new_variable = §variable['user_name'];

This leaves me to believe that I just use INNER JOIN and here is the question. do you have a video on how to select some value where user_name = §user and then use the value in the INNER JOIN to get a other value, because I can´t look it up on user_name beacuse that is not in the other table, only user_age it.
So I want to look up age in one table and use that to lookup a new value in a other table where age is prime key.

Best regards
Wizzuriz
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: To be a function or not to be a function.

Post by jacek »

Well
my_new_function(§§variable['user_name']){
should just be
my_new_function(§variable['user_name']){
;)

But yes, you should really be using a JOIN if you want to get information from other tables rather than a second query.
Image
wizzuriz
Posts: 53
Joined: Mon Jul 25, 2011 4:22 pm

Re: To be a function or not to be a function.

Post by wizzuriz »

the 2 $ is a MT, but my question is how should I make the join query?

Any idea, if so please share :)

best regards
wizzuriz
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: To be a function or not to be a function.

Post by jacek »

wizzuriz wrote:but my question is how should I make the join query?
There is some information on it here http://www.w3schools.com/sql/sql_join.asp

Without a specific example I can't really help more than that :s
Image
Post Reply