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
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'];
}

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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() ."'";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.