Php code works fine until it's put inside a function -


i mystified. below code works fine if it's enclosed in tags , $data1, $data2 variables gotten via $post. i'd enclosed in function, , if do:

function ($input1, $input2) {  (then code here)  } 

it fails. variables? or (just guess) can't "require" within function?

anyway...here's code. feel simple , syntactic, i'm totally stumped. here's code:

//set connection or die returning error. require "connection.php";  echo 'connected';      // make query:      $query = "insert message (col1, col2) values ('$somedata1', '$somedata2')";     $result = @mysqli_query ($myconnection, $query); // run query.      if ($result) { // if ran ok. echo "success!";      } else { // if did not run ok.          echo "error";      } // end of if       mysqli_close($myconnection); // close database connection.  //free result set. mysqli_free_result($result);  //close connection. mysqli_close($myconnection);  echo "<span style='color:red;'>done.</span>"; 

editing add more code:

first, db connection follows:

<?php  define ('db_user', 'abcdef'); define ('db_password', '12345'); define ('db_host', 'localhost'); define ('db_name', 'mydb');  // make connection: echo "defined."; $myconnection = mysqli_connect (db_host, db_user, db_password, db_name)  or die ('could not connect mysql: ' . mysqli_connect_error() ); echo "connected.";  // set encoding...  mysqli_set_charset($myconnection, 'utf8');  ?> 

and not calling anywhere else in page had function. hope have needed connect , store tightly there inside function. maybe helps or makes more sense?

i had not gotten point calling function. including @ top other functions causing white, blank page appear.

you using variables $myconnection, $data1 , $data2 defined outside function. add line start of function , work. note have put every variable's name there used not defined in function.

global $myconnection, $data1, $data2; 

also, don't put require in function.

edit: many have suggested, i'll include other ways.

function argument:

function func($myconnection, $data1, $data2) {     // ...  }   // calling function  func($myconnection, $data1, $data2); 

$globals:

replace occurrences of $myconnection, $data1 , $data2 $globals['myconnection'], $globals['data1'] , $globals['data2'].


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -