SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query fails when passing second parameter in mysql_query()

    Been working on a web app, have a core class with 2 database connections $connection and $connectionProgramme each connect to different database with same user and password

    All works fine throughout system but on one of the classes (which extends the core class), when I pass the db connecction in the mysql_query, it fails and when I leave it out it works fine. I pass the second parameter throughout the rest of the functions in the class and other classes and it's fine, just this one.

    Jus trying to work out why? If anyone has ideas be great to hear them. Have included code below:

    Connection function in core class:

    {start of snippet}
    // CONNECT TO DATABASE
    private function connect() {

    #deal with system database connection
    $conn = mysql_connect($this->host, $this->username, $this->password);

    if($conn == false)
    $this->error = "Unable to connect to database server: ".mysql_error();

    if(!mysql_select_db($this->database, $conn))
    $this->error = "Unable to select system database: ".mysql_error();

    $this->connection = $conn;



    #deal with programme database connection
    $connProgramme = mysql_connect($this->host, $this->username, $this->password, true);

    if($connProgramme == false)
    $this->error = "Unable to connect to database server: ".mysql_error();

    if(!mysql_select_db($this->databaseProgramme, $connProgramme)) {
    $this->error = "Unable to select programme database: ".mysql_error();
    }
    $this->connectionProgramme = $connProgramme;

    }
    {end core snippet}

    The other class extends this one. Has a function with the snippet below:
    {snippet start}
    $sqlProgramme = "SELECT _programme.* ".
    "FROM _programme, person ".
    "WHERE _programme.personID=person.personID ".
    "ORDER BY firstName, lastName";
    $resProgramme = mysql_query($sqlProgramme, $this->connectionProgramme) or die("issue is ".mysql_error());
    {snippet end}


    If I take out $this->connectionProgramme in the mysql_query function the query works fine. If I leave it in it fails but NO mysql_error() is generated.

    I'm sure it's something simple or stupid I'm doing but can't work this one out. Any suggestions greatly appreciated.

    Thanks

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    I assume your class definition specifies connectionProgramme as a property?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    I assume your class definition specifies connectionProgramme as a property?
    hi StarLion, thanks for responding.

    At start of my core class I have the following to define them:

    public $connection;
    public $connectionProgramme;

    In the constructor function I execute the connect function as follows:

    $this->connect();

    ... and this function assigns each of the connections to the 2 variables ($connection and $connectionProgramme)

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the query is FAILING and not simply returning 0 rows?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would check to make sure $this->connectionProgramme is in a proper value. Echo it just before the query.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •