Page 1 of 1
hard time connecting to database
Posted: Sun May 01, 2005 10:39 pm
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
Posting Code in the Forums
Posted: Sun May 01, 2005 10:57 pm
by John Cartwright
Look at your code with syntax highlighting.
Plus your
statements should look like this
Posted: Sun May 01, 2005 11:03 pm
by dilirium
Jcart wrote:Look at your code with syntax highlighting.
Plus your
statements should look like this
Still no error, just blank

Posted: Mon May 02, 2005 6:22 am
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>
Posted: Mon May 02, 2005 8:48 am
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.
Posted: Mon May 02, 2005 9:49 am
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

Re: hard time connecting to database
Posted: Mon May 02, 2005 10:24 am
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.
Posted: Mon May 02, 2005 11:28 am
by timvw
i just tested what the ; or die would do
Parse error: parse error, unexpected T_LOGICAL_OR
Posted: Mon May 02, 2005 5:26 pm
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!!)
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.
