Forum Moderators: coopster
<?php
$id = $_GET["id"];
$title = $_GET["title"];
$db = "mydb";
// Connect to the database server
$dbcnx = @mysql_connect($db);
if (!$dbcnx) {
echo( "<P>Unable to connect to the " . "database server at this time.</P>" );
echo( "<P>Please try again later.</P>" );
exit(); }
// Select the Plans Database
if (! @mysql_select_db($db) ) {
echo( "<P>Unable to locate the Plans " . "Database at this time.</P>" );
echo( "<P>Please try again later.</P>" );
exit(); }
// Request Tip selected by user
$result = mysql_query("SELECT Title, Description FROM Plans where id=$id");
if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit(); }
?>
<html>
<head>
<?php
echo("<title>" . $title . "</title>");
?>
</head>
<body>
<?php
// Display the text of each Tip in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<H1>" . $row["Title"] . "</H1>");
echo("<P>" . $row["Description"] . "</P>");
echo("<P> </P>");
}
?>
</body>
</html>
I've tried using a variable to represent the database, but I'm still getting the same error.
Unable to connect to the database server at this time.
Please try again later.
An additional question: >>>> Can the .php file be anywhere? Or does it have to be in a special folder (like the cgi-bin perhaps)? <<<<
Thanks again for your indulgence and patience.
$conn = mysql_connect("localhost", "username", "password");
$db = mysql_select_db("mydb");
...
Your php files can technically be anywhere, unless specified otherwise by the settings. I would, however, recommend not putting them in the cgi-bin directory. Depending on server configuration it might cause unnecessary head ache ;)
This is now my code:
// Connect to the database server
$dbcnx = mysql_connect("localhost");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " . "database server at this time.</P>" );
echo( "<P>Please try again later.</P>" );
exit(); }
$db = mysql_select_db("mydb");
// Select the Plans Database
if (! mysql_select_db("Plans") ) {
echo( "<P>You were able to connect to the database.</P>" );
echo( "<P>However, we are unable to locate the Plans" . " table at this time.</P>" );
echo( "<P>Please try again later.</P>" );
exit(); }
Now, I'm getting the above specified error. That's why I assume that I was able to connect to the database. Also, I just used localhost (tech support from hosting company said so), although I specified a username and password to access the MySQL Database. Is this okay?
I know that my questions are pretty simple. But... however, thanks again.
Your mysql_connect [php.net] has no username or password.
also isn't Plans your table name? in mysql_select_db [php.net] you need to refernce the db name.
If I open a database, does this automatically open all the tables associated with the database
yes, if you want to test if you are actually connecting to the db get rid of the @ in front of your connect because that will suppress errors and use something like
$dbcnx = mysql_connect($db) or die [php.net]("<p>" . mysql_error()."<br>");
"in mysql_select_db you need to refernce the db name."
How do I exactly do it?
This is what I did already... same results.
$db = mysql_select_db("sitename"."Plans");
// Select the Plans Database
if (! mysql_select_db("Plans") ) {
echo( "<P>You were able to connect to the database.</P>" );
echo( "<P>However, we are unable to locate the " . $table . " table at this time.</P>" );
echo( "<P>Please try again later.</P>" );
exit(); }
[edited by: jatar_k at 9:30 pm (utc) on Mar. 25, 2003]
[edit reason] generalized [/edit]
$dbh=mysql_connect("localhost", "siteusername_dbusername", "password") or die ('No can do!');
mysql_select_db ("siteusername_dbname");
$query = "SELECT * FROM plans";
while($i = mysql_fetch_array($query)){
echo $i[columnname];
echo $i[anothercolumnname];
echo $i[yetanothercolumnname];
}