homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Connecting to MySQL Database
Can't get through... newbie
Francis

10+ Year Member



 
Msg#: 647 posted 3:57 pm on Mar 25, 2003 (gmt 0)

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.

 

DrDoc

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



 
Msg#: 647 posted 4:06 pm on Mar 25, 2003 (gmt 0)

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

Francis

10+ Year Member



 
Msg#: 647 posted 5:11 pm on Mar 25, 2003 (gmt 0)

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.

jatar_k

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



 
Msg#: 647 posted 5:23 pm on Mar 25, 2003 (gmt 0)

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

Francis

10+ Year Member



 
Msg#: 647 posted 8:16 pm on Mar 25, 2003 (gmt 0)

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]

Birdman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 647 posted 9:18 pm on Mar 25, 2003 (gmt 0)

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

Francis

10+ Year Member



 
Msg#: 647 posted 5:38 pm on Mar 27, 2003 (gmt 0)

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved