Cannot connect to local database using php - php

I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.

Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j

Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop

Related

PHP - If no MySQL Connection then display simple message

How can I catch and ignore errors when connecting to a database and display a simple text message instead?
My connection looks like this:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
If the connection fails I want it simply to echo "Error connecting to the database" instead of the big error box.
EDIT:
Although error_reporting(0) did work, what worked better was just putting '#' in front of mysqli_connect:
$connection = #mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection) {
$connection_status = 'Connection to database failed';
} else {
$connection_status = null;
}
?>
<?php echo $connection_status; ?>
Taken from the documentation of mysqli_connect:
Examples
Example #1 mysqli_connect() example
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
mysqli_connect:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection)
{
error_reporting(0);
die("Error: Unable to connect to MySQL." . PHP_EOL);
}
To disable "big error boxes" (I assume standart messages?), write error_reporting(0); above this.
You may set your custom handler for errors
register_shutdown_function("shutdownHandler");
function shutdownHandler()
{
if (!is_null($e = error_get_last())).
{
echo $e['message'];
}
}

Php MySQL Connecting a database via MySQL does not work but ODBC does

I'm trying to connect to a database via MySQL but it does not work. I'm getting the following error:
"Connection failed: No connection could be made because the target machine actively refused it. "
Here's the basic code:
<?php
$servername = "server";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I can connect to the database from Excel and also via an ODBC connection with the code below but not via MySQL:
<?php
$user = 'username';
$pass = 'password';
$server = 'server';
$database = 'database';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server; DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if (!$conn){
exit("Connection to the database Failed: " . $conn);
}
echo "Connected successfully";
?>
Any ideas?
You say you can connect with odbc which means you have an SQL Server database, not a Mysql database. If you don't like odbc, pdo is your other option. Mysqli is not an option as its mysql only.
Try this code:
<?php
$link = mysqli_connect('server','username','password','database');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo("success");
?>

Warning: mysql_connect(): Error while reading greeting packet

<?php``
if(mysql_connect("localhost","root",""))
echo"connect";
else
echo "not connect";
?>
this is my code but it's not connected . give the error as warning
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2
Warning: mysql_connect(): Error while reading greeting packet. PID=2296 in C:..\pro1.php on line 2
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2
not connect
You can try using either MySQLi or PDO and their prepared statement to be more secure.
If you intend using just MySQL then use the below code to initialize your Database connection.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
For reference see http://php.net/manual/en/function.mysql-connect.php
Alternatively kindly use MySQLi
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
For reference see http://php.net/manual/en/mysqli.construct.php
If you consider using PDO then try
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
For reference see http://php.net/manual/en/pdo.connections.php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
http://php.net/manual/en/mysqli.construct.php
remove '' after php open tag and
try like this
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('dbname'); //your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

Cant see data after importing in _post form

Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli

Connecting to mysql from php fails silently

I'm trying to connect to a server running on linux from a php file using the following file:
<?php
$db_host = "127.0.0.1";
$db_user = "userk";
$db_pss = "pass";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: "
. $db_host."\n". $db_user . " " .$db_pss;
$link = mysql_connect($db_host, $db_user,$db_pss);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
the output page is:
register Trying to connect with credentials: 127.0.0.1 userk pass
I've checked previous questions looking for useful information. But it still doesn't work.
I'm able to connect to mysql with this user from terminal using putty. I don't know why I don't receive any messages after the connection attempt. Should I check the firewall configuration on the server, how can I check that?
Thanks!
As we know mysql_* is depreciated now, so better use it using MYSQLI_*:
<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT );
$db_host = "localhost";
$db_user = "userk";
$db_pss = "pass";
$database="abc";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: " . $db_host."\n". $db_user . " " .$db_pss" " .$database;
$link = mysqli_connect($db_host, $db_user,$db_pss,$database);
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';
mysqli_close($link);
?>
It can be a case that your errors are suppressed. So its not showing an error. try it out. It will give the error now. That will help figuring out real problem.:)
<?php
$db_host = "localhost";
$db_user = "userk";
$db_pss = "pass";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: " . $db_host."\n". $db_user . " " .$db_pss;
$link = mysql_connect($db_host, $db_user,$db_pss);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
try this :)

Categories