I'm php begginer and i'm trying to connect database using XAMPP.
When i open my file there is not any error about connecting to database, but also there isn't "Connected successfully"; text, there is just blank page.
This is my code:
<html>
<head>
<title></title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "lala2";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?>
</body>
</html>
You used a mix between mysqli and mysql ! Here is the exact method name you should use to connect to a database, and its connection status checks :
MYSQLI style :
/* database connection information */
$server = "";
$user = "";
$password = "";
$database = "";
/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";
$link = new mysqli($server, $user, $password, $database);
/* If connection failed */
if (!$link) {
printf($messErr_connectionDatabaseFailed);
printf("<br />");
}
/* If connection successed */
else {
/* everything is ok, go to next part of you algorithm */
}
MYSQL style (depreciated due to performance and security issues) :
/* database connection information */
$server = "";
$user = "";
$password = "";
$database = "";
/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";
$link = mysql_connect($server, $user, $password);
/* if connection failed */
if (!$link) {
printf($messErr_connectionDatabaseFailed);
printf("<br />");
}
else {
/* selecting the database */
mysql_select_db($database, $link);
/* guessing your select db doesn't failed, next part of you algorithm here */
}
Please use the PDO functions to connect to the database instead. It will be easier to scale your app if you do decide to use different database drivers. Also "binding parameters" is considerably easier using PDO. For a quick intro on PDO, please read http://php.net/manual/en/intro.pdo.php
As far as why you are getting a blank page, there could be many reasons for that. You want to look at your log files first. On Windows, check the Event Logs. On Linux, your logs will be on /var/log. Add the following lines at the beginning of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
Please also look at this thread from StackOverflow: PHP produces a completely white page, no errors, logs, or headers.
Related
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
For Instance consider this standard database connection php file.
db_conn.php
<?php
$servername = "localhost";
$username = "root";
$password = "Yash123";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
If connected successfully it is shows that result onto the website.
What I'd need it to do is not show up on the html but be there in the file so I can test the file executing in the CLI(Command Line Interface) of PHP.
I am using require_once(); in the index file.
uncomment the echo line. or use php's error_log('Connected Successfully');. this would log that the connection was successful. This would hide output from your html and log the string passed as parameter to your error_log file
One could easily dispense with the echo statement and instead return the connection object upon success. This would entail revising the OP code so that it becomes the contents of a database connect function. This idea I gleaned from binpress.com and include suggestions from the Manual, too:
<?php
/* Assuming that user name,password and database name are
credentials that come from a file outside of the
document root for security. */
function db_connect() {
static $conn;
$servername = "localhost";
if ( !isset( $conn ) ) {
// load and parse file containing credentials:
$config = parse_ini_file('../config.ini');
// Create connection
$conn = new mysqli( $servername, $config['username'],$config['password'],
$config['dbname']);
// Check connection
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') '
. $conn->connect_error);
}
// Connected successfully
return $conn;
}
Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result
I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){
i'm trying to use mysqli module for PHP5 and it doesn't work. The module is activated in php.ini. What could be the problem?
<?php
require_once("class/guestbook.php");
$host = "localhost";
$user = "root";
$pass = "";
$db = "guestbook";
//Connect
$c = new mysqli($host, $user, $pass, $db);
//Check connection
if (mysqli_connect_errno()) {
printf("Connection failed! %s\n", mysqli_connect_error());
exit();
}
//Return the name of current database
if($result = $c->query("SELECT_DATABASE();")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
//Close connection
$c->close();
?>
Thank you!
The only thing wrong with your neat code, most probably, is your query itself.
SELECT_DATABASE(); // returns an error for obvious reasons
Means nothing to mysql. This does
SELECT DATABASE();
You don't see the error message because you don't check for errors on query execution. You are only looking for connection errors. Try an else{} block for $c->query and print the error message to see it.