Jump to content

Empty QUERY and cleared variables in MYSQL


IanS

Recommended Posts

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.


Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.