Why can I not connect to my localhost database server? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I can access localhost/phpmyadmin/ through my browser and create tables, insert tuples, etc but I can't connect to it in PHP code. Here's what I got:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conn = new mysqli('localhost', 'root', '');
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success';
$mysqli->close();
?>
and i have an HTML form somewhere else with an action attribute that runs the above code. But whenever I test my code, it returns an Error 500. I won't tell me anything more specific, even though I included error reporting! Why?

You have error in creating connection:
mysqli is not a function, you need to use mysqli_connect
$conn = new mysqli_connect('localhost', 'root', ''); // here is change
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success'
$conn->close(); // close connection using connection resource
?>

Related

PHP connecting to database fails without error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected

Connected to mysql database but cannot print the data [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$db=mysqli_connect("host","root","pass","dbase");
if (!$db) {
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 "Connected successfully";
$sql = "SELECT username FROM students";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
$db->close();
?>
So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server.
The setsebool -P httpd_can_network_connect=1 in the terminal did the trick.
Edit: Note to future readers. The OP edited their question with the corrected syntax after answers were posted and this answer was based on their original post:
https://stackoverflow.com/revisions/36189996/2
and added:
"So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick."
Firstly, you didn't select a database.
and if (!db) needs to be changed to if (!conn) and if ($db->connect_error) to if ($conn->connect_error). The same variable needs to be used throughout your code.
You're also using the wrong variable $db it's $conn (or whatever you want to use, but as I said above, you need to use the same variable for your connection and for the query.
Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about an undefined variable.
Therefore:
$conn = #mysqli_connect("host","root","pass", "your_database");
Sidenote: host I take it is only representative of the host you're using. If you're on your own PC, you would use localhost and accessing that file as http://localhost/file.php instead of file///file.php if that is what you're doing and will not work. The web browser will not parse PHP directives like that and you need to install a webserver/PHP in order for it to work, including MySQL.
Read the manual on connecting:
http://php.net/manual/en/function.mysqli-connect.php
and remove the # symbols while testing. They are error suppressors.
Check for errors with http://php.net/manual/en/mysqli.error.php
Example from the manual:
Sidenote: 127.0.0.1 or localhost or if hosted, use the setting they gave you to use.
<?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);
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
You use another variable than you connected to the database!
You connected to $db but perform the query with $conn.
Also, in the check if the database connection could be established you do not have the $ before db.
It always helps a lot when you have in a terminal window the PHP error log open. 99% of errors can be fixed by your self.

how to connect to a mysql database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I connect to a MySQL database in PHP?
Database name: NewDB1
Username : Mark
Password : secret
I have googled a few solutions, but none of them seem to work.
<?php
$servername = "localhost";
$username = "Mark";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
With PDO -
<?php
// turn on error reporting and display
error_reporting(E_ALL);
ini_set('display_errors', 1);
// define the user
define('USER', 'Mark');
define('PASS', 'secret');
// establish database connection
try {
$dbh = new PDO('mysql:host=localhost;dbname=NewDB1', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // make sure to turn on error reporting
}
catch(PDOException $e) {
echo $e->getMessage(); // show any error messages.
$errorCode = $e->getCode();
}
?>
This comes from a more detailed tutorial on how to understand and simplify PDO.

mysql php update with a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I've been at this for a while now and anything I do or research turns up more errors. I keep getting this error: Fatal error: Call to a member function query() on a non-object with code that looks like this. the pic column is for a profile picture directory they uploaded.
$newTarget_file = "uploads/picture.png"
....
$sql = "UPDATE users SET pic='$newTarget_file' WHERE username=:username";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
also this is a php that is submited to that saves the picture they upload and the directory is the $newTarget_file . This is realy frustrating my on how I do this. Thanks in advance for the help.
Basically I want to update the pic column where username=:username with a variable I have already $newTarget_file . what do I do?
As others have said, the error is because your connection object doesn't exist.
Fatal error: Call to a member function query() on a non-object
Based on the tutorial you are following, have you implemented this?
$conn = new mysqli($servername, $dbuser, $dbpw, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
You should have gotten a connection error before the query ever tried to run. Also, as Michael pointed out, you can't used named parameters with mysqli. It's only supported in PDO, but you can still use placeholders. Your new code should look something like this.
if (!($stmt = $conn->prepare("UPDATE users SET pic=? WHERE username=?"))) {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
exit;
}
if (!$stmt->bind_param("ss", $newTarget_file, $username)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
exit;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
exit;
}
$stmt->close();

It is no any erro message showing when using PHP to Connect Mysql Database , [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
It is nothing to show!!! No any error message
It seems to just print out the word and not trying to connect mysql
I am using Netbean to build the project Is it have any problem?? Thankyou
Here is mycode:
<?php
$link = mysql_connect("localhost", "root", "")
or die("No");
print ("Connected");
mysql_close($link);
?>
Please check below :
<?php
$link = mysqli_connect("localhost", "root", "");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Note: Try to use mysqli instead of mysql, as it is deprecated now.
try this
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Categories