IanS Posted September 4, 2006 Share Posted September 4, 2006 HI,Im new to PHP - so still getting a grip on hopefully whats obvious to someone,although having read the manual - not obvious to me.I am modifying an existing script and it just dont seem to work on my machine.a WAMP server.Environment as follows:Apache 3.33(win32)PHP V5.04MySQL 4.1.10a-nt-extension:mysqli***** Test.php ***include ("./sqlfunctions.php")// have code that sets up a variable called $statment.$statment = "select * from debtors"$res = sql($statment)**** sqlfunctions.php ****function sql($statment,$assoc=1){ echo $statment // gives me select * from debtors // connect to DB If (!mysql_connect(host,user,pass)) { echo "error" . mysql_error(); exit; }echo $statment // gives me nothing - variable contents seemed to have been cleared??// when i try to use $statment in actual qry - i get "empty qry" error....}So - any obvious mistakes here??Thing that annoys me is that im told this style of coding works on someones production LAMP environment!!regardsIan. Quote Link to comment https://forums.phpfreaks.com/topic/19637-empty-query-and-cleared-variables-in-mysql/ Share on other sites More sharing options...
wildteen88 Posted September 4, 2006 Share Posted September 4, 2006 The probelm is you have the mysql improved extension enabled. However you are using the normal mysql functions in the script. (mysql_connect, mysql_error etc). the mysql iimproved extension (php_mysqli.dll) doesnt work with the normal mysql functions (mysql_*). You'll need to enable the nomal mysql extension (php_mysql.dll) in order for your script to work.Both extensions have their own set of mysql functions. For exmaple the mysql improved extension has these functions:mysqli_connectmysqli_querymysqli_fetch_arrayNotice the i after mysql. The mysqli extension will only work with the functions prefixed withg mysqli. It wont work with functions prefixed with mysql. In order to use the functions prefixed with mysql you'll need to use the normal mysql extensin (php_mysql.dll) Quote Link to comment https://forums.phpfreaks.com/topic/19637-empty-query-and-cleared-variables-in-mysql/#findComment-85633 Share on other sites More sharing options...
IanS Posted September 5, 2006 Author Share Posted September 5, 2006 Looked at my PHPinfo and BOTH mysql and mysqli are enabled.If i take the php mysql_* code out of the Included PHP file and out of the function, and just use it directlyas inline code, it all works fine.SO,That leads me to believe the actual syntax of the Mysql code is fine.It something to do with the Include process, the Function process, or maybe .ini file or some combination.Just dont know?Any more ideas?Is what im doing - SQL code within user function within Include file - all OKin principle?Ian Quote Link to comment https://forums.phpfreaks.com/topic/19637-empty-query-and-cleared-variables-in-mysql/#findComment-86118 Share on other sites More sharing options...
wildteen88 Posted September 5, 2006 Share Posted September 5, 2006 Do you have display_errors turned in the php.ini and is error_reporting set ot E_ALL? How do you know it doesnt work. What does it do. Could you provide more info.Is the following code, whats exactly in test.php[code=php:0]include ("./sqlfunctions.php")// have code that sets up a variable called $statment.$statment = "select * from debtors"$res = sql($statment)[/code]If it is it should be this:[code=php:0]include ("./sqlfunctions.php"); // missing a ;// have code that sets up a variable called $statment.$statment = "select * from debtors"; // missing a ;$res = sql($statment); // misssing a ;[/code]You have forgotten to add the semi-colon on the end of each line. You must make sure every line has a semi-colon at the end of it, unless you are defining a function, using an if statement/while loop etc. Quote Link to comment https://forums.phpfreaks.com/topic/19637-empty-query-and-cleared-variables-in-mysql/#findComment-86286 Share on other sites More sharing options...
IanS Posted September 6, 2006 Author Share Posted September 6, 2006 Sorry, i wasnt literal enough.The code i was giving was an example.1. php.ini display_errors = Onerror_reporting = E_ALL $ ~E_NOTICE2. more Code** test.php **<?PHPinclude ("./sqlfunctions.php"); // contains user function called sql, for mysql functions$selected_id='1';$statment = "select * from debtors where Ourref=$selected_id";$res = sql($statment);$row = mysql_fetch_array($res);..?>** sqlfunctions.php **<?PHPfunction sql($statment,$assoc=1){ $dbhost= "host"; $dbuser= "user"; $dbpass= "pass"; $dbname= "dbase"; echo $statment; // displays correct contents of $statment eg: "select * from .... " etc. if(!mysql_connect($dbhost,$dbuser,$dbpass)) { echo Stylesheet() . "\n\n<div class=Error>"; echo $Translation["error:"] . mysql_error(); echo "</div>"; exit; } echo $statment; // display empty (or null?) contents of $statment eg: "".....?>Summary=======The first echo displays the correct contents.The second echo shows nothing.The sql statment mysql_connect runs and connects.The if(!mysql_connect(xxx)) { failure code}failure code isnt actioned, therefore no other functions or actions are taken before the second echo $statment;To me (limited expertise excepted), The $statment variable seems to be reset by the mysql_connect function call.I've even used other variable names eg:$statmentxc and get the same result.This is sending me bonkers!!There has to be a simple solution to this.RegardsIan. Quote Link to comment https://forums.phpfreaks.com/topic/19637-empty-query-and-cleared-variables-in-mysql/#findComment-86814 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.