Welcome to WebmasterWorld Guest from 54.166.152.121

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Connecting to MySQL Database

Can't get through... newbie

     
3:57 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



I'm just learning the ropes for MySQL and PHP, and I'm stuck.

<?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>&nbsp;</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.

4:06 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



First, you need to connect to the database (which is what mysql_connect does). Then select the database...

$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 ;)

5:11 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



Okay, that seems to connect to the database. However, I am now getting an error message that I am not able to find the table I want to access. If I open a database, does this automatically open all the tables associated with the database or I still have to use a command to open the table?

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.

5:23 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I see a couple of problems.

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>");

8:16 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



jatar_k, when you said:

"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]

9:18 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is how I connect, maybe It'll help.

$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];
}

5:38 pm on Mar 27, 2003 (gmt 0)

10+ Year Member



The script is now working. I manage to sew up all the tips and advices that all of you gave. Thank you very much for the help. IT WAS WELL APPRECIATED!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month