tags:

views:

44

answers:

3

I have two completely separate queries that were transferred over to procedures. When they were ad-hoc queries, they ran great but now that I have stored them, things have gotten strange.

PHP calls a class method which returns the values of one of the procs. The second method is then called and fails. When I run the run the proc that PHP fails to give me, directly from the command line, it gives me the results just fine. This is certainly strange.

Can someone tell me if I'm missing something? I'd be happy to post some code but in the interest of time, I figured I would ask and see if this is a common problem first.


This is the function that connects;

function connect()
{
    $conn = mysql_connect($host, $user, $pass);
    if(!$conn){
        echo 'Error: cannot connect';
    }else{
        $selectDB();
    }
}

This is the proc that is failing:

BEGIN
SELECT name FROM source ORDER BY name ASC;
END

This is the first proc that returns the results.

BEGIN
SELECT cpw, cpw_id FROM cpw ORDER BY cpw ASC;
END
A: 

Without first seeing code, it's difficult to say. I can assure you that this isn't normal behavior though. From your comment on the OP, it would seem that your user-credentials are failing after the first query. I would check to be sure that nothing is overwriting them, or modifying them in anyway between requests.

At this point, it would be helpful to see the piece of code that makes the connection, and calls for the procs.

Jonathan Sampson
Hey Jonathan, I'll post the connection method in a minute.. brb
jim
A: 

Seeing the code will allow for more specific help, but have you checked already for variable name conflicts in the procs that could be overwriting your connection credentials?

zenbuffy
No, I didn't... This is a great point. I will check this now. Thanks.. brb
jim
Hey Zen, I looked and there is no conflict. I was kinda hoping that would have been it.
jim
Ok - should the user be "SYSTEM" and should the host be "localhost"? I presume you're on a local system, so it's likely that "localhost" is correct, but is the username supposed to be "SYSTEM"?
zenbuffy
That also struck me as weird because I have never seen that before. Normally it's "root" but never system. I am on a local machine but my db is on a VPS on the net.
jim
That would seem to point to those credentials being reset or changed somewhere unexpectedly. It's denying access to SYSTEM because it's obviously expecting root.Have you tried some very simple debugging, e.g. sending the username, host, and pass info to screen or a log as you call the connection method and also the procs to verify that they are correct and possibly narrow down where they're being changed (it seems likely that this is the case).
zenbuffy
OK, this gets weirder by the minute. Here is a message I have never seen before: "Commands out of sync; you can't run this command now"
jim
Are you using mysql_use_result() in either of your procs?
zenbuffy
Check here (http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html) for info on this error, it's straight from the database. Basically, either you haven't freed a previous result before trying to use a new one, or you're getting two results at once, and it doesn't know which one to use (as you haven't told it).
zenbuffy
I'm also reading the mysql manual and saw that. I'm using free_result after my fetch_array statement so I don't see the issue. I'm not using use_result at all as I didn't think it was necessary.
jim
Ok, and are you checking right to the end of the first data set?
zenbuffy
The first data set completes and there is where I see the error. So, the second query is failing because it is looking for this mysql_use_result(). I've googled the php equivalent but can't find such an animal. Should I just get rid of the free_result?
jim
If you're just fetching your data using fetch row then you can nuke the free_result. Perhaps that's where the out of sync error is cropping up.
zenbuffy
Hey Zen, thanks for helping me on this. Actually, I'm using fetch_array not row. I'm baffled at the moment because I can't seem to find this mysql_use_result function or anything close to it. PHP docs talk about the error but I haven't yet found the solution. :)
jim
Could you post proc code and the php that calls them? Might help me narrow it down.
zenbuffy
Sure.. Up top in a minute. Thanks again.
jim
Hey Zen? It looks like I will have to use mysqli instead as there is support for procs. Thanks for all the help!
jim
No problem. Feel free to get in touch if you're still stuck.
zenbuffy
Thanks, will do.. :)
jim
A: 

In case the function connect() you've posted is real code (and not an abbreviated version for SO) you have a "problem" with PHP's variable scope (and lifetime). $host, $user and $pass are unknown within the function. I.e. effectively mysql_connect(null, null, null);is executed and the php-mysql extension tries to establish the default connection accrding to the ini parameters mysql.default_host, mysql.default_user and mysql.default_password.

If you set the error reporting to E_ALL ( e.g. via error_reporting(E_ALL) prior to the function call) you should get some "undefined variable" messages.

VolkerK