Jump to content

A Question about Functions


jkewlo

Recommended Posts

Hello,

 

I am wondering if I have 2 functions.

one of them is to read a file called License.txt

and reads a 32 randomly created key by a java application

function get_Data() {
$License = "license.txt";
$File = fopen($License, 'r');
$Read = fread($File, 35);
fclose($File);
echo $Read;

}

 

and the other to get some data from a database

function get_Name(){
$sql="SELECT * FROM sec_code WHERE code=". $Read ."";
$result=mysql_query($sql) or die(mysql_error());
	echo $result['Name'];
}

 

What I am wondering is it possible to use data from another function, like in my WHERE clause.

I am trying to get the user data from the key that is in the License file. but they cannot be the same in the same function.

 

Thanks

~John Grissom

:rtfm:

Link to comment
https://forums.phpfreaks.com/topic/207666-a-question-about-functions/
Share on other sites

Instead of having your get_Data() function echo the data have it return it like so:

 

function get_Data() {
$License = "license.txt";
$File = fopen($License, 'r');
$Read = fread($File, 35);
fclose($File);
return $Read;
}

 

You can then use the return value in context like so:

 

function get_Name(){
$sql="SELECT * FROM sec_code WHERE code=". get_Data() ."";
$result=mysql_query($sql) or die(mysql_error());
	echo $result['Name'];
}

If your get_Data() function returned the value, you can call that function anywhere you need the value -

function get_Data() {
   $License = "license.txt";
   $File = fopen($License, 'r');
   $Read = fread($File, 35);
   fclose($File);
   return $Read;
}

 

However, if you anticipate needing the value more than once in a script, you should store that returned value in a program variable so that you don't waste time reading it from the file each time -

 

$key = get_Data();

Right I had done that in a previous try and didnt get no luck.

 

the keys are stored in the database as well as the License.txt file for security.

 

but when I try what AlexWD said  I get this error

 

Unknown column 'NJp3dzODeAFJIePJDleBnLdaX699gYB4' in 'where clause'

 

and I have seen this error earlier, as I tried what he had said.

The key is a string, so you need to place quotes around it in the query. Because you haven't it was thinking that you were comparing it against another column.

 

$sql="SELECT * FROM sec_code WHERE code=". get_Data() ."";

 

should be:

$sql="SELECT * FROM sec_code WHERE code='". get_Data() ."'";

Ok, I normally do the '". ."' around my code. idk why I didnt. I guess when you stare at it all day long you kinda of forget to do certain things.

 

So I am not get any more error's but it is not displaying any data.

 

in the Name field I have John Grissom in it

To get the data from the MySQL result returned by mysql_query you need to use a function like mysql_fetch_assoc, like so:

 

function get_Name(){
$sql="SELECT * FROM sec_code WHERE code=". get_Data() ."";
$result=mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['Name'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.