Jump to content

Archived

This topic is now archived and is closed to further replies.

Sam Granger

Help, small error. MySQL Class. Not sure if I fixed it correctly?

Recommended Posts

[code]<?php
include ('./includes/db_settings.php');
class mysql {

  function Connect($DB_SERVER, $DB_USER, $DB_PWD, $DB_NAME){

    $connection = mysql_connect("$DB_SERVER",
    "$DB_USER",
    "$DB_PWD");
    mysql_select_db("$DB_NAME", $connection);

  }

  function Close(){

    mysql_close($this->connection);

  }

  function FetchRow($query){
    $rows = mysql_fetch_row($query);
    return $rows;
  }

  function FetchArray($query){
    $array = mysql_fetch_array($query);
    return $array;
  }

  function FetchNum($query){
    $num = mysql_num_rows($query);
    return $num;
  }

  function Query($sql){
    $query = mysql_query($sql) or die(mysql_error());
    return $query;
  }

}
?>[/code]

I have this file included in another php file. When I do this:

[code]$DB->Close();[/code]

I get the following error:

[quote]Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/sgclient/public_html/mysql_class.php on line 16[/quote]

I tried changing:

[code]
  function Close(){

    mysql_close($this->connection);

  }[/code]

to:


[code]  function Close(){

    mysql_close();

  }
[/code]
And the error disappears. However I'm not the best at php. This mysql function was not written by me. Is this the correct fix? Or should it be something else?

Thanks in advance for any help and thanks for reading!

Share this post


Link to post
Share on other sites
Have you checked that you have actually opened the mysql connection, because you could be trying to close a connection that does not exist.

And try  or die(mysql_error()); after it to see if it throws an actually error.

function Close(){ mysql_close($this->connection) or die(mysql_error());}

Share this post


Link to post
Share on other sites
$connection = $DB->Connect($DB_SERVER, $DB_USER, $DB_PWD, $DB_NAME);

This code should open the connection, correct? If this is the case I have opened it. Let me try what you just suggested.

Share this post


Link to post
Share on other sites
Sorry, I wasnt very clear.

Have you actually opened a mysql connection on that page?

Share this post


Link to post
Share on other sites

×

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.