frequent lost of connection to mySQL server - php

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?

Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

Related

Can't connect my database on my live server

Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');

Webmatrix showing blank page

I am new to php! And i have to create a site. So i wrote the code in HTML, downloaded webmatrix, created a MySql database and inserted the code for DB connection in the php file. But when i run the php page, it shows a blank page. It works fine if I remove the code for database connection.
Here is my connection code:
<?php
$conn=mysqli_connect(localhost;rjitdbUy5k1;******;rjitdb);
if(!$conn)
{
die('Could not connect:'.mysql_error());
}
echo
'Connected successfully';mysql_close($conn);
?>
The Request log displays an error:
Got the answer!
I missed out the
mysql_select_db(DBNAME)
string.

Accessing SQL Server 2012 from php under Linux

I'm in troubles...
I have a web server apache/php under linux, also i have another server with SQL Server database under windows. I need to get access to SQL Server database from my apache/php.
What do i need, please help. I have no idea how to start.
**sorry for my english.
Could do with a bit more clarification but I'll try my best..
If you mean through the code, you would do it like this:
Create a new php file called "connect.php" and insert all of this code, replacing the tags like USERNAME with the correct details.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Create connection
$con = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'DATABASE');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>
Next, EVERY file that requires a connection to the database, simply add
include("connect.php"):
To it at the start of the page and it should be fine :)
I seriously recommend you read up a bit more though as you seem like a bit of a newb (we all started somewhere)!
Hope this is what you were after!
Look for dblib, you will need to add that driver.

Can't connect to database "Fatal error: Call to undefind function mysqli_connect()"

I've been scouring this site for 5 hours now trying to get this sorted, I rarely ask for help but this is one of the weirdest and most annoying things I've encountered.
First of all I'd like to say that this DID work fine, I have limited examples of what the cause is but I'll list them anyway.
Here's the full error message:
Fatal error: Call to undefind function mysqli_connect() in C:\wamp\www\game\connect.php on line 3
And here's the code
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Weird thing is, it was working completely fine and just suddenly stopped, this has happened more than once.
Here's what I've tried:
Checking the extensions are enabled -
Rebooting various times -
Setting the correct php path -
Using many example codes that "work" -
I also had some code that inputted data straight from phpdesigner into the database, which successfully worked, but that no longer works and I've made literally 0 changes.
The last time it stopped working, I filled out a registration form on my site as a test (that doesn't work either) and it suddenly went off. When filling in the form I click register and nothing happens besides a refresh.
Bit extra: In my httpd file the pfp and pfpinidir are as follows
php5_module"c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
PHPIniDir "C:\wamp\bin\apache\apache2.4.9\bin\php.ini"
Your mysqli extension might not be enabled. so u need to enable that.
You have two PHP binaries on your machine. One is connected to the Apache, and is the one u use when you surf to a web page.
The phpinfo() shows you which php.ini file is used by the web php binary. u need to edit this file and in it activate the mysqli extension
You're trying to connect to DB twice, plus you're mixing MySQL APIs with mysql_error(), they do not mix together.
The 4th parameter is the DB name which is what you've done in the first example.
Use either:
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
or omit ,"game" from mysqli_connect() - while mysqli_error() requires DB connection parameter.
<?php
// Create connection
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>

PHP MySQL external database connection timed out

I'm getting a connection timed out when I try to connect to an external (not on the same server as this code is on) database through the following code. I know the settings of the database is set up right, and the login info is good, 'cause I can login fine from my computer with HeidiSQL. If anyone can see a problem with this code, thanks.
function database_connect(){
$link = #mysql_connect("xx.xxx.xxx.xx:3306","root","pass");
$sql_error = mysql_error();
if (!$link) {
echo "Connection with the database couldn't be made.<br>";
echo "$sql_error";
exit;
}
if (!#mysql_select_db("databasename")) {
echo "The database couldn't be selected.";
exit;
}
return $link;
}
database_connect();
have you tried adding the IP address of where that script is hosted to "Remote Database Access Hosts"?
If you're on cPanel, you need to allow the IP address who can remotely access the datase(mysql).
If you're not, then this is just an idea of what to do.
Problem known now.
My webhost does not allow connections to an external database.

Categories