TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-06-2008, 01:47 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default help with oop

what i'm trying to do with my class is instead of writeing

while($row = mysql_fetch_assoc($query)) {

echo $row['field'];

}

i'm trying to write my own function that will do this but i dont know or am kind of confuses to how i can output more fields(from sql) and if i did write a function how will i end the while loop with a paranthesis

here is my class

Code:
<?php

class dbconnect {

public $host;
public $username;
public $password;
public $database;
public $table;
public $query;


#===================================
#    Database Connection
#===================================
function connectdb($in_host,$in_username,$in_password,$in_database) {

$conn = mysql_connect($this->host = $in_host,$this->username = $in_username,$this->password=$in_password) or die(mysql_error());
mysql_select_db($this->database=$in_database)or die(mysql_error());

}

#=========================================
#    sql query
#=========================================

function query($in_query) {

$output = mysql_query($this->query=$in_query)or die(mysql_error());
return $output;



}


#=========================================
#    Alternate to mysql_fetch_assoc
#=========================================


function fetch($this->query($in_query),$field)) {

while($row = mysql_fetch_assoc($this->query($in_query)) {


echo $row['$field'];

}


}
if you have a similar class can i take a look at it?
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-06-2008, 03:39 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Ok, one or two things wrong with the class.

PHP Code:
$output mysql_query($this->query=$in_query)or die(mysql_error());
return 
$output
Pointless variable, just uses up memory albeit not alot but it all adds up:
PHP Code:
return mysql_query($this->query=$in_query)or die(mysql_error()); 
Would produce the same effect.

Also:
PHP Code:
function fetch($this->query($in_query),$field)) {

while(
$row mysql_fetch_assoc($this->query($in_query)) {


echo 
$row['$field'];

You cant do that within function parameters and also if it did work, it would be useless as you call the same function within the body of the function.

Also each time your while loop loops, you are calling a query and getting a new resource, which will loop infinitely over the top entry in the table.
This will accomplish the same thing without looping infinitely
PHP Code:
    function fetch($in_query$field) {
        
$res $this->query($in_query);
        while(
$row mysql_fetch_assoc($res)) {
            echo 
$row[$field] ;
        }
    } 
As to your actual question, do you mean something like this:
PHP Code:
    function fetch($in_query) {
        
$res $this->query($in_query);
        
$retArr = array();
        while(
$row mysql_fetch_assoc($res)) {
            
$retArr[] = $row;
        }
    return 
$retArr;
    } 
That fetches all the rows from the result set.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design