LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2008, 06:54 AM   #1
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Rep: Reputation: 56
Trying to get PHP to connect to a MySQL Database


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?
 
Old 03-13-2008, 09:19 AM   #2
key4ce
Member
 
Registered: Mar 2008
Location: Eindhoven
Distribution: Free BSD / CentOS/ Windows 2008
Posts: 42

Rep: Reputation: 15
what is your php and mysql version?
 
Old 03-13-2008, 09:39 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

If you change the echo to this:

PHP Code:
printf("Can't connect to localhost. Error: %s\n"mysqli_connect_error()); 
You will then get a proper error message so it will be easier to figure out what the problem is.
 
Old 03-13-2008, 10:36 AM   #4
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Rep: Reputation: 15
Just in case: You are filling in "password" and "username" with your actual mysql password and mysql username, right?
 
Old 03-13-2008, 11:55 AM   #5
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by derzok View Post
Just in case: You are filling in "password" and "username" with your actual mysql password and mysql username, right?
Yes, of course.
 
Old 03-13-2008, 11:58 AM   #6
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by key4ce View Post
what is your php and mysql version?
PHP: 5.2.5

MYSQL: 5.0.45
 
Old 03-13-2008, 12:24 PM   #7
key4ce
Member
 
Registered: Mar 2008
Location: Eindhoven
Distribution: Free BSD / CentOS/ Windows 2008
Posts: 42

Rep: Reputation: 15
Do you have the php_mysqli plugin? If not try the native driver found here
 
Old 03-13-2008, 03:06 PM   #8
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by key4ce View Post
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?
 
Old 03-13-2008, 04:12 PM   #9
key4ce
Member
 
Registered: Mar 2008
Location: Eindhoven
Distribution: Free BSD / CentOS/ Windows 2008
Posts: 42

Rep: Reputation: 15
You fix that by the link i gave you.
follow the steps (as well as the configuration as the download) and you will resolve that error
 
Old 03-13-2008, 04:15 PM   #10
key4ce
Member
 
Registered: Mar 2008
Location: Eindhoven
Distribution: Free BSD / CentOS/ Windows 2008
Posts: 42

Rep: Reputation: 15
Sorry I made a mistake there.

try this as a connection script:
mysql_connect("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database");

your mysqli script isn't the standard way for connecting.

as i think php might look for defining mysqli as a variable rather then a actual use like mysql_connect

(sorry getting a bit late. so please excuse me if i am being vague)
 
Old 03-13-2008, 06:35 PM   #11
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
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.
 
Old 03-13-2008, 06:51 PM   #12
key4ce
Member
 
Registered: Mar 2008
Location: Eindhoven
Distribution: Free BSD / CentOS/ Windows 2008
Posts: 42

Rep: Reputation: 15
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..

try it.. (never hurts to try right?)
 
Old 03-13-2008, 07:50 PM   #13
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
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.
 
Old 03-13-2008, 08:56 PM   #14
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by AdaHacker View Post
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???
 
Old 03-13-2008, 09:00 PM   #15
bassplayer69
Member
 
Registered: Jul 2007
Location: In a van down by the river...
Distribution: MX Linux 21
Posts: 237

Original Poster
Rep: Reputation: 56
Here's the actual code for the whole page:

Code:
<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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot connect to mysql database PAD_LOCK Programming 5 09-01-2006 02:03 PM
How To Connect To Posgresql/mysql Database Using C cs-mania Programming 4 07-05-2006 11:00 PM
cannot connect to mysql database from php rocordial Linux - Software 2 08-21-2005 02:05 PM
cannot connect to mysql database externally lsimon4180 Linux - Software 17 03-02-2005 12:28 PM
cannot connect to mysql database mrosati Linux - Software 19 07-15-2004 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration