dennismonsewicz Posted May 11, 2010 Share Posted May 11, 2010 Before I begin, let me start off by saying that I am a newb when it comes to most things in using classes in PHP, so I apologize for the annoyance factor of simple questions Ok.. I am building out a class for a project I am working on and I am getting some weird errors and I have no idea how to solve them. <?php class Deductible { __construct() { mysql_connect('localhost', 'root', 'root')or die(mysql_error()); mysql_select_db('test')or die(mysql_error()); } function carrierSelect($carrier) { $results = mysql_query("SELECT DISTINCT manufacturer FROM portal_deductible WHERE carrier = '" . $carrier . "'"); return mysql_fetch_object($results); } } ?> Error Message: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in /Applications/MAMP/htdocs/class/phplib.php on line 4 If I change my code to the following <?php class Deductible { function Deductible() { mysql_connect('localhost', 'root', 'root')or die(mysql_error()); mysql_select_db('test')or die(mysql_error()); } function carrierSelect($carrier) { $results = mysql_query("SELECT DISTINCT manufacturer FROM portal_deductible WHERE carrier = '" . $carrier . "'", $this->Deductible()); return mysql_fetch_object($results); } } ?> I now get this error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/class/phplib.php on line 9 Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/class/phplib.php on line 10 Any ideas on why this is happening? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/ Share on other sites More sharing options...
KevinM1 Posted May 11, 2010 Share Posted May 11, 2010 A constructor still needs the 'function' keyword before it, as in: function __construct() {} Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056730 Share on other sites More sharing options...
dennismonsewicz Posted May 11, 2010 Author Share Posted May 11, 2010 Ah! Ok so that problem is fixed Now here is another one <?php require_once 'phplib.php'; $conn = new Deductible(); $manf = $conn->carrierSelect('att'); foreach($manf as $key => $val) { $conn->errorDisplay($manf); } ?> I can't get any results to return when using the following code... but if I do a $conn->errorDisplay($manf) I get everything back... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056734 Share on other sites More sharing options...
KevinM1 Posted May 11, 2010 Share Posted May 11, 2010 Well, what does errorDisplay do? Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056736 Share on other sites More sharing options...
dennismonsewicz Posted May 11, 2010 Author Share Posted May 11, 2010 Sorry all it does is print_r the array you pass into it. updated code <?php class Deductible { function __construct() { mysql_connect('localhost', 'root', 'root')or die(mysql_error()); mysql_select_db('test')or die(mysql_error()); } function carrierSelect($carrier) { $results = mysql_query("SELECT DISTINCT manufacturer FROM portal_deductible WHERE carrier = '" . $carrier . "'"); while($row = mysql_fetch_object($results)) { $array[] = $row; } return $array; } function errorDisplay($msg) { echo '<pre>'; print_r($msg); echo '</pre>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056741 Share on other sites More sharing options...
KevinM1 Posted May 11, 2010 Share Posted May 11, 2010 Wouldn't something like the following work: foreach($manf as $val) { echo $val->someColumnName; } Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056742 Share on other sites More sharing options...
dennismonsewicz Posted May 11, 2010 Author Share Posted May 11, 2010 It works!! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/201409-php-class-help/#findComment-1056749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.