I have a website that has forms, images, text, etc. I want extract data from the forms and keep a record of them in mySQL. In order to do this do I need to change the file extension from '.html' to '.php'? And if so then will this effect any inline css?
Also, when I need to connect to the server via the php, how to I know the database username, database password, and the database host?
I have go daddy as the web host, and use the CPanel they provide to access the phpmyadmin
Thanks - any help is highly appreciated!
This is the PHP code I have so far, and I keep getting error alerts when I run the test through XAMPP:
mysql_connect() is already deprecated please consider using mysqli or PDO.
PDO . database connection example :-
You should create a separate class containing the functions for basic operation in database and keep that file separate from your other code , just inherit the class and use the connection and function .
<?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();
}
?>
As the other answer stated, use pdo or mysqli instead of 'mysql`.
A good way to use PDO is to put the connection code within a file, and include this file anywhere you need to make use of the database.
Let's call this file dbconnector.php.
try
{
$conn = new PDO($connectionString, 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Now, wherever you want to use database, just use :-
include 'dbconnector.php'
Now, you can access the connection variable via $conn.
Read more on PDO.
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
Related
I have recently deployed my new website, I use GoDaddy Unlimited Hosting and I'am unable to connect to the database for some reason.
Here's my code for connecting my website to the database:
<?php
ob_start();
session_start();
// db properties
define('DBHOST','some.example.ip.Address');
define('DBUSER','username');
define('DBPASS','password');
define('DBNAME','database-name');
$conn = #mysql_connect (DBHOST, DBUSER, DBPASS);
$conn = #mysql_select_db (DBNAME);
if(!$conn) {
die('Some Error Message');
}
define('included', 1);
?>
And Instead of connecting my website to the database and showing the content It shows the die() error message I have used above, And I tried adding if (! $conn) { mysql_error() or die('some message'); } I can see my website but can't see the db content and when I submit any form It shows this message Query failed. Access denied for user 'root'#'localhost' (using password: NO)
Thank you for your time.
Look into using PDO. mysql_connect is deprecated and generally not considered secure. However, it should still work.
Here's a simple PDO connection:
TRY {
$handler = new PDO('mysql:host=localhost;dbname=NameofDB','username','password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
CATCH(PDOEXCEPTION $e) {
echo $e->getMessage() . "<br>";
die ('sorry for your luck!');
}
There are lots of tutorials around the web for PDO implementation, and it's generally considered he way to go.
I'm working on a website that has a data base (phpmyadmin) with several articles, what i want to do now is to put all off that on a remote server. I never did this with a website that has mysql, i tried this now and it seens it can't connect with the database ('Database error.'). So i have this code to do the connection:
<?php
try {
$pdo = new PDO ('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
}
catch (PDOException $e) {
exit('Database error.');
}
?>
It all works at 100% on localhost so i guess what it's missing is just the connection. Do I have to do something on this piece of code? if yes, What is it? and do I have to do anything else? plz explain like i'm a child. Thanks very much
File : Config.php
<?php
require 'inc.database.php';
// Checking if there already a connection. If not then connect to the database.
if(!$IsConnected){
$Database = new Database();
$Database->connect("localhost", "aih786_raheel", "raheel786", "aih786_basicblog");
$IsConnected = TRUE;
}
?>
I m using my config file on my every page because on every page i need to have my database object. Thing i want to clear is that by this approach can i avoid multiple attemps to connect to the database as it is not a good practice to make same connection again and again.
Lets say i have a login page which is the first page of my cms. The connection will be opened on the login page and now when i move to the dashboard.php page i require the config.php file in this page too...so by this it won't create the connection and object again.
Pleas tell me is this the right approach to achieve my goal and also will it give me the access to the object $Database ? I'm not sure if we can use the object on differnt pages once it has been created on first page.
A very rudimentary approach would be to define a function that returns a database connection on-demand, e.g.:
function getDefaultDatabaseConnection()
{
$db = new Database;
$db->connect(...);
return $db;
}
Usually, I try to fire up one connection per page load that needs it.
If I have the proper variable already stored in SESSION variables, then oftentimes
it is not necessary to fire one up.
Given that, I do consider it proper form to drop the connection object at the end
of the script that called it.
And Jack is right, I use a function to fire up the connection.
function dbConnect_readOnly() {
$host="127.0.0.1";
$user="*********";
$password="********";
$dbname="**********";
try {
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo "Unable to connect to database.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
return $DBH;
}
and to close:
function dbClose_connection($DBH) {
$DBH = null;
}
Include the script at the top of every page that eeds connectivity just after you check for session variables.
I'm trying to connect sql server with php, i'm trying to get info from the database..
Now, here is what i got:
try {
$user = '';
$pass = '';
$objDb = new PDO('mysql:host=192.168.10.250;dbname=WEB_POROSIA',
'$user', '$pass');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM 'WEB_POROSIA'
";
$statement = $objDb->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
I receive this error:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
This is my first attempt to connect php with sql server, and i don't know what this output means, i mean i don't know what might cause it!
I'm using xampp.
Thanks
It appears that you are trying to connect using the wrong driver.
For Sql Server you might want to use PDO ODBC driver not the mysql method you are trying to use.
You need to use a different DBO driver.
Check out other ODBC drivers, i.e PDO.
The code you have written tries to connect to a MySQL database rather than a MSSQL one.
You'll want to do something like this:
$db_connection = new PDO("dblib:dbname=$db_name;host=$host", $username, $password);
You can find more information here: http://grover.open2space.com/content/use-php-and-pdo-connect-ms-sql-server
i have a few question on php db connection and hoping someone can answer them all, when i create a db connection using pdo, like below
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
is this connection always created when someone refreshes the php page?
or does it check if that connection is already open and then use that connection instead?
how would i be able to close that connection when i am done with it?
yes
nope. It tries to utilize previously established connections only if you have set up permanent connections
generally you don't need to do anything special. php does that as long as your script ends
I found this in the php manual. Hope it helps.
To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.