...

View Full Version : get variable from a seperate database



twobyfour
06-26-2010, 07:56 AM
I need to retrieve a unique id from a seperate database based on there username and password and insert it in this table:


//Make variable for data to be inputed
$foo_name = $_POST[foo_name'];
$email_contact = $_POST['email_contact'];
$category = $_POST['category'];
$foo_site = $_POST['fo_site'];
$description = $_POST['description'];
$foo_url = $_POST['foo_url'];
$license = $_POST['license'];
$price = $_POST['price'];
$dated = date("d-M-Y");

//make the connection to the database
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());

//Then you choose the table to save info on

$sql = "INSERT INTO user_foo (foo_name, email_contact, category, foo_site, description, foo_url, license, price, dated) VALUES ('$foo_name', '$email_contact', '$category', '$foo_site', '$description', '$foo_url', '$license', '$price', CURDATE())";

How can i modify this query for the other database.

Keleth
06-26-2010, 06:02 PM
First, if thats a direct copy/paste, add the first apostrophe in the first var so it actually opens properly.

Second, just create a second connection, assigned to a different variable, then use that variable in your mysql_query.

http://php.net/manual/en/function.mysql-query.php

The var im referring to is the second one, the link_identifier.

twobyfour
06-26-2010, 06:41 PM
$foo_name = $_POST['foo_name'];
That was a mistake do to copy and paste and highlighting over the correct variable and writing in foo in its place. Its correct on the actual code.

Add a second conn like this


$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
$db1 = @mysql_select_db($db_name,$connection)or die(mysql_error());
$result = @mysql_query("SELECT username, memberid, FROM authorize", $db1);

Instead of username and password, i am just using there username. I basically want to match up there post which as a unique id to there assigned memberid in another database.


Using this code, it works, but in the memberid column, it puts in 0 and not the memberid from the other database

//make the connection to the database
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
$db1 = @mysql_select_db($db_name,$connection)or die(mysql_error());
$result = @mysql_query("SELECT username, memberid, FROM authorize", $db1);


//Then you choose the table to save info on

$sql = "INSERT INTO user_foo (foo_name, email_contact, category, foo_site, description,

foo_url, license, price, memberid, dated) VALUES ('$foo_name', '$email_contact', '$category',

'$foo_site', '$description', '$foo_url', '$license', '$price', '$memberid', CURDATE())";

twobyfour
06-26-2010, 08:23 PM
i am getting this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''923249916', dated) VALUES ('xxxxxxxxxxx', 'xxxxxxxxxx', '1', 'xxxxxxxxx', 'xxxx' at line 1

using this code


$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
$db1 = @mysql_select_db($db_name,$connection)or die(mysql_error());
$result = @mysql_query("SELECT username, memberid FROM authorize WHERE username = '".$_SESSION['username']."' AND memberid = '".$_SESSION['memberid']."'", $db1);


//Then you choose the table to save info on

$sql = "INSERT INTO user_foo (foo_name, email_contact, category, foo_site, description, foo_url, license, price, '".$_SESSION['memberid']."', dated) VALUES('$foo_name', '$email_contact', '$category', '$foo_site', '$description', '$foo_url', '$license', '$price', '$memberid', CURDATE())";

I need the memberid (923249916) to be inserted into '$memberid'

twobyfour
06-26-2010, 08:58 PM
i fixed it, it works now

added this line after the second conn query

$memberid = $_SESSION['memberid'];

//Then you choose the table to save info on


$sql = "INSERT INTO user_foo (foo_name, email_contact, category, download_site, description, foo_url, license, price, memberid, dated) VALUES ('$foo_name', '$email_contact', '$category', '$foo_site', '$description', '$site_url', '$license', '$price', '$memberid', CURDATE())";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum