Jump to content

PHP Class Help


Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
      }
   }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.