Page 1 of 1

Accessing Databases

Posted: Fri May 07, 2010 12:24 pm
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.

Re: Accessing Databases

Posted: Fri May 07, 2010 12:28 pm
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.

Re: Accessing Databases

Posted: Fri May 07, 2010 1:06 pm
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 ?

Re: Accessing Databases

Posted: Sat May 08, 2010 5:41 am
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");
}

Re: Accessing Databases

Posted: Sat May 08, 2010 11:55 am
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());
...

Re: Accessing Databases

Posted: Wed May 12, 2010 12:27 pm
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.

Re: Accessing Databases

Posted: Wed May 12, 2010 12:45 pm
by califdon
In your connection string, put quotes around "localhost". That should do it.

Re: Accessing Databases

Posted: Thu May 13, 2010 11:48 am
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

Re: Accessing Databases

Posted: Thu May 13, 2010 12:33 pm
by Eran
the mysql extension is not installed.

Re: Accessing Databases

Posted: Thu May 13, 2010 4:32 pm
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(); ?>