PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 1st, 2005, 03:07 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 7
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Angry Newbie to classes

I'm not new to PHP, but i have never had reason to use classes before. I am starting a personal project and I decided a class file would be a usefull because at the very least I would get some expeience using them.

Almost everything that I have tried so far has worked until now. I have to query a mysql db on a regular basis. So I decided instead of having to write out eh whole thing everytime I would put it into a class. Now it creates the object however I am trying to put the object through a while loop with mysql_fetch_array() nothign happens.

Please remember this is just some test code. If what I want can be done the final code may look a little (a lot) different
PHP Code:
//quicktest.php
//echo phpinfo();
include 'testclass.php';
$myclass=new classA
$DB_QUERY_select='SELECT *'
$DB_QUERY_from=' FROM test_table';
$DB_QUERY=$DB_QUERY_select.$DB_QUERY_from;
$table='test_table';
$query_line='Query originated on line 7 in quicktest.php';
echo 
'Before the Loop<br />';
$result=$myclass->dbSelectQuery($DB_QUERY,$table,$query_line);
echo 
$result."<br />";
while(
$result2=mysql_fetch_array($result)) {
echo 
'Loop Started<br />'
echo 
$query."<br />";
echo 
"Row ".$result[0]."<br />Column 2 ".$result[1]."<br />Column 3 ".$result[2]."<br />Column 4 ".$result[3]."<br />Column 5 ".$result[4]."<br />Column 6 ".$result[5]."<br />Column 7 ".$result[6]."<br />Column 8 ".$result[7]."<br />Column 9 ".$result[8]."<br />Column 10 ".$result[9]."<br /><br /> "


PHP Code:
//testclass.php
require 'function_library/crew473_DB_Connection.php';
class 
classA{
var 
$DB_QUERY;
var 
$table;
var 
$query_line;
function 
dbSelectQuery($DB_QUERY,$table,$query_line){
$query=mysql_query($DB_QUERY)or die("Could not query table \"".$table."\"<p>The database returned the following error(s):</p>".mysql_error().$query_line);
echo 
"inside the class".$query."<br />";
return 
$query;
}

__________________
CHornJr
"One day I'll know what I am doing"
My Blog
Suanhacky Lodge #49
Rebel Squadrons

Reply With Quote
  #2  
Old February 1st, 2005, 08:45 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
On your first block of code it says:
while($result2=mysql_fetch_array($result)) {

But your echo statement relates to $result...
Quote:
echo "Row ".$result[0]."<br />Column 2 ".$result[1]."<br />Column 3 ".$result[2]."<br />Column 4 ".$result[3]."<br />Column 5 ".$result[4]."<br />Column 6 ".$result[5]."<br />Column 7 ".$result[6]."<br />Column 8 ".$result[7]."<br />Column 9 ".$result[8]."<br />Column 10 ".$result[9]."<br /><br /> ";

Reply With Quote
  #3  
Old February 1st, 2005, 04:28 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 7
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Well that was an error. But that wasn't what was causing me my problem. I forogt to insert any information into the table so it couldn't run through the loop. That is fixed now.

Now my next problem with classes...

I want to run the same code but as an object in the same class. However it tells me the function 'dbselectquery()' is undefined. So how can I get a function from within a class run another function from within the same class

PHP Code:
//testclass.php
require 'function_library/crew473_DB_Connection.php';
class 
classA{
var 
$DB_QUERY;
var 
$table;
var 
$query_line;
function 
dbSelectQuery($DB_QUERY,$table,$query_line){
$query=mysql_query($DB_QUERY)or die("Could not query table \"".$table."\"<p>The database returned the following error(s):</p>".mysql_error().$query_line);
echo 
"inside the class".$query."<br />";
return 
$query;
}
/* Closes line function dbSelectQuery() */
function select_test(){
$DB_QUERY_select='SELECT *';
$DB_QUERY_from=' FROM test_table';
$DB_QUERY=$DB_QUERY_select.$DB_QUERY_from;
$table='test_table';
$query_line='Query originated on line 16 in testclass.php in select_test()';
$result=dbSelectQuery($DB_QUERY,$table,$query_line  );
while(
$result2=mysql_fetch_array($result)){
echo 
"Row ".$result2[0]."<br />Column 2 ".$result2[1]."<br /Column 3 ".$result2[2]."<br />Column 4 ".$result2[3]."<br />Column 5 ".$result2[4]."<br />Column 6 ".$result2[5]."<br />Column 7 ".$result2[6]."<br />Column 8 ".$result2[7]."<br />Column 9 ".$result2[8]."<br />Column 10 ".$result2[9]."<br /><br /> ";     
}
}


PHP Code:
//quicktest.php
//echo phpinfo();
include 'testclass.php';
$myclass=new classA;
//$DB_QUERY_select='SELECT *';
//$DB_QUERY_from=' FROM test_table';
//$DB_QUERY=$DB_QUERY_select.$DB_QUERY_from;
//$table='test_table';
//$query_line='Query originated on line 7 in quicktest.php';
//echo 'Before the Loop<br />';
//$result=$myclass->dbSelectQuery($DB_QUERY,$table,$query_line);
//echo $result."<br />";
//while($result2=mysql_fetch_array($result))
//{
 //echo 'Loop Started<br />';
 //echo $query."<br />";
 //echo "Row ".$result2[0]."<br />Column 2 ".$result2[1]."<br />Column 3 ".$result2[2]."<br />Column 4 ".$result2[3]."<br />Column 5 ".$result2[4]."<br />Column 6 ".$result2[5]."<br />Column 7 ".$result2[6]."<br />Column 8 ".$result2[7]."<br />Column 9 ".$result2[8]."<br />Column 10 ".$result2[9]."<br /><br /> ";
//}
$result3=$myclass->select_test();
echo 
$result3

Reply With Quote
  #4  
Old February 2nd, 2005, 10:13 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
dbSelectQuery() is declared within the same class?

try calling it using $this->dbSelectQuery()
same goes for the class's instance variables... $this->variable

Variables local to the method being called [obviously] don't need the be used this way... just instance variables

Reply With Quote
  #5  
Old February 2nd, 2005, 05:12 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 7
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Smile

Quote:
Originally Posted by MadCowDzz
dbSelectQuery() is declared within the same class?

try calling it using $this->dbSelectQuery()
same goes for the class's instance variables... $this->variable

Variables local to the method being called [obviously] don't need the be used this way... just instance variables


It's working now. Thanks. Hopefully, but probably not, this will be my last post in this thread

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Newbie to classes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT