How to write mysql php errors to a text file - php

I have the following PHP code that creates a mysql connection:
$link = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
In the event the connection is not made, how can I write the error to a text file on my server?

Yes it is possible with error_log function in php
$con = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$con)
{
error_log(mysqli_error($con) . "\n", 3, "/var/tmp/my-errors.log");
}

Related

Including a database connection from a separate file

I currently have a db file I include to establish a DB connection:
db.php:
$dbs = array(
"127.0.0.1",
"127.0.0.3",
"127.0.0.2",
);
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
However when I try to include it sometimes and do a query, it returns a blank result:
include("db.php");
mysql_select_db("database_table", $con);
$result = mysql_query("SELECT * FROM users where email='admin#test.com'");
while($row = mysql_fetch_array($result)){$id = $row['email'];break;}
The weird thing is, if I modify the initial db.php to connect like this, it works (non randomizing it):
db.php:
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Is there any explanation in this? Is there a difference in how they are connecting?
Thanks
First I would turn on error checking.
You can do that by adding the following to the very top of your php file:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
or the long way by changing it globally: display_errors = On in your php.ini
This will output what's wrong. If you can't figure it out be then try the following:
Then check if you could connect to all databases like you did with
if(!$con){
$con = mysql_connect("127.0.0.1","user_login","password") or die('Could not connect: ' . mysql_error());
}
Then I would check if they all had the same tables.
If that's correct I would check the output of your:
if(!$con){
$con = mysql_connect($dbs[rand(0,2)],"user_login","password") or die('Could not connect: ' . mysql_error());
}
Does it really work the way it's intended?

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());
}
?>

Mysql connection error

I am trying to access localhost db from remote server .
$link = mysql_connect('192.168.65.44', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
Warning: mysql_connect(): Can't connect to MySQL server on '192.168.65.44' (4) in /home/xx/xx/xx/xx/xx
Could not connect: Can't connect to MySQL server on '192.168.65.44' (4)
The error tells that there's a problem in your host. If you are trying locally in your system, use
$link = mysqli_connect('localhost', 'root', '');
Note : mysql_ is deprecated. You need to use either Improved Mysqli (mysqli_) or PDO.
put "localhost" in place of "192.168.65.44" in
mysql_connect
Try this it will work :
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

Error connecting PHP to SQLServer

I am using a script php that connect to sqlserver but it doesn't works.
//connection to the database
$dbconn = mssql_connect($Server)
or die("Couldn't connect to SQL Server on $Server");
if($dbconn)echo 'Connected';
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
![when running script it gives this error, i m using windows authentication][1]
http://i.stack.imgur.com/a9ndN.jpg
Check mysql_connect()
$dbconn = mysql_connect('hostname','username','password');
Connect using mysql_connect()
$dbconn =mysql_connect(servername,username,password);
example
$con = mysql_connect("localhost","me","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

php mysql problem

I have installed php and mysql on windows xp with iis 5.1 as my web server. But when i run the following code it just shows me a blank page. Can anybody let me know where i am wrong. Thanks in advance.
<?php
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Well, it doesn't generate any output if the connection succeeds so that's probably what's happening. What would you expect?
<?php
$con = mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
?>
what does this print ?
* make sure you have mysql running
* make sure you have php running on IIS
* try to check in php.ini:
error_reporting = E_ALL
display_errors = On
Use the following code
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} else {
die('Connection ok!');
}

Categories