whiteboikyle Posted June 3, 2008 Share Posted June 3, 2008 okay i have my data process.php page here <?php require("include/constants.php"); class Process { function Process(){ if(isset($_POST['register'])) { $this->register(); } else { header("Location: main.php"); } } function register(){ $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $query = "INSERT INTO members (ID, username, password, email) VALUES (NULL, '$username', '$password', '$email')"; mysql_query($query, connection); echo("<center><font size='4'><font color='red'>Post has been submitted! Will redirect in 2 seconds</center></font>"); echo("<META HTTP-EQUIV='refresh' CONTENT='2;news.php'>"); } }; $process = new Process; ?> and here is my include/constant.php page <?php define("DB_SERVER", "BLOCKED"); define("DB_USER", "BLOCKED"); define("DB_PASS", "BLOCKED"); define("DB_NAME", "BLOCKED"); $connection = @mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die("Error: Couldn't connect to the Database"); mysql_select_db(DB_NAME, $connection); php?> Now how would i make $connection constant throughout the whole process.php page because it gives an error Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\Register&Login\process.php on line 19 Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/ Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 mysql_query($query, connection); You forgot the $ sign. Also, you can do this: Change: function Process(){ To: function Process($conn){ Then add: $this->connection = $conn; And make $connection a variable in the class, and refer to it as $this->connection in the function. Also, what version of PHP are you using? It'd be better to not use Process, and actually make a constructor with __construct. Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556884 Share on other sites More sharing options...
whiteboikyle Posted June 3, 2008 Author Share Posted June 3, 2008 xD the missing $.. but it still has the same error. now about the function process($connetion){ where would i actually input the data for $this->connection? Like... hmmm kinda confused.. Fairly new to Class/Function.. and i am using PHP 5.2.5 Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556914 Share on other sites More sharing options...
revraz Posted June 3, 2008 Share Posted June 3, 2008 Is there only 1 connection for the page? If so, just omit it. Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556917 Share on other sites More sharing options...
whiteboikyle Posted June 3, 2008 Author Share Posted June 3, 2008 i want 1 connection clause that will work throughout the whole scripts.. Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556934 Share on other sites More sharing options...
whiteboikyle Posted June 3, 2008 Author Share Posted June 3, 2008 i went ahead and made a database.php <?php include("constants.php"); class MySQLDB { var $connection; //The MySQL database connection /* Class constructor */ function MySQLDB(){ /* Make connection to database */ $this->connection = @mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); @mysql_select_db(DB_NAME, $this->connection) or die(mysql_error()); } /** * query - Performs the given query on the database and * returns the result, which may be false, true or a * resource identifier. */ function query($query){ return mysql_query($query, $this->connection); } }; $database = new MySQLDB; ?> So how would i use this for my process.php i included the database.php and i used query("INSERT INTO members (ID, username, password, email) VALUES (NULL, '$username', '$password', '$email')"); but then it gave an error Fatal error: Call to undefined function query() in C:\wamp\www\Register&Login\process.php on line 21 which is that function query(); above Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556940 Share on other sites More sharing options...
wildteen88 Posted June 3, 2008 Share Posted June 3, 2008 You'll have to use $database->query() to call the query method in your MySQLDB class. In order to call the query method inside another classes method you can use MySQLDB::query() Alternatively you could just create a constructor in your Process class which connects to mysql automatically. Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556942 Share on other sites More sharing options...
whiteboikyle Posted June 3, 2008 Author Share Posted June 3, 2008 I have never dealt with a constructor before.. New word for me xD i will look into it Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556946 Share on other sites More sharing options...
wildteen88 Posted June 3, 2008 Share Posted June 3, 2008 I have never dealt with a constructor before.. New word for me xD i will look into it Really! You've already set one up in your MySQLDB class! Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556952 Share on other sites More sharing options...
whiteboikyle Posted June 3, 2008 Author Share Posted June 3, 2008 i am not making this code from scratch xD i am using my friends code as well.. Just making this for learning purposes.. so could you help me out? EDIT: nevermind i figured it out global $database; $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $database->query("INSERT INTO members (ID, username, password, email) VALUES (NULL, '$username', '$password', '$email')"); echo("<center><font size='4'><font color='red'>Post has been submitted! Will redirect in 2 seconds</center></font>"); echo("<META HTTP-EQUIV='refresh' CONTENT='2;news.php'>"); Is that a constructor? Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-556956 Share on other sites More sharing options...
wildteen88 Posted June 4, 2008 Share Posted June 4, 2008 Is the code you posted above placed in a function which has the same name as your class? eg: class class_name { // NOTE: the following is for PHP4 ONLY // a constructor is function which has the same name as the class function class_name() { // add any code here that you want to run whenever the class is initiated } // NOTE: If you are using PHP5 then use the following instead: function __construct() { // add any code here that you want to run whenever the class is initiated } } I would recommend you to read the manual to get yourself familiar with the OOP syntax. PHP5 OOP (Advanced) PHP4 OOP (Basic) Quote Link to comment https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/#findComment-557664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.