Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:05+00:00 2026-05-31T11:47:05+00:00

I have a class written in php that is supposed to find the labels

  • 0

I have a class written in php that is supposed to find the labels that will be put beside textboxes in the main file (when I create it). Basically the class searches through my DB to find the labels for the selected shape(which will be passed from the main page). When I run the functions using dummy values, it is as if my $dbConnection value is not setting the value. I get an error saying that $dbConnection is undefined, and then more errors saying that functions expect a certain parameter type, but the type given is null. When I look, they are all pointing to the $dbConnection variable. My class looks like this:

 class lblFinder
        {
            //declarations
                private $dbConnection;
                private $dbName="matecalculator";

                private $cmd="";
                private $size=0;
            //end of declarations
            public function __construct()
            {
                $this->dbConnection=mysql_connect("localhost", "root", "");
            }

            public function setSize($shape)
            {
                if($this->dbConnection===false)
                {
                    echo "<p>Something went wrong.</p><p> Error Code:".mysql_errno().": ".mysql_error()."</p>";
                }
                else
                {
                    if(mysql_select_db($this->dbName,$this->dbConnection))
                    {
                        $cmd="SELECT COUNT(varID) FROM tblvariables WHERE shapeID IN(SELECT shapeID FROM tblshapes WHERE shapeName='$shape')";
                        $qryResults=mysql_query($cmd,$dbConnection);

                        //get results
                        while (($Row = mysql_fetch_row($qryResults)) !== FALSE)
                        {
                            $size=$Row[0];
                        }

                        mysql_free_result($qryResults);
                    }
                    else
                    {
                        echo "<p>Something went wrong.</p><p> Error Code:".mysql_errno().": ".mysql_error()."</p>";
                    }
                }
            }

            public function getSize()
            {
                return $this->size;
            }

            public function setLabels($shape)
            {
                //declarations
                    $l=array();
                //end of declarations

                $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')";
                $qryResults=mysql_query($cmd,$dbConnection);

                $i=0;
                if(($Row = mysql_fetch_row($qryResults)) !== FALSE)
                {
                    $l[i]=$Row[0];
                    $i++;
                }
                mysql_free_result($qryResults);
                return $l;
            }
        }

Just for kicks and giggles, here is my test file (which passes dummy values). I know that round is a valid value from the DB, so I know that is not the problem.

 $arr=array();
        $lf=new lblFinder;
        $lf->setSize("Round");
        echo "Size=".$lf->getSize();
        $arr=$lf->setLabels("Round");
        $i=0;
        foreach($arr AS $label)
        {
            echo "Label $i is $label";
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:47:06+00:00Added an answer on May 31, 2026 at 11:47 am

    There is a typo/bug in your class, corrected here for you:

    public function setLabels($shape)
            {
                //declarations
                    $l=array();
                //end of declarations
    
                $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')";
                $qryResults=mysql_query($cmd,$dbConnection);
    
                $i=0;
                if(($Row = mysql_fetch_row($qryResults)) !== FALSE)
                {
                    $l[$i]=$Row[0]; // The $ symbol was missing from the i
                    $i++;
                }
                mysql_free_result($qryResults);
                return $l;
            }
    

    Also, for simplicity sake, you could have written this as follows:

    public function setLabels($shape)
            {
                //declarations
                    $l=array();
                //end of declarations
    
                $this->cmd="SELECT varDesc FROM tblVariables WHERE shapeID IN(SELECT shapeID FROM tblShapes WHERE shapeName= '".$shape."')";
                $qryResults=mysql_query($cmd,$dbConnection);
    
                if(($Row = mysql_fetch_row($qryResults)) !== FALSE)
                {
                    $l[]=$Row[0]; // Do not need to increment index of array, php does automatically
                }
                mysql_free_result($qryResults);
                return $l;
            }
    

    If the connection is returning null, then you can troubleshoot that issue by outputting the error that is happening during connection as follows:

    $this->dbConnection=mysql_connect("localhost", "root", "") or die("Error connecting to DB: " . mysql_error());
    

    EDIT:
    die() halts execution of the PHP script. In cases like this, it’s useful for debugging, since it prevents the rest of the script running and dumping a bunch of errors (like it is doing for you currently). mysql_connect("localhost", "root", "") or die(mysql_error()); tells the script that if the connection is not successful, to halt the php script and output the mysql error, so you have information to troubleshoot with.

    One of the challenges is that while you are telling us the line number, but which line is that in your code quoted above? With the line number, PHP is telling you exactly where the failure is. Can you tell us which line(s) are 37 and 40?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an apex class that will create a PDF quote and attach
I have written a class in php that is supposed to intake a formula
I have written a class that will handle internal logging in my application. Now
I have a class, written in Java, that makes a call to a PHP
I have written a PHP class that gets the headers from a .xls spreadsheet
I have a LinkedIn API class that I've written for PHP that makes use
I have written a class in python that implements __str__(self) but when I use
I have a class that I've written a TypeConverter for. I want to keep
I have inherited a project that has class libraries written in VB.NET, some of
I have a class with several functions that can render images. // render.php class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.