Jump to content

Recommended Posts

I have an object called PackagesParser which has a function called catchCommand.

 

function catchCommand() {

	if ( $_GET['command'] == 'insert' ) {

		$this->parseFile();

	}

	if ( $_GET['command'] == 'delete' ) {

		$this->deletePackage( $_GET['packageTitle'] );

	}

}

 

This function is called every time packagesParser.php is displayed, thus allowing PackagesParser to 'listen' for commands sent to it.

 

I am sending the following commands to PackagesParser using http://domain.co.uk/packagesParser.php?command=delete&packageTitle=BI1SB

 

This command triggers a call to deletePackage( $packageTitle )

 

function deletePackage( $packageTitle ) {
	echo('PackagesParser->deletePackage( '.$packageTitle.' )<br/>');

	$result = $this->databaseConnector->selectQuery("SELECT * FROM Packages WHERE packageTitle = '$packageTitle'");

	if ( mysql_num_rows( $result ) == 0 ) {

                    echo('No records!<br/>');

                } else {

                    echo('Total records : '.mysql_num_rows( $result ).'<br/>');

                }

}

 

The Packages Table has the following structure and data

 

id    packageTitle

 

16  BI1SB

17  BI1SB

18  BI1SB

 

So... deletePackage should display 'Total records : 3' but it actually displays 'No records!' and I have no idea why?

 

Can anyone spare a few minutes to help me sort this out?

 

Thank you.

Here it is...

 

// Returns a mysql result
function selectQuery( $query ) {
//			echo('DatabaseConnector->selectQuery()<br/>');

	$databaseConnection = mysql_connect( $this->host, $this->username, $this->password ) or die ('Unable to connect to database because: '.mysql_error() );		
	mysql_select_db( $this->database, $databaseConnection ) or die('Unable to select database because '.mysql_error() );

	$sql = mysql_query( $query ) or die( mysql_error() );

	return $sql;

}

 

Thanks for taking the time to look at this for me.

Are you re-instatiating the connection each time a query is called? That is bad practice man. You should only connect to the DB one time in a script. With the code above you could end up creating 10 mysql connections and than whoops your mysql is kaput.

 

I would re-do that function to not use the mysql_connect and if it is a class, which it seems like, do the database connection in the constructor. So when that class gets instantiated it is done and you do not have to worry about connecting to mysql again for the rest of the script.

Thanks for pointing that out Frost110. I usually do what you suggested but had just copy and pasted the connection in - this is a bit of a spike solution so efficiency isn't a key requirement yet. You were right though and thanks again for the comments.

 

Can anyone help me with the problem though.

 

It seems that the string 'BS1SB' that is in the url and stored in $_GET['packageTitle'] doesn't match the (varChar) field data 'BS1SB' which is really weird. I cannot work out why this is happening, despite googling like a crazy thing..!

I may have solved this.

 

I changed the select query to this...

 

$result = $this->databaseConnector->selectQuery("SELECT Packages.id FROM Packages WHERE Packages.packageTitle LIKE '%$packageTitle%'");

 

...and now it all works like it should do.

 

Thanks to everyone who replied.

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.