RedBoffin Posted April 23, 2007 Share Posted April 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/ Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 can you post the function $this->databaseConnector->selectQuery Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/#findComment-236008 Share on other sites More sharing options...
RedBoffin Posted April 23, 2007 Author Share Posted April 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/#findComment-236052 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/#findComment-236094 Share on other sites More sharing options...
RedBoffin Posted April 23, 2007 Author Share Posted April 23, 2007 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..! Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/#findComment-236267 Share on other sites More sharing options...
RedBoffin Posted April 23, 2007 Author Share Posted April 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48273-solved-_get-variable-mysql-select-problem/#findComment-236460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.