jkewlo Posted July 13, 2010 Share Posted July 13, 2010 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 Link to comment https://forums.phpfreaks.com/topic/207666-a-question-about-functions/ Share on other sites More sharing options...
Alex Posted July 14, 2010 Share Posted July 14, 2010 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 https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085602 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 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 https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085603 Share on other sites More sharing options...
jkewlo Posted July 14, 2010 Author Share Posted July 14, 2010 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 https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085607 Share on other sites More sharing options...
Alex Posted July 14, 2010 Share Posted July 14, 2010 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 https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085612 Share on other sites More sharing options...
jkewlo Posted July 14, 2010 Author Share Posted July 14, 2010 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 https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085615 Share on other sites More sharing options...
Alex Posted July 14, 2010 Share Posted July 14, 2010 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']; } Link to comment https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085619 Share on other sites More sharing options...
jkewlo Posted July 14, 2010 Author Share Posted July 14, 2010 ugh. lol. I think I need to pack it up for the day.. Thank you guys.. ++ for all of you. Link to comment https://forums.phpfreaks.com/topic/207666-a-question-about-functions/#findComment-1085620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.