• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

php database connectivty

techlearn

Broken In
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

Cyborg Agent
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

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

Hrishi

******************
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.
 
OP
T

techlearn

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

techlearn

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

krishnandu.sarkar

Simply a DIGITian
Staff member
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)
 
OP
T

techlearn

Broken In
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

Simply a DIGITian
Staff member
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.
 
OP
T

techlearn

Broken In
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

Simply a DIGITian
Staff member
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.
 
OP
T

techlearn

Broken In
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

Simply a DIGITian
Staff member
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.
 
OP
T

techlearn

Broken In
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

Simply a DIGITian
Staff member
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
 
Top Bottom