ballouta Posted November 12, 2008 Share Posted November 12, 2008 Hi, I have a cart.php page that needs to connect to two different databases. In other php pages, I used to write this: include('../CMS/global.inc.php'); In cart.php i wrote: include('../CMS/global.inc.php'); include('../CMS/operations.inc.php'); some queries returns error, if I add one fo the above include lines (according to the needed database), the error goes and it works properly. It seems I don't understand how include works! What do I do to include these two string connections in the page, without repeating them many times? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/ Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Without code hard to help. I bet you are not using the link_id feature for your mysql_query. The function mysql_connect returns a valid resource ID to use strictly for that connection. I would look into that. Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688722 Share on other sites More sharing options...
ballouta Posted November 12, 2008 Author Share Posted November 12, 2008 I am sorry premiso, i didn't understand what you mean, I know it is my problem, kindly make ur answer easier for me, Here's a sample code from cart.php <?php //Get this book price include('../CMS/global.inc.php'); $result = mysql_query("SELECT * FROM `books` WHERE `ncode` = '$id' AND `show` ='Y' "); $row = mysql_fetch_array($result); $theprice = $row[pricelist]; if ($_SESSION['item']) {//the user is trying to add an item to his cart //echo "It is Entering this scope "; //check if the user has a pending order or not include('../CMS/operations.inc.php'); $result = mysql_query("SELECT * FROM `ordersmaster` WHERE `member` = '$user' AND `status` ='P' "); $row = mysql_fetch_array($result); if (mysql_num_rows($result) > 0 ) { // and some code lines go here } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688728 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 You stated you need to connect to 2 different databases. Right? In order to do this, where the mysql_connect statement is needs to modified to assign the resource for the connection to an ID. However, if you are saying databases as tables, as I can see one table is books and the other is ordermaster, than my reply is null and void. Let me know which we are dealing with and I will gladly try to provide some more insight. Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688731 Share on other sites More sharing options...
ballouta Posted November 12, 2008 Author Share Posted November 12, 2008 yes i am dealing with two databases. the books table exists in store database where the ordersmasters exists in operations database. As for the connection string in store database, it is: <?php $dbh=mysql_connect ("localhost", "dcompany_XXXXX","XXXXXX") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("dcompany_store"); ?> The second one is the same of course with different database name, and password. thank you Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688735 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 So $dbh houses your connection string. Make sure that is not the same for both databases. <?php $dbh=mysql_connect ("localhost", "dcompany_XXXXX","XXXXXX") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("dcompany_store", $dbh); // this part is just an example of what I am talking about. $dbh2=mysql_connect ("localhost", "dcompany_XXXXX","XXXXXX") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("dcompany_orders", $dbh2); ?> <?php //Get this book price include('../CMS/global.inc.php'); $result = mysql_query("SELECT * FROM `books` WHERE `ncode` = '$id' AND `show` ='Y' ", $dbh); $row = mysql_fetch_array($result); $theprice = $row[pricelist]; if ($_SESSION['item']) {//the user is trying to add an item to his cart //echo "It is Entering this scope "; //check if the user has a pending order or not include('../CMS/operations.inc.php'); $result = mysql_query("SELECT * FROM `ordersmaster` WHERE `member` = '$user' AND `status` ='P' ", $dbh2); // this assumes you assigned the other reference to $dbh2 $row = mysql_fetch_array($result); if (mysql_num_rows($result) > 0 ) { // and some code lines go here } } ?> I hope that helps you out. Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688740 Share on other sites More sharing options...
ballouta Posted November 12, 2008 Author Share Posted November 12, 2008 Thanks I will modify my code and test it, May i ask very short questions please: 1) How I round such number or keep only two digits after the point: 1.72842413549 2) I want to apply the lowercase function on the following line without removing trim function: $_SESSION['authen']=trim($_POST["user"]); Thank you Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688752 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Thanks I will modify my code and test it, May i ask very short questions please: 1) How I round such number or keep only two digits after the point: 1.72842413549 2) I want to apply the lowercase function on the following line without removing trim function: $_SESSION['authen']=trim($_POST["user"]); Thank you number_format $_SESSION['authen'] = strtolower(trim($_POST["user"])); Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688759 Share on other sites More sharing options...
Barand Posted November 12, 2008 Share Posted November 12, 2008 If both databases are on the same same server you need only establish a single connection and select your default (most used) database with mysql_select_db(). Say you have mysql_select_db ('store'); When you want to reference a table from the operations db, just qualify the table name with the db name as a prefix $sql = "SELECT * FROM operations.ordermaster"; You can even do joins across dbs. E.G. $sql = "SELECT b.*, o.something FROM books b LEFT JOIN operations.ordermaster o ON b.foo = o.foo"; Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688857 Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 You can also call mysql_connect() to get the connection to the server, then simply call mysql_select_db() as many times as needed to switch back and forth between the two databases. Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-688902 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 Hi Barand you code is nice, but i still have problems: This is my code: <?php include('../CMS/operations.inc.php'); include('../CMS/global.inc.php'); mysql_query("SET CHARACTER_SET_RESULTS=NULL"); //Get this book price mysql_select_db ('store'); $result = mysql_query("SELECT * FROM `store`.`books` WHERE `ncode` = '$id' AND `show` ='Y' "); $row = mysql_fetch_array($result); $theprice = $row[pricelist]; ?> the error is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/dcompany/public_html/shopping/cart.php on line 93 and there are similar problems. thank you Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-689159 Share on other sites More sharing options...
nitation Posted November 13, 2008 Share Posted November 13, 2008 what is on line 93 Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-689161 Share on other sites More sharing options...
Barand Posted November 13, 2008 Share Posted November 13, 2008 checking the value of mysql_error() after the query is usually a good start. Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-689172 Share on other sites More sharing options...
ballouta Posted November 13, 2008 Author Share Posted November 13, 2008 this is line 93 $row = mysql_fetch_array($result); that comes after: $result = mysql_query("SELECT * FROM `store`.`books` WHERE `ncode` = '$id' AND `show` ='Y' "); Quote Link to comment https://forums.phpfreaks.com/topic/132468-connection-string-problem/#findComment-689183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.