Using PHP to connect to MySQL: ERROR? - php

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.

Related

PHP PDO connection failing without error

It's been many years since I did any dev in PHP.
The concept of connecting to MySQL using PDO is completely new to me, and I don't seem to be able to get it to work.
Using MySQLi, and the following code, which I know is deprecated, works nicely:
<?php
include 'models/db_details.php';
$db_connection = mysql_pconnect("$dbhost", "$dbusername", "$dbpasswd") or die("Couldn't connect to server: " . mysql_error());
$db = mysql_select_db("$dbname", $db_connection) or die("Couldn't select database.");
?>
<h1>After connecting to the DB</h1>
As expected, this displays:
After connecting to the DB
From all of the pages I have read, using PDO, this should look something like:
<?php
include 'models/db_details.php';
try
{
$conn = new PD0("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbusername, $dbpasswd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<h1>After connecting to the DB</h1>
The result of this is a completely blank page.
All of this is on a FreeBSD box, running:
Apache/2.4.25
PHP/5.6.30
MySQL 5.6.35
I have uncommented extension=php_pdo_mysql.dll in php.ini.
I can also confirm that the MySQL driver for PDO is installed:
What am I missing?
Check the font. Make sure it is pdo and not pd0 (zero).
Magic happens after that.

Connection to db using php in localhost

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 :)

iPage Connect PHP to my Database

I am new to programming. I have a website on iPage. Now, I am learning PHP and one of the things I am learning is to connect PHP to mySql database. I am using the following:
mysql_connect(host name, username, password)
My question is, why I am not getting an error?
no matter what username and password and even host name i enter, it just accepts it!
This is the code I am trying (the username and password are just as example)
<?php
mysql_connect('ipage','admin','password');
echo 'Connected!';
?>
when I run it, it just says connected even though my username and password are not admin, password.
Use mysqli_*, beacuse mysql_* is deprecated and will be removed in the future:
$conn = mysqli_connect('localhost', 'username', 'password');
if(mysqli_connect_errno($conn))
{
die('Error in connection to MySQL: ' . mysqli_connect_error());
}
else
{
echo 'Connected successfully';
}
You are not checking if you are connected.
You need to use something like this:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
My suggestion is to start reading some docs, they have some great examples and you can really learn a lot. DOCUMENTATION

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

Categories