ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I installed apache, php and mysql on my Mandriva 2008.0 machine and I was able to setup a database, tables and add rows to the tables. Now, I want to be able to access this database through php. I have this code:
Code:
$db = new mysqli('localhost', 'username', 'password', 'database');
if (mysqli_connect_errno())
{
echo 'Can't connect to the database.';
exit();
}
I always get an error connecting. I can use the same username, password and database from the command line (mysql -u username ...) and connect no problem. Is there any type of configuration I need to setup to get this to work? Am I missing something?
Do you have the php_mysqli plugin? If not try the native driver found here
Your link requires me to register first before downloading the native php driver. Here is the error message from error_log in the /var/log/httpd directory:
Code:
[Thu Mar 13 16:01:08 2008] [error] [client 127.0.0.1] PHP Fatal error: Class 'mysqli' not found in /php/results.php on line 31
That's probably the issue. Where can I find the php configuration files in order to add this?
Your connection script is fine - seeing how it's straight of the PHP manual, it'd better be! The problem is that the PHP mysqli extension is either not enabled or (more likely) not installed. I'm assuming you installed PHP through your distribution's package manager. If so, there should also be a package for the mysqli extension (in Ubuntu, it's named php5-mysqli). Just install that and you should be all set. There's no need to mess around with building mysqlnd - the package provided by your distro should be more than sufficient.
haha.. well it's more recommended to use that script.
and with it.. his problem doesn't seem to be a MYSQL or PHP Extension error (check the link i first posted)
as i posted the extension link for mysqli at first.
if you see the error it is more likely a error of php (seeing it as a VAR) then extension error
and the script i posted was confirmed by our key4ce developer..
No, it's definitely a missing extension. The mysqli class is built into the extension. The only reason it could not be found is because the extension has not been loaded.
As for the connection script, while the one you posted will definitely work, it's the old way of doing things. Unless you're stuck with an old version of PHP and/or MySQL, or a large existing codebase that uses the mysql extension, you should be using mysqli. The support for prepared statements alone should be enough reason. The original mysql extension was never that great and there's no reason to use it now that better extensions like mysqli and pdo are around.
No, it's definitely a missing extension. The mysqli class is built into the extension. The only reason it could not be found is because the extension has not been loaded.
As for the connection script, while the one you posted will definitely work, it's the old way of doing things. Unless you're stuck with an old version of PHP and/or MySQL, or a large existing codebase that uses the mysql extension, you should be using mysqli. The support for prepared statements alone should be enough reason. The original mysql extension was never that great and there's no reason to use it now that better extensions like mysqli and pdo are around.
Thank you for your response. I did find and installed the php-mysqli-5.2.4-2mdv2008.0 package. I also pointed my browser to the .php page and again, I'm still getting that error. Saying that the class mysqli is not found. Do I have include a file in my php script to use this library???
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];
$searchtype = 'Title';
$searchterm = 'Java';
$searchterm = trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You have not enetered search details. Please go back and try again.';
exit();
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'username', 'password', 'books');
if (!$db)
{
printf("<p>Can't connect to localhost. Errorcode: %d</p><br/>", mysqli_connect_errno());
exit();
}
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of books found: '.$num_results.'</p>';
$result->free();
$db->close();
?>
</body>
</html>
Its very basic. I'm just learning php. I've been using ASP and ASP.NET for the past seven years, so the concepts aren't new, just the syntax and what libraries I need. This page is the POST page, but I assigned the variables just so I can call this page to see if it works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.