473,809 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New To PHP - Error In My Class

Using PHP 5.1.2

I'm working on a personal project to get used to PHP, and I seem to be
stuck at the moment on a class I'm writing. Here is the code for the
class, with a comment highlighting where it dies when a function is
called externally from the code that instantiates the class:

----------BEGIN PASTE----------
class DataAccess{

private $databaseConnec tion;

function __construct()
{
@ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
if (mysqli_connect _errno())
{
// TODO: generate an error
}
}

function __destruct()
{
$databaseConnec tion->close();
}

function GetArticlesForC ategory($cid)
{
$querySelect = "SELECT stuff WHERE something = " . $cid;

// Testing shows that the following line is where the code dies.
$resultSet = $databaseConnec tion->query($querySe lect);
// End dying code

if ($resultSet)
{
$rowCount = $resultSet->num_rows;
if ($rowCount 0)
{
$i = 0;
while ($i < $rowCount)
{
$rows[$i] = $resultSet->fetch_assoc( );
$i++;
}
$resultSet->free();
return $rows;
}
else
{
// TODO: report an empty set
$resultSet->free();
return;
}
}
else
{
// TODO: generate an error
$resultSet->free();
return;
}
}

}
------------END PASTE------------

The error log seems to be claiming that $databaseConnec tion is
undefined on that line of code. Is there maybe something syntactic
that I'm missing? I've tried replacing all 3 references to
$databaseConnec tion in the class functions with
$this->$databaseConne ction, but that didn't seem to change anything.

Any ideas?

Aug 11 '06 #1
3 1268

cyber0ne wrote:
Using PHP 5.1.2

I'm working on a personal project to get used to PHP, and I seem to be
stuck at the moment on a class I'm writing. Here is the code for the
class, with a comment highlighting where it dies when a function is
called externally from the code that instantiates the class:

----------BEGIN PASTE----------
class DataAccess{

private $databaseConnec tion;

function __construct()
{
@ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
if (mysqli_connect _errno())
{
// TODO: generate an error
}
}

function __destruct()
{
$databaseConnec tion->close();
}

function GetArticlesForC ategory($cid)
{
$querySelect = "SELECT stuff WHERE something = " . $cid;

// Testing shows that the following line is where the code dies.
$resultSet = $databaseConnec tion->query($querySe lect);
// End dying code

if ($resultSet)
{
$rowCount = $resultSet->num_rows;
if ($rowCount 0)
{
$i = 0;
while ($i < $rowCount)
{
$rows[$i] = $resultSet->fetch_assoc( );
$i++;
}
$resultSet->free();
return $rows;
}
else
{
// TODO: report an empty set
$resultSet->free();
return;
}
}
else
{
// TODO: generate an error
$resultSet->free();
return;
}
}

}
------------END PASTE------------

The error log seems to be claiming that $databaseConnec tion is
undefined on that line of code. Is there maybe something syntactic
that I'm missing? I've tried replacing all 3 references to
$databaseConnec tion in the class functions with
$this->$databaseConne ction, but that didn't seem to change anything.

Any ideas?
You have to use $this->... to access class properties

And...
@ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
if (mysqli_connect _errno())
{
// TODO: generate an error
}
}
How can you tell if an error occurs if the mysqli error message's are
being displayed and php errors are suppressed for 'new mysqli'?

Replace every $databaseConnec tion with $this->databaseConnec tion,
replace TODO with echo mysql_connect_e rror() and remove the @ from
$databaseConnec tion.

Tim

Aug 11 '06 #2
cyber0ne wrote:
Using PHP 5.1.2

I'm working on a personal project to get used to PHP, and I seem to be
stuck at the moment on a class I'm writing. Here is the code for the
class, with a comment highlighting where it dies when a function is
called externally from the code that instantiates the class:

----------BEGIN PASTE----------
class DataAccess{

private $databaseConnec tion;

function __construct()
{
@ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
if (mysqli_connect _errno())
{
// TODO: generate an error
}
}

function __destruct()
{
$databaseConnec tion->close();
}

function GetArticlesForC ategory($cid)
{
$querySelect = "SELECT stuff WHERE something = " . $cid;

// Testing shows that the following line is where the code dies.
$resultSet = $databaseConnec tion->query($querySe lect);
// End dying code

if ($resultSet)
{
$rowCount = $resultSet->num_rows;
if ($rowCount 0)
{
$i = 0;
while ($i < $rowCount)
{
$rows[$i] = $resultSet->fetch_assoc( );
$i++;
}
$resultSet->free();
return $rows;
}
else
{
// TODO: report an empty set
$resultSet->free();
return;
}
}
else
{
// TODO: generate an error
$resultSet->free();
return;
}
}

}
------------END PASTE------------

The error log seems to be claiming that $databaseConnec tion is
undefined on that line of code. Is there maybe something syntactic
that I'm missing? I've tried replacing all 3 references to
$databaseConnec tion in the class functions with
$this->$databaseConne ction, but that didn't seem to change anything.

Any ideas?
the way you did it in constructor the variable databaseConnect ion has
scope only in that particular method, you should use

@ $this->databaseConnec tion = new mysqli("this", "is", "fake", "data");

and allways reference that property via $this->databaseConnec ion...

you were using local variables where you really needed class properties
--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination
Aug 11 '06 #3
Replace every $databaseConnec tion with $this->databaseConnec tion

I guess it was a syntax thing :) I was trying the following:

$databaseConnec tion
or
$this->$databaseConne ction

But I just got rid of that second $ where applicable and it seems to be
working now. Thanks!

Aug 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4383
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
6
1803
by: WindAndWaves | last post by:
Hi Gurus The page below has a strange error. It seems to be working very well, just when you enter 8 or 9 for day, month or year then you get an error. I really have no idea where that is coming from. Can you help? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD>
7
2714
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError> ErrorCodeOffset (Long Int) ErrorObjBaseName (Text(50)) ErrorDescription (Text(255)) 1 DontDoThat Hey! Don't do that!
2
2024
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
4
6413
by: Sanjay Kumar | last post by:
Folks ! I am working with VC++ after a long time and having problem linking latest xerces 2.7 in VC++ 2005 Express Edition. I have done following: 1. downloaded and unpacked the the library: http://www.apache.org/dist/xml/xerces-c/binaries/xerces-c_2_7_0-windows_2000-msvc_60.zip
1
3295
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly, but throws a load of linker errors
2
2760
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
6
121923
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could not help me. Does any body know what is the problem?. Thanks. OtherFunctions.obj : error LNK2001: unresolved external symbol "int
1
2821
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I incorporate the same logic within my simulation, the class that implements the functor logic has problems compiling. I get the following errors: -- Building myTest.cpp --
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.