473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Use this Class?

133 100+
Hi,

I have a class that i am using and want to know how to echo a result from this.

This is a class
[PHP]

class database {


function selects($select) {
$this->result = mysql_query($select,$this->connection);
$this->row = mysql_fetch_assoc($select);
}

function getRes() {
while ($res = mysql_fetch_array($this->result)) {
$this->reser[] = $res;
}
return $this->reser;
}

}
[/PHP]

Here is the code

[PHP]$current = new database;
$current->selects("SELECT * FROM users WHERE id = '$_COOKIE[id]'");
$cresults = $current->getRes();
// assign your db results to the template
$smarty->assign('cresults', $cresults);
[/PHP]

I though i would be able to do something like:

print $cresults['username'];

But this does not work.

Any Ideas?

Cheers,
Adam
Feb 1 '08 #1
4 1348
Amzul
130 100+
Hi,

I have a class that i am using and want to know how to echo a result from this.

This is a class
[PHP]

class database {


function selects($select) {
$this->result = mysql_query($select,$this->connection);
$this->row = mysql_fetch_assoc($select);
}

function getRes() {
while ($res = mysql_fetch_array($this->result)) {
$this->reser[] = $res;
}
return $this->reser;
}

}
[/PHP]

Here is the code

[PHP]$current = new database;
$current->selects("SELECT * FROM users WHERE id = '$_COOKIE[id]'");
$cresults = $current->getRes();
// assign your db results to the template
$smarty->assign('cresults', $cresults);
[/PHP]

I though i would be able to do something like:

print $cresults['username'];

But this does not work.

Any Ideas?

Cheers,
Adam
try to put echo in your function

Expand|Select|Wrap|Line Numbers
  1. function getRes() {
  2.                 $i=-1;
  3.     while ($res = mysql_fetch_array($this->result)) {
  4.                                 $i++;
  5.         $this->reser[] = $res;
  6.                                echo $this->reser[$i];
  7.     }
  8.     return $this->reser;
  9. }
not tested but i think it will work
Feb 1 '08 #2
adamjblakey
133 100+
I have just added this but does not seem to work.

Any ideas?
Feb 4 '08 #3
adamjblakey
133 100+
Anyone else have something i can try for this?
Feb 5 '08 #4
nathj
938 Expert 512MB
Anyone else have something i can try for this?
Hi,

I have a data class also and the function that executes the query returns an associative array:
[php]
function queryGetData($plSetCount, $pcQuery)
{
// ensure there is a connection
$this->ensureConnection();
// run the supplied query
$result = mysql_query($pcQuery, $this->database_link) or die("Error: ". mysql_error());
$laReturnArray = array();
$i=0;
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
if ($row)
{
$laReturnArray[$i++]=$row;
}
}
if($plSetCount)
{
$this->nRecentCount = mysql_num_rows($result);
}
mysql_free_result($result);
return $laReturnArray;
}
[/php]
Don't worry about the ensure connection stuff the important part for you is that it takes a select statement as the second parameter (the first para is a logical that indicates if you want a count on the object set), executes this select and transforms the results into an associative array. This array is returned to the calling code enabling the following:
[php]
// assume dataobject exists
$lcQuery = "select * from table" ;
$laResults = $loDatObject->queryGetData(false, $lcQuery) ;

if($laResults)
{
foreach($laResults as $laRow)
{
echo $laRow['fieldname'];
}
// alternatlively you could replace the foreach with print_r($laResults) to get the array printed to screen.
}
else
{
// no results
echo '<p> no results returned</p>';
}
[/php]

I do this quite a lot and I got the basis for my existing code from here

I hope this helps
nathj
Feb 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.