473,770 Members | 5,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql_close(): supplied argument is not a valid MySQL-Link resource

I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sq l)).

I seem to get this error only when I try and create a new copy of this
class when one already is open.

For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.

Below is my db code and the example.

class Database{

private $conn;

function __construct(){
$this->conn = mysql_connect(D B_SERVER, DB_SERVER_USERN AME,
DB_SERVER_PASSW ORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db (DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}

function query($sql){
$query = mysql_query($sq l);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}

function fetch_array($re sult){
$array = mysql_fetch_ass oc($result);
return $array;
}

function close(){
mysql_close($th is->conn);
$this->conn = null;
}

function __destruct(){
$this->close();
}
}

Example

function get_events($whe re = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where )){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($q uery)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}

//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($q uery);

load is called from constructor.

If anyone has an idea please please help this problem is very annoying.
Dec 12 '07 #1
2 9357
Greetings, Iain Adams.
In reply to Your message dated Wednesday, December 12, 2007, 18:55:27,
I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sq l)).
I seem to get this error only when I try and create a new copy of this
class when one already is open.
For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.
Below is my db code and the example.
class Database{
private $conn;
function __construct(){
$this->conn = mysql_connect(D B_SERVER, DB_SERVER_USERN AME,
DB_SERVER_PASSW ORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db (DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}
function query($sql){
$query = mysql_query($sq l);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}
function fetch_array($re sult){
$array = mysql_fetch_ass oc($result);
return $array;
}
function close(){
mysql_close($th is->conn);
$this->conn = null;
}
function __destruct(){
$this->close();
}
}
Example
function get_events($whe re = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where )){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($q uery)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}
//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($q uery);
load is called from constructor.
If anyone has an idea please please help this problem is very annoying.
I have an idea.. Why do You use class to do this things?
You coding style is very naive and You're using alot of defaults in Your code.

If You REALLY need to get it to work, go studying the link_identifier
parameter of mysql_* functions.

After that, Your code should looks like

class Database
{

protected $conn; // do not use 'private' if You aren't absolutely sure You want it

function __construct()
{
$this->conn = mysql_connect(D B_SERVER, DB_SERVER_USERN AME, DB_SERVER_PASSW ORD)
or die('Could not connect: ' . mysql_error());
if(is_resource( $this->conn))
{
mysql_select_db (DB_DATABASE, $this->conn) or die('Could not connect: ' . mysql_error());
}
}

function query($sql)
{
$query = mysql_query($sq l, $this->conn);
if(!is_resource ($query))
{
die("Error: in database query");
}
else
{
return $query;
}
}

function fetch_array($re sult)
{
$array = mysql_fetch_ass oc($result);
return $array;
}

function close()
{
if(mysql_close( $this->conn))
{
$this->conn = null;
}
}

function __destruct()
{
$this->close();
}
}
--
Sincerely Yours, AnrDaemon <an*******@free mail.ru>

Dec 12 '07 #2
Iain Adams wrote:
I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sq l)).

I seem to get this error only when I try and create a new copy of this
class when one already is open.

For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.

Below is my db code and the example.

class Database{

private $conn;

function __construct(){
$this->conn = mysql_connect(D B_SERVER, DB_SERVER_USERN AME,
DB_SERVER_PASSW ORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db (DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}

function query($sql){
$query = mysql_query($sq l);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}

function fetch_array($re sult){
$array = mysql_fetch_ass oc($result);
return $array;
}

function close(){
mysql_close($th is->conn);
$this->conn = null;
}

function __destruct(){
$this->close();
}
}

Example

function get_events($whe re = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where )){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($q uery)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}

//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($q uery);

load is called from constructor.

If anyone has an idea please please help this problem is very annoying.
Iain,

Your class looks fine. However, I suspect what's happening is your
__destruct() function is getting called when you don't expect it. This
is often done when you pass something by copy instead of by reference.

Put an echo in your __destruct() function (and maybe even in close())
and see if it isn't being called when you don't expect it to be called.

And BTW - private is correct for your connection. protected should be
used only when absolutely necessary, if even then.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Dec 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17352
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? The user of my (hopefully to be opensourced) program has the ability to create database/table/column names on the fly. I'm aware of obvious characters such as ., , things like >, etc., which won't work, but haven't been able to source a...
1
2394
by: cantelow | last post by:
Hi. I have a successful compile with no complaints of dso php with mysql that is getting the message, mysql_close not resolved in libmysqlclient.so on apache start after install in libexec. There is a suggestion in the online php docs to add something to ld.so.conf, but we're on Tru64, don't have that option. There's also a suggestion to set LD_LIBRARY_PATH, and I've done that at both compile time and apache restart time with no...
2
1538
by: cantelow | last post by:
Sorry, this is a repeat message, but I think I wasn't brief enough last time. I was able to compile DSO for apache php with mysql using tips in the online docs, but I'm still getting unresolved libmysqlclient.so mysql_close at runtime. This is not covered in the online doc user comments because the msg doesn't say that libmysqlclient.so isn't found. Has anyone seen this? mysql version 2.23.54, php version 4.3.10 or
6
1799
by: bettina | last post by:
The program works local ok. The data were found in the datenbank and display by the program. When I wanted to test my program online, I get the following message: mysql_result(): supplied argument is not a valid MySQL result resource in .... this message several times for all the mysql_result.... The connection to the Datenbank seems to work because I didn't receive an error message..... that's the script for the connection:
3
5416
by: F. GEIGER | last post by:
Im on Python 2.3.4, using pysqlite 2.0.0 (final). When I try to execute self._dbc.execute(q, data) where q is 'select count(*) from Difflets ' and date is None I get the following exception:
5
3561
by: gp | last post by:
i am implementing Iterator in a class, I have pretty much copied the code from php.net on Object Iteration. Adding all the normal methods for the task...rewind, current, next, etc. I was attempting to add a seek method but am stymied by the above warnin, as well getting a seek on index 0 (zero) to work. public function __construct($contents) { if ( is_array($contents)) { if ( is_array($contents)) {
7
8134
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h> #include<stdlib.h> #include<math.h>
6
3684
by: rhepsi | last post by:
Hi All... I Came across this error while populating a combobox from a datatable (I'm working in VB.NET): Specified argument was out of the range of valid values. Parameter name: '-1' is not a valid value for 'index'
1
1353
by: guoxin | last post by:
Hi All, May i know how to rectify the following php error? Thanks folks Warning: Invalid argument supplied for foreach() in /home/alan/do_add-user.php on line 36 do_add-user.php.php on line 304: $project_array = $_POST;
11
4757
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do. $testvend = SELECT language FROM vendor_details WHERE id = $vendorid ...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.