php Insert Query - Why Are There Two Connections to a Database? -
in script link page connects database :
include "connect.php";
connect.php
<?php error_reporting(e_error); /* allows php connect database */ // database variables $host = "myhost"; $user = "username"; $password = "password"; $dbname = "database"; // connect database $connect = mysql_connect($host, $user, $password) or die ("could not connect server ... \n" . mysql_error ()); mysql_select_db($dbname) or die ("could not connect database ... \n" . mysql_error ()); ?
then in script have insert query:
include "connect.php"; $link = mysql_connect($host, $user, $password); $query = "insert mytable values ('0','".mysql_escape_string($forename)."','".mysql_escape_string($surname)."', '".mysql_escape_string($username)."', '".mysql_escape_string($password)."', '".mysql_escape_string($email)."')"; if(mysql_db_query ($dbname, $query, $link)) { $message = "you have registered"; header("location: register.php?message=".urlencode($message)); } else { die("query was: $query. error: ".mysql_error($link)); } } }
why necessary :
$link = mysql_connect($host, $user, $password);
hasn't connection been established?
there no point in doing this, mysql_*
functions assume last opened connection if none given.
however, 2 calls mysql_connect
, only 1 connection made. docs:
if second call made
mysql_connect()
same arguments, no new link established, instead, link identifier of opened link returned.new_link
parameter modifies behavior , makesmysql_connect()
open new link, ifmysql_connect()
called before same parameters.
so default, existing connection returned.
Comments
Post a Comment