View Full Version : Access WAMPSQL database from tomcat page via different port

08-14-2012, 03:30 PM
Hi all, first time post here.

I have setup a wampsql database on my server on port 80 and I can pull data from the database using the PHP area within WAMP (c:\wamp\www\). I successfully accessed the database with the following code:

$con = mysql_connect("localhost","myusername","mypassword");
if (!$con)
**die('Could not connect: ' . mysql_error());

I am developing a web page that requires JavaScript so I have setup tomcat environement using a different port and installed Javabridge to give me PHP capability within tomcat.

My problem is if I reuse the above code it cannot see the swampsql database from my websites on tomcat. Is there a further information I can add to the following line so I can access the wampsql db from tomcat.:

$con = mysql_connect("localhost","myusername","mypassword"); ??

Any help much appreciated


08-14-2012, 03:36 PM
Javascript doesn't require tomcat as its a client language, not a server one. JSP and servlets requires tomcat, but if you aren't using JSP then I wouldn't bother with the expense of the tomcat.
What's the error you receive? If it fails, it will trigger that die. If there is no error, you need to issue a mysql_select_db() function call to choose the db to work with after the connection call.

08-14-2012, 04:52 PM
Thanks for the reply. I think I need jsp as creating a dynamic webpage.

The error I receive is :

"PHP fatal error: call to undefined function mysql_connect()"

08-14-2012, 05:36 PM
PHP is capable of creating dynamic webpages. You don't need JSP for that unless you plan on using Java as your server side language.

You do not have the mysql library installed. Try using the mysqli or pdo library instead:

$con = new MySQLi('host', 'user', 'password', 'database');
if ($con->connect_error)
printf('Connection error: %s', $con->connect_error);

// or
$dsn = 'mysql:dbname=testdb;host=';
$user = 'user';
$password = 'pass';
$con = new PDO($dsn, $user, $password);
catch (PDOException $ex)
printf('Error: %s', $ex->getMessage());

Dormlich is the PDO expert here if you go that route.