# php database connectivty



## techlearn (Jun 19, 2014)

Hey,
I am a newbie to php programming i need help with database connectivity, i need to use a single file which will contain database connection in all other pages. Is there any example ?

What i am trying to do is

```
<?php
	$sql_host = 'localhost';
	$sql_user = 'root';
	$sql_pass = '';
	$err_msg = "Error occured";
    $sql_db = 'alumni portal';
	
	if(!@mysql_connect($sql_host,$sql_user,$sql_pass) || !@mysql_select_db($sql_db)){
		echo $err_msg;
	}
?>
```

with this I can establish the connection.

Now I try to use this in other php page by using "include" command.
It is not working can anybody tell me what am I doing wrong?

PS: how to close the connection in new file or I don't need to handle it.


----------



## Shah (Jun 19, 2014)

AFAIK, database name can't  contain spaces. I might not be correct, though.

Try my code and see if it helps. BTW, Call *dbconnect()* and *closedb()* functions whenever needed.


```
<?php
function dbconnect() {
$cn=mysqli_connect("localhost","root","","alumni_portal");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

function closedb() {
//To close db
mysqli_close($cn);

}

?>
```


----------



## krishnandu.sarkar (Jun 19, 2014)

Also try to stick with PDO or if not possible then atleast OOP MySQLi. Procedural MySQL is not recommended at all nowadays.


----------



## Hrishi (Jun 19, 2014)

Use Php data objects instead. In long run that will be helpful and more secure. 
When i used to code , i personally preferred PDO.
Lime krish said above. I would not recommend something legatic like that. Its very much unsafe these days.


----------



## techlearn (Jul 1, 2014)

[MENTION=129731]Shah[/MENTION]
Thanks got it working...
 [MENTION=140405]Hrishi[/MENTION] and [MENTION=32490]krishnandu.sarkar[/MENTION]
Now learning MySQLi


----------



## Shah (Jul 1, 2014)

techlearn said:


> [MENTION=129731]Shah[/MENTION]
> Thanks got it working...
> [MENTION=140405]Hrishi[/MENTION] and [MENTION=32490]krishnandu.sarkar[/MENTION]
> Now learning MySQLi



What was wrong with your code, then?


----------



## techlearn (Jul 1, 2014)

Dont know just removed the space between db name and it worked...


----------



## Shah (Jul 1, 2014)

techlearn said:


> Dont know just removed the space between db name and it worked...



I told so.


----------



## techlearn (Jul 7, 2014)

Hi,
I am getting error "Unable to ConnectToo many connections" while calling my php page, can you please guide me through this.


----------



## krishnandu.sarkar (Jul 7, 2014)

techlearn said:


> Hi,
> I am getting error "Unable to ConnectToo many connections" while calling my php page, can you please guide me through this.



Well are you even closing the connections which you are doing? Are you trying to connect your database everytime a page loads and leaving it opened behind?

Check your code. Else you can increase the number of connections from Settings File / PHPMyAdmin. (Though this is not recommended. Better check your code)


----------



## techlearn (Jul 7, 2014)

krishnandu.sarkar said:


> Well are you even closing the connections which you are doing? Are you trying to connect your database everytime a page loads and leaving it opened behind?
> 
> Check your code. Else you can increase the number of connections from Settings File / PHPMyAdmin. (Though this is not recommended. Better check your code)


Yes, a connection is always opened when a new page is called and even i close it with mysql_close(). Is there anything i am missing..?

PS : as suggested i am currently not using mysqli or pdo.


Regards,
Techlearn


----------



## krishnandu.sarkar (Jul 7, 2014)

techlearn said:


> Yes, a connection is always opened when a new page is called and even i close it with mysql_close(). Is there anything i am missing..?
> 
> PS : as suggested i am currently not using mysqli or pdo.
> 
> ...



Can I take a look at your code? I think something is wrong with your code.

Also, generally MySQL Default settings have 100 concurrent connections. But I'm sure you are doing your testing in local and you do not have that much traffic. So how even you are reaching that 100? If possible please paste your code here within CODE blocks.

BTW, as I already said above you can increase the connection limit to 300 or 400 but that's not recommended as I guess you are testing in local and something must be wrong...

Quickfix, restart your MySQL Server / Reboot your PC and it'll fix the issue for now.


----------



## techlearn (Jul 7, 2014)

krishnandu.sarkar said:


> Can I take a look at your code? I think something is wrong with your code.
> 
> Also, generally MySQL Default settings have 100 concurrent connections. But I'm sure you are doing your testing in local and you do not have that much traffic. So how even you are reaching that 100? If possible please paste your code here within CODE blocks.
> 
> ...


I will upload my code, btw i am not testing my application locally doing it on live website.


----------



## krishnandu.sarkar (Jul 8, 2014)

techlearn said:


> I will upload my code, btw i am not testing my application locally doing it on live website.



Are you in shared hosting? Which host you are using?

Generally if you are using some reputed hosting provider then there's something wrong in your code, as there are admins who are managing these and they configure it very well. But if you are in some free hosting then I doubt.

Anyway in both the cases it seems that there's something wrong with your code.

Once you upload the code I can verify.


----------



## techlearn (Jul 8, 2014)

krishnandu.sarkar said:


> Are you in shared hosting? Which host you are using?
> 
> Generally if you are using some reputed hosting provider then there's something wrong in your code, as there are admins who are managing these and they configure it very well. But if you are in some free hosting then I doubt.
> 
> ...



I am on a shared host and i just know cpanel details nothing more than that, the other person is not even available over the phone yet 


Here is the code samples
*dbconnect.php*

```
<?php
// Create connection
$hostname='localhost';
$username='best';
$password='tl123';
$database='android';
//connecting localhost
$con=mysql_connect($hostname,$username,$password) or die("Unable to Connect" .mysql_error());
//connecting database
$selected = mysql_select_db($database,$con) or die("Could not select database" .mysql_error());
?>
```

*party_list.php*

```
<?php
require ('dbconnect.php');

if($_SERVER['REQUEST_METHOD'] == "POST"){

	$response = array("status" => -1, "name" => "Error Occured");

	// Get data
	$sales_personid = isset($_POST['sales_personid']) ? mysql_real_escape_string($_POST['sales_personid']) : "ID_NOT_FOUND";

	if($sales_personid=="ID_NOT_FOUND"){
			$response = array("status" => -1, "name" => "Please select sales person");
	}

	// Insert data into data base
	$sql = "SELECT * FROM `tparty` WHERE fuserloginid = " .$sales_personid;
	$result = mysql_query($sql,$con);

	if($result){
		if(mysql_num_rows($result)>0){
			$json = array();
			while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
				$json[]=array("status" => 1, "name" => $row['fpartyname']);
			}
		} else {
			$json = array("status" => 0, "name" => "No record found");
		}
	}else{
		$json = array("status" => -1, "name" => "Error in searching - " .mysql_error());
	}
}else{
	$json = array("status" => -1, "name" => "Request method not accepted");
}
@mysql_close($con);

/*Output header */
	header('Content-type: application/json');
	echo json_encode($json);

?>
```

I cannot post all the pages here, but i have done something similar to this on all pages, dont know if i have done something wrong.


----------



## krishnandu.sarkar (Jul 8, 2014)

Well, your code looks fine. Though you may try using echo after mysql_close() to be sure. Though I'm 100% sure it's fine. But still you can check.

BTW which web hosting you are using? And how many users are connecting? Try to talk to them via live chat and ask them to increase number of MySQL connections.


----------



## techlearn (Jul 10, 2014)

krishnandu.sarkar said:


> Well, your code looks fine. Though you may try using echo after mysql_close() to be sure. Though I'm 100% sure it's fine. But still you can check.
> 
> BTW which web hosting you are using? And how many users are connecting? Try to talk to them via live chat and ask them to increase number of MySQL connections.



thanks, actually there was some open connections in other pages, got that sort out and talked with the hosting provider,  if it occurs next time will ask him to upgrade the plan that will work i suppose.


----------



## krishnandu.sarkar (Jul 10, 2014)

techlearn said:


> thanks, actually there was some open connections in other pages, got that sort out and talked with the hosting provider,  if it occurs next time will ask him to upgrade the plan that will work i suppose.



Yeah. Generally in professional world they configure it quite well so that problems do not occur. They have professionals for that. So it's generally problem in developer's side


----------

