Jump to content

Connection String problem


ballouta

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/132468-connection-string-problem/
Share on other sites

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


}
}

?>

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.

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

 

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.

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

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"]));

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";

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

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.