Accessing Databases

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

Accessing Databases

Post by Imosa »

I'm new to using PHP and I've been taking a self-taught independent study in my school. Right now I'm trying to figure out why I can't connect to my local host as well as I cannot select my database. I've been trying to use PHP inside of a HTML file and I'm not even sure if that works? My final project is due a week from today and I've spent the last week working on this one small problem.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing Databases

Post by JakeJ »

Post the code you have and maybe we can help correct. Without that, there's nothing we can do. Make sure you post ALL of the code especially since you're new to php we can't assume anything.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Accessing Databases

Post by mikosiko »

And a more basic question...

Do you have a webserver (IIS/Apache), PHP and your database (I'm assuming Mysql) installed and working in your localhost ?
scorpio90
Forum Newbie
Posts: 23
Joined: Wed Mar 10, 2010 10:45 am
Location: Newcastle

Re: Accessing Databases

Post by scorpio90 »

Also, make sure you have all the correct infomation if you are using a connection script, such as you password, server (localhost) and database name as if not it will just spit out a load of errors :D heres a example :


$host = "localhost";
$usr = "username";
$pwd = "password";
$db = "database name";

$cid = mysql_connect($host, $usr, $pwd);
mysql_select_db($db, $cid);
if (!$cid) {echo("ERROR: " . mysql_error() . "\n");
}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Accessing Databases

Post by califdon »

Finally, include error reporting in your PHP syntax so that you will have some feedback from your server about what the specific nature of the problem is, like this:

Code: Select all

...
mysql_connect($host, $user, $pwd) or die(mysql_error());
...
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

Re: Accessing Databases

Post by Imosa »

Is there anything that could be causing issues in php.ini? I've tried tons of guides and I'm still coming up with issues.. I've been receiving back on one of my test files:
Fatal error: Call to undefined function mysql_connect() in C:\ApacheDocumentRoot\benstore.php on line 11
I'll post my code just to see if it might have some sort of error that I haven't noticed.
<?php

$dbuser = 'webpro';
$dbpass = 'webpro';
$database = 'benstore';
$table = 'shoes';

$con = mysql_connect(localhost, $dbuser, $dbpass) or die('Error connecting to mysql');

mysql_close($con) or die('Error close mysql');


?>
Theres some other stuff that I've taken out but when I try to run the code above it won't work.

Thanks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Accessing Databases

Post by califdon »

In your connection string, put quotes around "localhost". That should do it.
Imosa
Forum Newbie
Posts: 12
Joined: Tue May 04, 2010 11:56 am

Re: Accessing Databases

Post by Imosa »

Already tried it.. Could it be a problem with the server?

I get this error message when I try to put quotations around localhost:
Fatal error: Call to undefined function mysql_connect() in C:\ApacheDocumentRoot\benstore.php on line 11
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Accessing Databases

Post by Eran »

the mysql extension is not installed.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing Databases

Post by JakeJ »

pytrin wrote:the mysql extension is not installed.

To verify this, create a page with the following code

Code: Select all

<?php echo phpinfo(); ?>
Post Reply