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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. :?

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.