Jump to content

DB issues (upgraded from 4.X to 5.2)


kiss-o-matic

Recommended Posts

Okay, some code broke.  I was doing this, basically.

 

inc_db.php

<?php
$link_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('main_db') or die ('Could not select database');
?>

 

inc_other_db.php

<?php
$other_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('other_db') or die ('Could not select database');
?>

 

funcs.php

<?php
function do_something( $a, $b )
{
     $query  = "SELECT id FROM users WHERE a = '$a' AND b = '$b'";
     $result = mysql_query( $query ) or die ( 'Query result failed: ' . mysql_error() );
     // do something with result
}
?>

 

Caveat: One script pulls in inc_other_db.php *after* the inc_db.php so it uses that.

New funcs.php

 

<?php
function do_something( $a, $b )
{
     global $linkdb;
     $query  = "SELECT id FROM users WHERE a = '$a' AND b = '$b'";
     $result = mysql_query( $query, $linkdb ) or die ( 'Query result failed: ' . mysql_error() );
     // do something with result
}
?>

 

Runs, but mysql_query() always fails, and mysql_error() always returns a blank error.

Any ideas?

 

Something big changed in PHP5 that I'm totally missing?  I'm getting jack in the PHP error logs. :(

How can I print out some info about $linkdb in my function?  I checked it with if ( !$linkdb) and isset(), and both seemed fine. 

Totally frustrated.  Sleeping now.

 

Link to comment
https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/
Share on other sites

Nothing in your code is php version specific.

 

However, using mysql_error(...) after a mysql_query() statement requires the same link resource as a paraemter that the mysql_query() statement used so that it will report any errors that occurred on the same connection that the query was executed on.

If your database username/password is the same for both databases and you are simply using two different database names, you don't need to make two different connections (the second one is actually just reusing the first connection.) You can either just call mysql_select_db when you need to switch database names or you can use the database name in your query. Instead of table_name in a query, you would use db_name.table_name

Hey

 

Looks like I was doing some stuff before I included the connection to the database I needed.  Anyway, all sorted.

 

But, both of your suggestions are wise and I should take them into account.  It's a pretty old site, and I was cutting my teeth on PHP.  Lots of code to change. :?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.