-3

I followed many tutorials to set up a registration form on my site. Now I added file upload on a extra site and I'm not sure how to combine the MySQL connections. I want to make a PDO Solution.

database.php (MySQL Connection #1) for register form.

<?php
$server = 'localhost';
$username = 'xxx';
$password = '';
$database = 'auth';

try{
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
    die( "Connection failed: " . $e->getMessage());
}

dbconfig.php (MySQL Connection #1) for file upload.

<?php
$dbhost = "localhost";
$dbuser = "xxx";
$dbpass = "";
$dbname = "dbtuts";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server'); 
mysql_select_db($dbname) or die('database selection problem');
?>

I'm working with the Online IDE www.c9.io and using Bootstrap.

Winter
  • 3,894
  • 7
  • 24
  • 56
  • What **precisely** do you mean by _combine 2 connections_ – RiggsFolly Dec 20 '16 at 14:17
  • If you want to use PDO for both connections you will have to rewrite all the `mysql_` extension functions into PDO code. Its not just a case of changing the connection method – RiggsFolly Dec 20 '16 at 14:20
  • there is a "connection to sql"-file from the first tutorial "making a registration form" and a second "making a file upload"". sry my english is not that good i hope you understand me. :( – sinankarateke Dec 20 '16 at 14:23
  • be warned that mysql_ functions are deprecated an may not work in PHP 7. You should update them at least to mysqli_ (with the i on the end) functions. Anyway the mysql_connect functions returns you a resource which you can keep into memory and use as parameter calling other mysql_ functions – Frank B Dec 20 '16 at 14:23
  • *"i want to make a PDO Solution"* - So why the `mysql_*`? and why 2 connections? – Funk Forty Niner Dec 20 '16 at 14:24
  • Fred -ii- this is a code from 1 tutorial i just copy & paste it. – sinankarateke Dec 20 '16 at 14:26
  • @sinankarateke what's the end goal? – JustBaron Dec 20 '16 at 14:27
  • RiggsFolly this is the question how should the pdo code look like for 'dbconfig.php'? – sinankarateke Dec 20 '16 at 14:29
  • 1
    I don't see the reason of using 2 separate connections; just use the same one and change the database when querying. – Funk Forty Niner Dec 20 '16 at 14:29
  • i didnt expected that the answers flow so quick. so, puh. i'll now delete 'dbconfig.php' the one with "mysql_connect" and correct the code who are related to 'dbconfig.php'. – sinankarateke Dec 20 '16 at 14:40
  • edit: and also drop the 'dbtuts' database and move the table in the first database 'auth'. – sinankarateke Dec 20 '16 at 14:41
  • _how should the pdo code look like for 'dbconfig.php_ Exactly the same as the PDO one. Infact you could `include 'database.php';` in the second script. **But all the other mysql_ code will need rewriting** You cannot connect with PDO and execute `mysql_` functions – RiggsFolly Dec 20 '16 at 14:48
  • 1
    @RiggsFolly i found a thread who is answering me how to do the mysql_code rewriting (http://stackoverflow.com/questions/12310238/php-changing-old-mysql-query-to-pdo) and i also think this was my question ._. – sinankarateke Dec 20 '16 at 15:06
  • @RiggsFolly i found your last answer useful but i also appreciate all you guys how helped me to figure it out and giving "food for taught". i try to translate from german to english so good i can right now ^^ – sinankarateke Dec 20 '16 at 15:19

1 Answers1

2

You don't need it. Just create a single connection to a single database and then use it all the way through. Especially if it's your first attempt to work with PHP.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Surely this qualifies as a comment not an answer. Replacing the connection code will only make the `mysql_` function calls later in the OP's other script fail. At least a small mention of this would be useful! – RiggsFolly Dec 20 '16 at 14:49