DIGIT-ARC read-only archive · snapshot: July 23, 2026

php database connectivty

18 posts · originally from https://geek.digit.in/community/threads/php-database-connectivty.184737/

T
techlearn Original post #1
Jun 19, 2014 at 1:25 AM
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:
<?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
Shah #2
Jun 19, 2014 at 7:04 AM
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.

Code:
<?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 at 8:21 AM
Also try to stick with PDO or if not possible then atleast OOP MySQLi. Procedural MySQL is not recommended at all nowadays.
Hrishi
Hrishi #4
Jun 19, 2014 at 9:35 AM
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.
T
Jul 1, 2014 at 9:14 AM
[MENTION=129731]Shah[/MENTION]
Thanks got it working...
[MENTION=140405]Hrishi[/MENTION] and [MENTION=32490]krishnandu.sarkar[/MENTION]
Now learning MySQLi
Shah
Shah #6
Jul 1, 2014 at 10:04 AM
[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?
T
Jul 1, 2014 at 10:21 AM
Dont know just removed the space between db name and it worked...
T
Jul 7, 2014 at 6:12 PM
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 at 8:44 PM
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)
T
techlearn #11
Jul 7, 2014 at 8:54 PM
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 at 10:30 PM
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

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.
T
techlearn #13
Jul 7, 2014 at 10:42 PM
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.
I will upload my code, btw i am not testing my application locally doing it on live website.
krishnandu.sarkar
Jul 8, 2014 at 8:24 AM
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.
T
techlearn #15
Jul 8, 2014 at 10:37 AM
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.

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 :-x


Here is the code samples
dbconnect.php
Code:
<?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
Code:
<?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 at 8:21 PM
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.
T
techlearn #17
Jul 10, 2014 at 1:54 PM
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 at 10:07 PM
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 :p