Jump to content


Photo

Empty QUERY and cleared variables in MYSQL


  • Please log in to reply
4 replies to this topic

#1 IanS

IanS
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 September 2006 - 04:26 AM

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.04
MySQL 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!!


regards

Ian.




#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 10:07 AM

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_connect
mysqli_query
mysqli_fetch_array
Notice 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)

#3 IanS

IanS
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 September 2006 - 02:02 AM

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 OK
in principle?


Ian

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 September 2006 - 12:10 PM

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
include ("./sqlfunctions.php")

// have code that sets up a variable called $statment.
$statment = "select * from debtors"
$res = sql($statment)


If it is it should be this:
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 ;

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.

#5 IanS

IanS
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 September 2006 - 12:49 AM

Sorry, i wasnt literal enough.
The code i was giving was an example.

1. php.ini
display_errors = On
error_reporting = E_ALL $ ~E_NOTICE

2. more Code

** test.php **
<?PHP
include ("./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 **
<?PHP

function 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.

Regards

Ian.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users