hard time connecting to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dilirium
Forum Newbie
Posts: 2
Joined: Sun May 01, 2005 10:32 pm

hard time connecting to database

Post by dilirium »

I am very new to using PHP and calling up data from a MySQL database, and ask for assistance from anyone that could help.

The following is my HTML code:

Code: Select all

<html>
	<head>
		<title></title>
	</head>
<body>
	<?php
		// connect to database
		$host="localhost";
		$user="username";
		$pass="password";
		$database="databasename";
		
		$connect=mysql_connect($host,$user,$pass) or die("Could not connect.");
		mysql_select_db($database); or die("Could not select database.");
		
		// get data from table
		$result=mysql_query("SELECT * FROM tablename");
	?>
	
	<?php
		echo "<table border='1' color='000000' cellpadding='0' cellspacing='0'>";
		echo "<tr><th>ONE</th><th>TWO</th></tr>";
		while($row=mysql_fetch_array($result)) {
		echo "<tr><td>";
		echo $row['one'];
		echo "</td><td>";
		echo $row['two'];
		echo "</td></tr>;
                }
		echo "</table>";
	?>
</body>
</html>

In the database itself, from phpmyadmin, I have the following fields:

id, int(11), no null, auto_increment, primary key
one,text, no null
two, text, no null


When I run the PHP file all I get is a blank screen - no error, no nothing. I wish there was an error message instead as it would lead me to how to fix this problem.

I am sure its something simple that I missed and I'd appriciate the help. Thanks guys. :)

Jcart | Please read :arrow: Posting Code in the Forums
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Look at your code with syntax highlighting.
Plus your

Code: Select all

or die()
statements should look like this

Code: Select all

or die('Error: '. mysql_error())
dilirium
Forum Newbie
Posts: 2
Joined: Sun May 01, 2005 10:32 pm

Post by dilirium »

Jcart wrote:Look at your code with syntax highlighting.
Plus your

Code: Select all

or die()
statements should look like this

Code: Select all

or die('Error: '. mysql_error())
Still no error, just blank :cry:
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

<html>
    <head>
        <title></title>
    </head>
<body>
    <?php
        // connect to database
        $host="localhost";
        $user="username";
        $pass="password";
        $database="databasename";
        
        $connect=mysql_connect($host,$user,$pass) or die("Could not connect.");
        mysql_select_db($database) or die("Could not select database.");
        
        // get data from table
        $result=mysql_query("SELECT * FROM tablename");

        echo "<table border='1' color='000000' cellpadding='0' cellspacing='0'>";
        echo "<tr><th>ONE</th><th>TWO</th></tr>";
        while($row=mysql_fetch_array($result)) {
        echo "<tr><td>";
        echo $row['one'];
        echo "</td><td>";
        echo $row['two'];
        echo "</td></tr>";
                }
        echo "</table>";
    ?>
</body>
</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Something to try is outputting tidbits of text along the way to see what you get. If you get nothing, run a phpinfo() page to see what you get.

Code: Select all

<html>
    <head>
        <title></title>
    </head>
<body>
    <?php
        // connect to database
        $host="localhost";
        $user="username";
        $pass="password";
        $database="databasename";

        if ( $connect = mysql_connect($host, $user, $pass) )
        {
            echo "Connection worked.<br />\n";
        }
        else
        {
            die("Could not connect: " . mysql_error());
        }

        if ( mysql_select_db($database) )
        {
            echo "Database was selected also<br />\n";
        }
        else
        {
            die("Could not select database: " . mysql_error());
        }

        // get data from table
        if ( $result=mysql_query("SELECT * FROM tablename") )
        {
            echo "We have a query also.<br />\n";
        }
        else
        {
            die("Sorry, no query: " . mysql_error());
        }

        echo "<table border='1' color='000000' cellpadding='0' cellspacing='0'>";
        echo "<tr><th>ONE</th><th>TWO</th></tr>";
        while($row=mysql_fetch_array($result))
        {
            echo "<tr><td>";
            echo $row['one'];
            echo "</td><td>";
            echo $row['two'];
            echo "</td></tr>";
        }

        echo "</table>";
    ?>
</body></html>
If the above doesn't work, then try:

Code: Select all

<html>
<head></head>

<body>
<?php
    phpinfo();
?>
</body>
</html>
This might show a little more information.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

as usually, the first lines of your script should look like

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
and then, as others already mentionned, you should use mysql_error to find out a more concrete explanation of eventual errors...

and think about *every* line of code you write, and variable you use.

For example, you assign the database connection to $connect and after that you stop using $connect. So, is that assignment really needed? Or do you need to pass the link to the other mysql_ calls? It's all about being consistent :)
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Re: hard time connecting to database

Post by thegreatone2176 »

going back to this original code you have

Code: Select all

mysql_select_db($database); or die("Could not select database.");
you have a ; in the middle which i think may make that or be true everytime and make your script die.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i just tested what the ; or die would do
Parse error: parse error, unexpected T_LOGICAL_OR
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

mysql_select_db($database); or die("Could not select database.");
should be

Code: Select all

mysql_select_db($database) or die("Could not select database.");
Basically, the first line would be like this:
First, select the database.
Second, or die.
(php: what? "or die?" I don't understand!!) :P

Instead of using:

Code: Select all

echo "<tr><td>";
echo $row['one'];
echo "</td><td>";
echo $row['two'];
echo "</td></tr>";
use either

Code: Select all

print("<tr><td>{$row['one']}</td><td>{$row['two']}</td></tr>");
or

Code: Select all

print('<tr><td>' . $row['one'] . '</td><td>' . $row['two'] . '</td></tr>');
;)

also, instead of all this garbage (IMHO):

Code: Select all

$host="localhost";
        $user="username";
        $pass="password";
        $database="databasename";
 
        if ( $connect = mysql_connect($host, $user, $pass) )
        {
            echo "Connection worked.<br />\n";
        }
        else
        {
            die("Could not connect: " . mysql_error());
        }
much easier to simply right:

Code: Select all

@mysql_connect(localhost,'user','pass') or die('Error Connecting: '.mysql_error());
just being picky, really, but cleaning up your code makes bugfixing much easier. ;)
Post Reply