Sql Lite connection. Is it possible with Msqli procedural? - php

I have a project with MySQLi procedural and PHP. People ask me to make it work off line. I have been searching and I could find sql lite. I have installed it with MAMP. I have managed to create the same table that I have in MySql. Now I try to make the connection. I have google it and I could find ways to do it with PDO. It works well for me and I could apply it. But before going further I would like to be sure that there is no way to do that with MySQLi procedural (this would save me a lot of time and problems if I do not have to change everything to PDO).
So, my question: is it possible to use MySQLi procedural to make a connection with Sql Lite? If so, what would be the equivalent of this:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connection with the server failed: ' . mysqli_error());
}
//Database name is : 'test'
if (!mysqli_select_db($con, 'test')){
echo "The connection with the database failed";
}
(this is where I have the sql lite database: /Applications/MAMP/db/sqlite/test
I have a Mac, I have installed MAMP php lite Admin, and I am very new to all this. I have manage to do it alone, just with Google but I am not sure if I am going in the right direction. I have no experience. I would I appreciate any suggestion of someone with any experience )

Related

PHP remote database access with plugin

I have been developing a plugin for Wordpress and I am stuck at point where I have to connect my plugin to a database remotely.
I know the procedural way of estabilishing the connection to a database which looks something like this $link = mysqli_connect("hostname", "username", "password", "database");
The $link variable will either return true or false depending on connection status, in my case the plugin is not allowed to connect to my MySQL server due to Administrator restrictions.
I have few questions regarding this.
What do you do in cases like these? If one enables the access via MySQL dashboard on a single database to make the connection, would that make a database vulnerable to injections or less protected?
If the previous is the right way, how do you pass the parameters properly to a function without revealing the plain credentials?
Is there any other way to implement this logic to get the needed data that plugin requires?
P.S. I have already tried looking up here for an answer without luck, the PHP documentation also did not help.

Best practices for updating from mysql to PDO

I'm updating a code base from mysql to pdo and this is the first time I've done a project like this so I've been doing research on best practices and would like some input. Here is the old code:
$link = #mysql_connect('localhost', "xxx", "xxx")
or die('Could not connect: ' . mysql_error());
mysql_select_db("xxx") or die('Could not select database');
In my code I'm putting all the login credentials into a separate file and using the ip address, username and password to connect as opposed to localhost.
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
log_error("Failed to run query", $e->getMessage(), $e->getCode(), array('exception' => $e));
die('Could not connect!');
Two questions, is this a good alternative to using the die method deal with errors and log to the client and also is this the correct way to connect? Is there an advantage to connecting using localhost vs the server IP address? Thanks for the help!
Is this a good alternative to using the die method?
What you're doing is reasonable as part of a migration - it should behave the same way it did before. You might consider, however, not catching that PDOException and allowing it to be thrown all the way up to your global error handler. That's nicer because it's less code, and you're not catching an exception just to die. But before making that change, you should have an idea of the way it will behave in your production systems (will you see the error in your logs)?
Is this the correct way to connect?
Yes, it seems like you're using PDO correctly.
Is there an advantage to connecting using localhost vs the server IP address?
You mean in production? Best practice would be to run your database on a separate server - in which case localhost would obviously not work. If the load is light and you are running the database and PHP server on the same box for now, then it shouldn't matter either way.
Good questions, and I already have all the answers for you (and even answers to questions you didn't ask yet), just follow my article, How to connect to MySQL using PDO.
To answer your questions more directly:
is this a good alternative to using the die method deal with errors
By all means - no.
To be honest, your current approach is not much better than the old one. The code here is inflexible, you have to edit it manually to change the behavior. It would be much better if your code would only throw an error, whereas its processing would be defined elsewhere and easily configurable.
Let alone the die() is still there. Trust me, a site that tells you 'Could not connect!' on a white screen looks awfully unprofessional. You should never ever use die like that.
#mkasberg is right, generally, you don't catch an Exception but let it bubble up to where it will be appropriate to handle it. However, PDO connection is a special case as the stack trace in case of error would contain the db credentials which you likely don't want to show to anyone. To prevent this, I propose to throw a brand new exception that would contain the same error information but no stack trace.
is this the correct way to connect?
Apart from what was said above, without seeing the DSN we cannot tell for sure.
Is there an advantage to connecting using localhost vs the server IP address?
Generally, a hostname is preferred, like with any other service. the IP address could change but thanks to DNS system the domain name would always point at the correct address. However, localhost is a special case, and judging by many questions here on Stack Overflow I would rather recommend to use the IP address, as it could save you a headache or two, like a too big timeout when DNS is misconfigured.

How to connect MySQL to PHP?

So I'm brand spanking new to mysql and php.
I'm set up with Mysql workbench and I'm practicing building a site using Notepad++ and just run it through Chrome. All I want to do is create a sign up page, which I'm assuming I use a .php page on the site, where it would be a username and password. That's it. I can't seem to find any tutorials on how to connect mysql to the .php page, or how to create a sign in page. Any help would be appreciated!
Welcome to PHP!
Typically a connection is established on a PHP page with something along the lines of this:
$conn = mysqli_connect("localhost","[username]","[password]","[databasename]") or die("Error " . mysqli_error($conn));
The "or die" will produce an error if there's a problem establishing a connection. Also, this uses the newer "mysqli_" method for connection; make sure when you call this connection in future that you use mysqli_ methods (there are still traditional "mysql_" methods available, but are depreciated).
Hope this helps!
M
here you go you, here you can find a way to properly connect to the database as well as all the data you need to get set up with your signup form
http://mrbool.com/how-to-create-a-sign-up-form-registration-with-php-and-mysql/28675

cross server access in php

Can I get data from three different servers on my php application? Actually I have my data on three different servers and I want to generate a report with having data from all three servers. Can you please suggest me a code for this?
function dbcon(ipaddress,servername,serverpassword,databasename)
{
$con = mysql_connect(ipaddress,servername,serverpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(databasename) or die ("Cant find DB");
}
Certainly that is possible. I assume (though you are not very clear in this) that you are talking about three database servers? Then all you have to do is:
make sure the database servers are accessible via network (tcp)
create three database connections instead of only one
Since opening a connection to a database server returns a handle you have something you can use to address a specific connection. The syntax required for opening the connection is clearly explained in the manual pages. I suggest you read them and take a look at the provided examples...
First:
Welcome to Stack Overflow! Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
In order to connect to three different databases, you'll need 3 connection objects, and query each separately. Make sure you have configured the receiving ends to correctly accept connections.

read a .sdf (SQL Server CE) in php

I have a file called PhotoGallery.sdf and was wondering how to connect to this db with php, or if it is even possible?
Maybe point me to the right documentation. I found query strings for this, but not a db connect string...

Categories