Jump to content

[SOLVED] PHP 5 Classes


rizla_za

Recommended Posts

Hi guys/girls

 

I'm a newbie in php 5 and classes so I hope someone would be able to help me with this 1. I'm busy learning php 5 and classes and have written a simple class to help me and if someone can explain to me why the one class works and the other not I will be very grateful.

 

Working Code:

<?php

class OpenRead

{

var $name;

function OpenDB($localhost,$username,$password,$database)

{

$mysqli = new mysqli($localhost, $username, $password, $database);

$query = "select * from test where id='1'";

$result = $mysqli->query($query);

$row = $result->fetch_assoc();

$this->name = $row["name"]; 

}

}

?>

 

<?php

    $a = new OpenRead();

    $a->OpenDB('localhost','user_psi','pass_psi_3375','psimation_inv');

 

    echo $a->name;

?>

 

This code is working fine and display the name in my db, but when I use the code witch follow it's not working.

 

<?php

class OpenRead

{

var $name;

function OpenDB($localhost,$username,$password,$database)

{

$mysqli = new mysqli($localhost, $username, $password, $database); 

}

  function GetName()

  { 

  $query = "select * from test where id='1'";

  $result = $mysqli->query($query);

  $row = $result->fetch_assoc();

  $this->name = $row["name"]; 

  }

}

?>

 

<?php

    $a = new OpenRead();

    $a->OpenDB('localhost','user_psi','pass_psi_3375','psimation_inv');

    $a->GetName();

    echo $a->name;

?>

 

Like I said I'm a noob to php 5 and this might be a very stupid question...

Link to comment
Share on other sites

You need to assign $mysqli to a class variable.

 

var $_mysqli;

 

and assign $mysqli to that when you connect...

 

$mysql = new mysqli();

$this->mysqli = $mysql;

 

then in your function use $this->mysqli->query(); etc etc;

 

I believe in the other functions you can also just do

 

global $mysqli;

 

 

You should learn the newer and PHP5 way of classes using Private, Protection public etc etc.

 

<?php
class OpenRead
{
var $name;
private $_mysqli;

function OpenDB($localhost,$username,$password,$database) 
{
$mysqli = new mysqli($localhost, $username, $password, $database); 
$this->mysqli = $mysqli;
}
  function GetName()
  {  
  $query = "select * from test where id='1'"; 
  $result = $this->mysqli->query($query);
  $row = $result->fetch_assoc();
  $this->name = $row["name"];  
  }
}
?>

Link to comment
Share on other sites

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.