Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/108591-how-to-make-a-include-global/
Share on other sites

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.

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

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

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.

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?

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)

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.