Weird PHP/MySQL phenomenon

LotsOfZeros

^^^ Bi-Winning ^^^
Feb 9, 2008
4,648
118
0
www.makemoniesonline.com
Going to need some help here because my head feels like it's going to blow up.

Working on a script that does a simple mysql_connect and then a three column insert statement using mysql_query($query);

I run the script and nothing gets updated to the database and no errors output to the browser. I am connecting just fine from what I can tell because when I change the user, password or database in the mysql_connect, I am actually getting the connection error message.

The script seems to be running just fine, indicated by no errors output but here's the weird part: when I echo out the $query variable, the input statement shows clearly in the browser, syntactically perfect. I can even copy and paste it into PHPmyAdmin and run it, resulting in an error free insert of the row.

I've spent the last 2 hours messing with it trying to debug whatever is keeping the table from updating as a result of the INSERT statement and I really feel like I'm going insane.

Any ideas?
 


are you doing a mysql_select_db($dbname) ?

if so, add debugging to each statement by adding 'or die(mysql_error())' to the db connect and to the query line.

e.g.
mysql_query($sql) or die(mysql_error());
 
check you are even connecting to mysql...

echo mysql_error();

at every point including the actual mysql_connect() point...

assuming you have a db connect in the script coz, if you don't.... well..
 
OK, I've basically covered all of the bases on suggestions added here and it kept coming up with "No database selected" where I have used the following:

mysql_connect('localhost', 'user', 'pass') or
die("Could not connect: " . mysql_error());
mysql_select_db('database');

This exact connection setting has been used several times before on this host with no problems. I explicitly added the user to the database when I created them both.....
... and guess what? cPanel does not show the user assigned to that database any more.
Has anyone ever seen this before? Where somehow the user gets dropped?

Then again, this IS a Wiredtree server.
 
My freaking head hurts. I know I added the user which really concerns me.
Thanks everyone for your fresh suggestions, I probably would have been cranking away on this joke all night.
 
Step one, hire a professional to look at it.

I could walk you though the whole thing, but an amateur doesn't understand where to look. You're probably not going to give us proper source code either, so how can we help?

People could tell you to output what, but if you don't know where to put the proper messages you won't know shit. MOST PHP SQL calls do not output error messages so unless you dig a little bit you won't know what's wrong.

Going out on a limb here, if you can run the statement normally and it inserts the row (say in PHPmyAdmin) that would lead me to believe when you're connecting to the database that you provided the wrong credentials. If that's the case you have no real reason to be messing with it as it's the most basic part of the entire process.

Then again, this IS a Wiredtree server.

Wiredtree server has nothing to do with it, again get ahold of someone (like me) that knows what the frack they are doing.
 
Yeah, the server excuse was due to me being too tired - the configuration hasn't changed so it was definitely something on my end. I'm going to have a fresh look at it again this morning. Not sure what happened really. Either way, I hope to find out soon.

It's highly possible it was me inadvertently removing the user since I did a lot of cleaning up of old databases in cPanel before working on the script.

Rage, I'll PM you if I come across it again though, Thanks.
 
Try a PHP debugger.
Firephp extension of Firebug is a good tool or Xdebug.

Code:
http://xdebug.org/index.php

To understand better you should run phpinfo() on your host.
Write a simple test.php file with the following code:
Code:
<?php
phpinfo()
?>

Copy and paste the full (not the html) output of phpinfo() into this page form:

Code:
http://xdebug.org/find-binary.php

Now download the files you need to debug your app.
 
I explicitly added the user to the database when I created them both.....
... and guess what? cPanel does not show the user assigned to that database any more.
Has anyone ever seen this before?

I've had several cases where I know, for a fact, that I've made changes to a site (uploaded a script, added email, created db table, ....) and checked it a week later to see things reverted.

I've never looked too far into it, I just assume that servers are flawed and revert to short term backups randomly.

I know none of this makes sense, servers are just supposed to work. Hell, nobody complains publicly about reverts like this which leads me to believe I'm the odd ball. Glad to see I'm not alone in this phenomenon.

There's no fix, learn to live with it. You're not crazy.
 
This exact connection setting has been used several times before on this host with no problems. I explicitly added the user to the database when I created them both.....
... and guess what? cPanel does not show the user assigned to that database any more.
Has anyone ever seen this before?

I personally have stopped using cPanel for managing my server because I don't like their settings, but that's not what's causing your issue.

Your problem is that you're reusing connections and are assuming that you're using a specific one when inputting data. It's likely that you're trying to add data through a connection that has been overwritten after one of your later calls, and since you aren't setting specific namespaces for your connections, you can't access it.

You should be using a data abstraction layer when connecting to a database. PHP:PDO Introduction - Manual

The reason for this is that it's a consistent architecture for working with a database. You change the database type, no problems, your code all still works and all you have to change is the first call for stating which database type your connecting to (MySQL, MSSQL, etc.). Not only that, but it gives you quite a lot more error control and notifications if something isn't working correctly.

You should also only be updating and inserting elements into a database with commit and rollback calls so that if a specific element in a query set fails, then the rest of the dependent queries don't commit and roll back to a working version prior to you trying to execute them.

Anyway, PHPMyAdmin is nice, but plug the SQL into it yourself after applying PDO and a database connect object and I think you'll find that it works much better and you can get more error control.

MOST PHP SQL calls do not output error messages so unless you dig a little bit you won't know what's wrong.

That's why you use PDO. You know exactly what's mucking up.
 
Did you ever do this and if so what did the error message say?:

mysql_connect('localhost', 'user', 'pass') or
die("Could not connect: " . mysql_error());
mysql_select_db('database');

mysql_query($query) OR die(mysql_error());
 
No db selected? Try:

Code:
$conn = mysql_connect('localhost', 'user', 'pass')
    or die("Could not connect: " . mysql_error());
mysql_select_db('database', $conn);

/* whatever other code you have */

mysql_query($query, $conn) or die(mysql_error());
OK, I've basically covered all of the bases on suggestions added here and it kept coming up with "No database selected" where I have used the following:

mysql_connect('localhost', 'user', 'pass') or
die("Could not connect: " . mysql_error());
mysql_select_db('database');

This exact connection setting has been used several times before on this host with no problems. I explicitly added the user to the database when I created them both.....
... and guess what? cPanel does not show the user assigned to that database any more.
Has anyone ever seen this before? Where somehow the user gets dropped?

Then again, this IS a Wiredtree server.