changing code to mysqli - php

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

Related

PHP Oracle connection

I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");

select database using mysqli in config.php file

I have a nice free for searching a database. It was written with PHP and MySql...however for whatever reason, I occasionally need to add an "i" at the end of MySql occasionally to get things working. I can connect and login, but not select the database, since it passes through until I get the
"request "Unable to select database."
Here's the meat from the config.php file that probably has the issue :
$SETTINGS["hostname"]='localhost';
$SETTINGS["mysql_user"]='root';
$SETTINGS["mysql_pass"]='root';
$SETTINGS["mysql_database"]='myDB';
$SETTINGS["data_table"]='data'; // this is the default database name that we used
/* Connect to MySQL */
if (!isset($install) or $install != '1') {
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"]) or die ('Unable to connect to MySQL server.<br ><br >Please make sure your MySQL login details are correct.');
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
};
?>
The problem you are facing is the line in which you select the database;
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
As defined by the documentation of mysqli_select_db() the connection $connection ($link in the docs) should be the first argument:
$db = mysqli_select_db($connection, $SETTINGS["mysql_database"]);
The reason why you need to occasionally add an i to every mysql_* function is because all mysql_* functions are officially deprecated, no longer maintained and removed in PHP 7.0.0. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
With mysqli, you can select your database directly using the connection method :
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"], $SETTINGS["mysql_database"]);
If you want to use mysqli_select_db, you need to reverse your arguments. First the connection, then the database :
mysqli_select_db($connection, $SETTINGS["mysql_database"]);

Website - Connecting to MySQL database

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.

Trying to connect with sql server database

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

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