Website - Connecting to MySQL database - php

I am aware this is a stupid question, but to connect to a database that is on my local server, do i connect via this code:
mysql_connect("localhost","user","pass")
so using localhost connection. This seems to make sense, as it is sending a message to the host computer to connect to it's local database.
or do i use this piece of code:
mysql_connect("Ipaddress","user","pass")
This also makes sense to me somehow. Which one do i use for my website which i am hosting at home.
EDIT: obviously I want it so that people from all over the world enter information, and it is sent to a database. It isn't meant to be used by me.

A better way to do this is:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
};
?>
This is what I personally use, there are other ways to do this.

You should use localhost:
You could something like this:
<?php
$mysql = mysql_connect("localhost", "user", "pass");
if(!mysql){
die('Could not connect: ' . mysql_error());
}
//code her
mysql_close(mysql) //stops the connection
?>

Your database will most likely be stored on the same server as your program, you should put localhost.
Anyway, take some time to learn about PDO. It's more secure, cleaner and actually easier to use.

Related

changing code to mysqli

I read more than once that is better to use mysqli and than mysql.
I would like to know:
I'm using WAMP for my local server and some share server for my online sites. what do i need to check on php.ini in order to be sure that mysqli will works?
below is my db connection and query code. how would it looks like using mysqli?
$con = #mysql_connect ("localhost", "username", "pass");
if(!$con)
exit("Unable to connect to the Database server");
$db2 = #mysql_select_db("DB_NAME");
if(!$db2)
exit("Unable to connect to the Database");
$query = mysql_query("SELECT somme_filed FROM some_table");
while ($index= mysql_fetch_array($query ))
{
....
}
most of my site was written in mysql. Do I really need to change all the code to mysqli? Is it really so important?
1.As long as you have only one instance of php installed there should not be any problem but if you have more than one instance you may need to point it manually to the version you wish to use. How do I activate mysqli with wampserver?
2.You can use mysqli to connect by doing something like this
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
3.MYSQL is no longer under development and has been considered deprecated.
Why shouldn't I use MYSQL functions

mysqli_connect not working on shared server

Beginner question here. I am learning php and mysql. I have a website that I created using a local server. The database was linked to it fine on MAMP. I am now trying to upload it to a shared server using iPage. It will not connect.
Here is my code
<?php
$connect = mysqli_connect('host','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
I know the user, the password and the database name are correct. My only doubt is regarding the host. My website is in a subdomain. Should my code be:
<?php
$connect = mysqli_connect('subdomainName.domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or
<?php
$connect = mysqli_connect('domainName','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
or even
<?php
$connect = mysqli_connect('localhost','user','password','db_name');
if (!$connect) {echo 'Could not connect';} ?>
Do I need to do anything in my hosting page to link the database in any way. I have tried all options and nothing works. Thanks
Before implementing code you have to make new database in your website server,for this you have to log in to cpanel and make db in it, use that details and try the third one and use mysqli_connect_error() for getting the error details. like below.
Note: Better to use double quotes here because of string.
// Create connection
$conn = mysqli_connect("localhost", "username", "password");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if you are getting Connection failed: no such file or directory. then refer another question PHP - MySQL connection not working: 2002 No such file or directory
Better to contact your hosting provider if you are in shared hosting.

Can't connect my database on my live server

Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');

Accessing SQL Server 2012 from php under Linux

I'm in troubles...
I have a web server apache/php under linux, also i have another server with SQL Server database under windows. I need to get access to SQL Server database from my apache/php.
What do i need, please help. I have no idea how to start.
**sorry for my english.
Could do with a bit more clarification but I'll try my best..
If you mean through the code, you would do it like this:
Create a new php file called "connect.php" and insert all of this code, replacing the tags like USERNAME with the correct details.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Create connection
$con = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'DATABASE');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>
Next, EVERY file that requires a connection to the database, simply add
include("connect.php"):
To it at the start of the page and it should be fine :)
I seriously recommend you read up a bit more though as you seem like a bit of a newb (we all started somewhere)!
Hope this is what you were after!
Look for dblib, you will need to add that driver.

Correct Procedure for mysqlConnect

I am new to php syntax and am looking for advice on creating the most acceptable or correct code. I focus on front end design, but I like to make sure my code is proper. I am in a digital media program, my instructor has given us this code for connecting to our MYSQL databases.
<?php
mysql_connect("localhost", "root", "root")or die("Cannot Connect to DB");
mysql_select_db("Example")or die("cannot select the DB Example ");
?>
However when I look at connect scripts online they set the mysql_connect function as a variable lets say $connect and run an if statement stating; if not $connect produce error, and the same for mysql_select_db. They also close the script with mysql_close($connect); like below
<?php
$connect = mysql_connect("localhost", "root", "root");
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("Example", $connect);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($connect);
?>
Is either one better? What problems can I have if I dont close the connect with mysql_close?
Regarding using an if instead of an or, it doesn't matter. The or short-circuits, so if the connection worked, die(...) won't be executed. Using either is a matter of preference. If you want to use the or version while keeping the result of the mysql_connect() call, simply assign the whole expression to a variable:
$connection = mysql_connect(...) or die('Connection failed.');
mysql_close() should be used after you're done with all your database communication.
The other mysql_*() functions will use the connection created by the latest call to mysql_connect() or mysql_pconnect(). If for some reason you want more than one connection, trusting the implicit connection object in this manner will fail. Better is to store the connection object yourself and passing it in wherever you need it.
Also before starting to work with databases, you should be aware that mysql is not secure anymore and is deprecated, you should use mysqli. You can use it also as object oriented language.
more details : http://www.php.net/manual/en/book.mysqli.php
example :
<?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();
}
?>

Categories