PHP - How To Get The Code Of A Function?
Hello,
I just wanted to know if it was possible to get the code of a function created inside a PHP file, for example : Code: [Select] <?php function test() { $var = 0; echo $var++; } ?> I want to get the code inside the function, means : Code: [Select] <?php $string = function_get_content("test"); /* $string = "$var = 0; echo $var++;" */ ?> Is that possible in any way? I believe that I'd have to analyze the page's code :s Thanks in advance.. Similar TutorialsHello All, I'm trying to put a lot of what I do into function calls now to make my code a bit tidier and consise. Take this simple if statement: if ($_GET['thedate'] =='') { $var = date('l, jS F, Y',time()); } else { $var = date('l, jS F, Y', strtotime($_GET['thedate'])); } How could I put this into a function and in some kind of switch statement? Thanks Checking if php function mysqli_fetch_assoc() been removed from newer php versions.
Running LAMP
Apache 2.4.7
PHP 5.5.9
MySQL 5.5.38
Running WAMPserver
Apache 2.4.9
PHP 5.5.12
MySQL 5.6.17
LAMP system, this code works fine:
<?php session_start(); $_SESSION['dbhost'] = $_POST['dbhost']; $_SESSION['dbuser'] = $_POST['dbuser']; $_SESSION['dbpass'] = $_POST['dbpass']; $_SESSION['dbname'] = $_POST['dbname']; ?> <!store variables in a session> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <?php if (!empty($_SESSION['dbname'])) { echo "Database connection settings are saved for ".$_SESSION['dbname'].".<br>"; $con = mysqli_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass'],$_SESSION['dbname']); if (mysqli_connect_errno()) { die("Failed to connect to your database with the saved settings.<br>"); } else { echo "Successfully connected to the database.<br><br>"; } $sql = "SELECT count(*) FROM account"; $query=mysqli_fetch_assoc(mysqli_query($con,$sql)); $count = $query["count(*)"]; if ($count == 0) { echo "Detected your account has not been configured yet: <br>"; echo 'Click <a href ="setup_account.html">here</a> to configure.<br><br>'; } else { $sql = "SELECT ircnick FROM account"; $query=mysqli_fetch_assoc(mysqli_query($con,$sql)); $user = $query["ircnick"]; echo "Found your previous saved settings: <br>"; echo 'If you want to change the settings for '.$user.' click <a href="setup_account.html">here</a>.<br>'; } } else { echo "Error saving your settings.<br>"; } ?>But on WAMP get this output: (!) Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\setup.php on line 44 Call Stack # Time Memory Function Location 1 0.0000 249936 {main}() ..\setup.php:0 2 0.0156 260184 mysqli_fetch_assoc() ..\setup.php:44 Trying to share my code with a friend who is using Windows. Edited by SmexyPantsGnome, 25 August 2014 - 09:12 PM. I've been making our thesis and i've been thinking if there is a function or method perhaps to get the interpreted codes(the codes you see if you get the source of the site on browsers) into another text file. something like fwrite the intertpreted code to a text file Hi, I have some code which works but when I created a function and call this same code it doesn't. The error I get is as follows: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/fhlinux010/l/languageschoolsuk.com/user/htdocs/admin/email.php on line 42 Error retrieving schools The code function CreateSchoolCheckboxes() { echo '<div style="height:400px;width:400px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">'; $querySchools = "SELECT * FROM school"; $result = mysql_query($querySchools, $conn) or die ("Error retrieving schools ".mysql_error()); while($row = mysql_fetch_array($result)) { $schoolname = $row['name']; echo '<input type="checkbox" name="school" value="'.$schoolname.'">'; echo $schoolname . '<br>'; } echo '</div>'; } Im sure that this is probably something simple but any suggestions would be much appreciated. Thanks, Joe Hello Everyone, I do not know if I am not doing something right or if I am completely wrong. I am trying to get an error or success message depending on the out come . This is the code that I have written so far Code: [Select] <?php if (!$_POST['submit']){ $msg = "you must enter a name and message"; header("Location: {$_SERVER['URL']}testmail.php?msg=$msg"); exit; } else { $name = $_POST['name']; $message = $_POST['message']; if (strlen($name) <= 20 && $message <= 300){ if (($name == "") || ($message == "")){ $msg = " please fill in all required feilds"; header("Location: {$_SERVER['URL']}testmail.php?msg=$msg"); exit; } else { ini_set("SMTP", "smtp.greatlakes.net"); $to = "mikedmartiny@gmail.com"; $subject = "Classified Ad Submission"; $headers = "From: autostylersrv@greatlakes.net"; $headers = "MIME-Version: 1.0rn"; $headers = "Content-type: text/html"; $headers = "charset=iso-8859-1rn"; $message = "<html><body>This is a email sent from $name<br /><br />$message</body></html>"; function mail($to, $subject, $message, $headers) { $msg = "mail has been sent"; } else { $msg = "there was a error"; } } } else { $msg = "You have exceded the max lentgh"; header("Location: {$_SERVER['URL']}testmail.php?msg=$msg"); exit; } } echo "$msg"; ?> $dir_name="./$masterdir"; $dir = opendir($dir_name); $basename = basename($dir_name); $fileArr = array(); while ($file_name = readdir($dir)){ if (preg_match("/\.jpg$/i", $file_name)) { #Get file modification date... # $fName = "$dir_name/$file_name"; $fTime = filemtime($fName); $fileArr[$file_name] = $fTime; } } # Use arsort to get most recent first # and asort to get oldest first arsort($fileArr); ////$numberOfFiles = sizeOf($fileArr); $numberOfFiles =7; for($t=0;$t<$numberOfFiles;$t++){ $thisFile = each($fileArr); $thisName = $thisFile[0]; $thisTime = $thisFile[1]; $thisTime = date("d M y", $thisTime); echo"$masterdir/$thisName"; } closedir ($dir); this returns the latest 7 files in a dir... works fine but each is now deprecated so gives warning How can i call javascript function from php code?? (Kindly Help) As an example (idea) Code: [Select] <script language="javascript"> function divclose() { alert("I am an alert box!"); } </script> <? echo "<SCRIPT LANGUAGE='javascript'>divclose();</SCRIPT>"; ?> Code: [Select] <?php $objConnect = mysql_connect("localhost","","cgdfgdfg") or die(mysql_error()); $objDB = mysql_select_db("ffdfvbbd"); $pic2 = "SELECT * FROM images"; if (!isset($_GET['Page'])) $_GET['Page']='0'; $pic1 = mysql_query($pic2); $Num_Rows = mysql_num_rows($pic1); $Per_Page = 16; // Per Page $Page = $_GET["Page"]; if(!$_GET["Page"]) {$Page=1;} $Prev_Page = $Page-1; $Next_Page = $Page+1; $Page_Start = (($Per_Page*$Page)-$Per_Page); if($Num_Rows<=$Per_Page) {$Num_Pages =1;} else if(($Num_Rows % $Per_Page)==0) {$Num_Pages =($Num_Rows/$Per_Page) ;} else {$Num_Pages =($Num_Rows/$Per_Page)+1; $Num_Pages = (int)$Num_Pages;} $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; $pic1 = mysql_query($pic2); $cell = 0; $link1 = "SELECT * FROM images"; echo ' <div id="tablediv"> <table border="0" cellpadding="17" cellspacing="0" class="table"> <tr>'; while($pic = mysql_fetch_array($pic1)) { if($cell % 4 == 0) { echo '</tr><tr>'; } if($cell == 2) { echo ' <td> filler </td>'; } elseif ($cell == 3) { echo ' <td> filler </td>'; } else { echo ' <td> <a href="/' . $pic["link"] . '.php"> <div class="image"> <img src="https://s3.amazonaws.com/images/' . $pic["pic"] . '.png" alt="' . $pic["alt"] . '" height="200" width="200" /> </div> </a> </td>'; } $cell++; } echo '</tr></table></div>'; ?>The code above works just fine. However, once I add a WHERE function,as shown below, I get a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" error. Code: [Select] <?php $objConnect = mysql_connect("localhost","","cgdfgdfg") or die(mysql_error()); $objDB = mysql_select_db("ffdfvbbd"); $pic2 = "SELECT * FROM images WHERE folder = 'blog' "; //WHERE FUNCTION IS HERE if (!isset($_GET['Page'])) $_GET['Page']='0'; $pic1 = mysql_query($pic2); $Num_Rows = mysql_num_rows($pic1); $Per_Page = 16; // Per Page $Page = $_GET["Page"]; if(!$_GET["Page"]) {$Page=1;} $Prev_Page = $Page-1; $Next_Page = $Page+1; $Page_Start = (($Per_Page*$Page)-$Per_Page); if($Num_Rows<=$Per_Page) {$Num_Pages =1;} else if(($Num_Rows % $Per_Page)==0) {$Num_Pages =($Num_Rows/$Per_Page) ;} else {$Num_Pages =($Num_Rows/$Per_Page)+1; $Num_Pages = (int)$Num_Pages;} $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; $pic1 = mysql_query($pic2); My mysql table includes column thumbnailID folder link pic alt time The folder column is there so I can specify what I want in the page. Anyhow, why won't it work? Hi there, can someone help to write a code for Copyright tag at the footer of the page. The format needs to be webpage start year - current year (eg © 2015-2020 ). I need to use function and parameters. The code should be as simple as possible as i am just a beginer! Thanks This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=307486.0 how can i use die but still continue with the html in the rest of code instead of stopping all codes from where die is initiated. i want to die (insert into database) but then still allow the html codes below the die function to work still. \= One more question. So I have a function with two parameters, $x and $y. Here it is. Code: [Select] function dydx($x,$y){ $equation = 2 * $x; return $equation; } Now here's the problem. I want $equation to be user defined. Easy enough, I use a post command and I get $equation to equal whatever the user inputs. The problem is that if I get input from the user, I'm not sure how to take that input and then have the function parameters work on it. Like if the user inputs "3*$x + 2*$y", i want to be able to let the parameters for the function dydx act on it. How could I go about doing this? Hello all, I have some piece of code that is nested like this $variable = 'This is a global argument'; function parentFunction($variable) { function childFunction() { echo 'Argument of the parent function is '.$GLOBALS['variable']; } childFunction(); } parentFunction(5); What I want to know is - Is there a way to access a variable from the parent function without passing arguments to the child function? (Something like how classes have parent::?). I don't want to use $GLOBALS because it might cause some variable collision, and I didn't want to pass arguments because incase I decide to change the arguments in the parent function I'd have to do it in the child function aswell. From my searching around in the Internet it seems like this is not possible, but if theres a slight chance that there might be something out there, i'm willing to give it a shot . Thanks in advance I need to call usort from a class function, and I'm puzzled about how to define the comparison function. I've tried to define the comparison function in the same class, but I can't get usort to call it. I found one hint that it will work if I make the comparison function static, but I tried that, and it didn't work for me. If I define the comparison function outside the class, it won't have access to object properties that it needs to operate. The only solution I can think of is to define the comparison function outside the class and put the object properties it needs in globals. Is there a cleaner way to do this? Question 1) Is the only and proper way to call a parent function "parent::function()"? Are there other/better ways from within a child function? Question 2) What are the deciding factors for when to make a function or attribute static? How do you make that decision? Assuming 5.3... Thanks. I have this function completely written in my class file that I am working on. The point to this function is to be able to check the login of a user or administrator for either of the control panels associated with my site. It will check the session intime as well as the page / module referenced. Once it passes all those checks, it will check and ensure the emailaddress/password stored in the current session still holds true and the account is still active... if the account is still active it will update the lastActivity as well as update all of the session variables with what is currently in the database. What I am looking for is basically a look at the function, see if it looks good.. If there is any part to it that could create security holes for the site just off the login function itself... Usage: $q->validUser($_SESSION['user'], $_mod); <?php function validUser($sess, $p) { if ($sess['inTime'] == '' && $p != 'login' && $p != 'logout') { session_destroy(); $login = '0'; $_int = ''; return $login; } else if ($sess['inTime'] < time()-3600 && $p != 'login') { $sess['inTime'] = ''; session_destroy(); $this->check_login($sess, $p); } else { $this->user = $sess['emailAddress']; $this->pass = $sess['password']; $login = $this->sql_query("SELECT * FROM users WHERE emailAddress = '".$this->user."' AND password = '".$this->pass."' AND status = '1' LIMIT '1'"); if ($login = $this->sql_numrows($login) < 1) { $sess['inTime'] == ''; session_destroy(); $login = '0'; } else { // logged in, lets update the database for last_activity AND the session. $this->sql_query("UDATE users SET lastActivity = '".now()."' WHERE emailAddress = '".$this->user."'"); $login = $this->sql_query("SELECT * FROM users WHERE emailAddress = '".$this->user."' AND password = '".$this->pass."' AND status = '1' LIMIT '1'"); $login = mysql_fetch_assoc($login); foreach ($login as $key => $value) { $sess[$key] = $value; } $sess['inTime'] = time(); $login = '1'; } return $login; } } ?> That is the main function, sql_query and sql_numrows is: <?php function sql_query($query = "", $transaction = FALSE) { unset($this->query_result); if ($query != "") { $this->num_queries++; if ($transation == BEGIN_TRANSACTION && !$this->in_transation) { $result = mysql_query("BEGIN", $this->db_connect_id); if (!$result) { return false; } $this->in_transaction = TRUE; } $this->query_result = mysql_query($query, $this->db_connect_id); } else { if ($transaction == END_TRANSACTION && $this->in_transaction ) { $result = mysql_query("COMMIT", $this->db_connect_id); } } if ($this->query_result) { unset($this->row[$this->query_result]); unset($this->rowset[$this->query_result]); if ($transaction == END_TRANSACTION && $this->in_transaction ) { $this->in_transaction = FALSE; if (!mysql_query("COMMIT", $this->db_connect_id)) { mysql_query("ROLLBACK", $this->db_connect_id); return false; } } return $this->query_result; } else { if ($this->in_transaction ) { mysql_query("ROLLBACK", $this->db_connect_id); $this->in_transaction = FALSE; } return false; } } function sql_numrows($query_id = 0) { if(!$query_id) { $query_id = $this->query_result; } return ($query_id) ? mysql_num_rows($query_id) : false; } ?> Any insight that can help to benefit these functions would be appreciated. When I put this chunk of code into it's own function: function fetch_all ($dbc, $query) { include ('knuffix_list_func.php'); pagination_start ($dbc, $query); $offset = $pag_array[0]; $rows_per_page = $pag_array[1]; $query = $query . " LIMIT $offset, $rows_per_page"; echo "test query: " . $query; knuffix_list ($query, $dbc); pagination_end ($pag_array); } And when I echo out the query as you can see in the example, then I notice that the variables $offset and $rows_per_page never get appended. I set the variable $pag_array to a global inside the function pagination_start(). It usually works when I DON'T wrap a function around this chunk of code, but if I do wrap a function around everything then the global suddenly won't work anymore. Btw, this also won't work if I wrap a function around the function DECLARATIONS. Any ideas, how I could make it work? I want to define a function instead of repeating query in all my php pages. I call a function by passing an $id value and from that function i have to get all the info related to that id, like name, description and uom.
I am trying to do this, but i dont know how to get these values seperately.
here is my function
function items($item_id) { $details = array(); $result = mysql_query("select item_id, name, uom, description from items where item_id=".$item_id."") or die (mysql_error()); while($row = mysql_fetch_array($result)) { $details[] = array((stripslashes($row['name'])), (stripslashes($row['uom'])), (stripslashes($row['description']))); } return $details; }and i call my function like this $info = items($id);Can somebody guide me in this im using a function which connects to a db called 'comments' and then inside that function i again called another function that will connect to the db 'main' to get avatars.... but as i put Code: [Select] mysql_select_db("main") or die(mysql_error()); on the new function, the original function stops to fetch rows. i tried to remove "mysql_select_db("main") or die(mysql_error());" and used the new function to return some text only and it worked, so i guess the connection to the db was the problem... i tried doing Code: [Select] mysql_query("SELECT * FROM main.avatar INNER JOIN main.list ON main.avatar.title=main.list.id WHERE main.avatar.page_id='$id'"); but it also didnt work |