Accessing Databases
Moderator: General Moderators
Accessing Databases
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
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
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 ?
Do you have a webserver (IIS/Apache), PHP and your database (I'm assuming Mysql) installed and working in your localhost ?
Re: Accessing Databases
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
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");
}
$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
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
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:
Thanks.
I'll post my code just to see if it might have some sort of error that I haven't noticed.Fatal error: Call to undefined function mysql_connect() in C:\ApacheDocumentRoot\benstore.php on line 11
Theres some other stuff that I've taken out but when I try to run the code above it won't work.<?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');
?>
Thanks.
Re: Accessing Databases
In your connection string, put quotes around "localhost". That should do it.
Re: Accessing Databases
Already tried it.. Could it be a problem with the server?
I get this error message when I try to put quotations around localhost:
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
the mysql extension is not installed.
Re: Accessing Databases
pytrin wrote:the mysql extension is not installed.
To verify this, create a page with the following code
Code: Select all
<?php echo phpinfo(); ?>