Connection to db using php in localhost - php

Even if I have used LAMPP many times, this time something goes wrong. When I visit the browser(chrome) nothing echos. Here is my code:
index.php
<?php
error_reporting(E_ALL); /*after edit*/
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Do I miss something? The output is nothing. By the way i write my files in
var/www/html/my_pages
and i call it this way: localhost/my_pages. Simple echos are working and php in general is fine. Something goes wrong with my db connection.

Use this code
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
else
{
echo 'Connected successfully';
}
?>

Yes probably, because mysqli_connect() method return the object, not Boolean value.
You can verify the connection with the following code:
if($link->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return false;
}
else{
echo "Connection Successful";
return $link;
}

Why not using PDO.
$dsn = "mysql:host=localhost;dbname=db";
try {
$pdo = new PDO($dsn, "root", "");
} catch (PDOException $ex) {
$ex->getMessage();
}
with PDO you can change anytime you need the Db vendor:
http://php.net/manual/en/book.pdo.php

<?php
echo phpinfo();
?>
Run this file and get all PHP and apache details. Search for mysqli support in it. If it is supported, you should have something like below.
Also check for your root directory

Thanks everyone for the response! I found the problem. Something went wrong and mysqli wasn't enabled in php. That's why i had this error Fatal Error:Call to undefined function mysqli_connect() in /var/www/html/diamond/index.php on line 8 I reinstalled php and problem solved :)

Related

Can't Connect to a Database on IPOWER hosting - PHP / MySQL

Please help me connect to a DB - I need to get this to work:
<?php
require_once 'general.php';
try {
$db = new PDO('mysql:host=account.ipowermysql.com;dbname=zipcodes', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
print 'Oh SNAP ... Error!: ' . $e->getMessage() . '<br/>';
die();
}
If i run it as is, i get a 500 error page.
If I add ?> at the end, I get a blank page.
If I run "DB-connect" code generated by IPOWER it works.
IPOWER generated code:
<?php
$link = mysql_connect('account.ipowermysql.com', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(zipcodes);
?>
I guess the server is configured not to show PHP errors, but a 500 error page instead...
PS - here is general.php code:
<?php
error_reporting(0);
#ob_start();
session_start();
function echo_session($arg) {
if(isset($_SESSION[$arg])) {
return $_SESSION[$arg];
} else {
return '';
}
}
PS2 - original code (at the top) works on GoDaddy and MediaTemple ... it's something with IPOWER ...
I'm a noob and I need this to work, but don't know how to.
Please help

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

How do I check if I'm connected to database?

This is the code to connect to the mysql database:
$con = mysql_connect("", "", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
I want to echo 'connected'/'disconnected' depending on state.
How can it be done?
Do it Like this
if ($con) {
echo 'connected';
} else {
echo 'not connected';
}
Or try this
echo $con ? 'connected' : 'not connected';
Firstly, use the mysqli_xxx() functions instead of the old obsolete mysql_xx() functions.
This is strongly recommended anyway because the old library is in the process of being deprecated, but will also make your question easier to answer. (you could also use the PDO library, for which the answer will be similar, but for this answer I'll stick with mysqli for simplicity)
With the mysqli library, you get a variable that contains your DB connection, which you can examine at any point.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
Now, you can query the $mysqli variable to find out what is happening.
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
Later on, you can query the variable using the ping command: http://www.php.net/manual/en/mysqli.ping.php
$mysqli->ping();
or maybe the stat command if you want more info: http://www.php.net/manual/en/mysqli.stat.php
Try using mysql_ping
mysql_ping — Ping a server connection or reconnect if there is no
connection
something like this should help you
if(mysql_ping($con))
echo 'connected';
else
echo 'disconnected';

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Using PHP to connect to MySQL: ERROR?

I tried running the following code: (http://localhost/read.php)
<html>
<body>
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE DATABASE testphp",$link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
</body>
</html>
and got the following error:
Fatal error: Call to undefined function mysql_connect() in
C:\Program Files (x86)\ApacheSoftware Foundation\Apache2.2\htdocs\read.php
on line 5
Look at you phpinfo(). Most likely mysql extensions is not there.
And while you are at it, you could just drop the ancient mysql_* way ow doing thing and learn how to use PDO and prepared statements. It's an abstraction API for database connection and interaction.
your mysql-extension for php is not loaded! check that in your php.ini.

Categories