Jump to content

Don't know if I'm connected to Database - no error messages displayed


bobafett24

Recommended Posts

Hi

I'm currently trying to get the hang of Php/MySQL. I can generate normal Hello World messages using Php and I can use the database to insert/select, etc data using the DOS screen.

Here's the code I'm using:

<?php
// login information

$db_host='localhost';
$db_database='test';
$db_username='root';
$db_password='orlaith';

// Connect
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection)
{
  die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select)
{
  die ("Could not select the database: <br />". mysql_error());
}

/ /Close the connection
mysql_close($connection);
?>

When I try to put in Php commands in the middle of this they don't output but when I comment everything out they appear fine. Can anyone help me here?

What could I be doing wrong? And how can I fix this?

Any help would be appreciated a lot.

To anyone that can help, thanks in advance
Link to comment
Share on other sites

Hi

The truth is I'm using an O'Reilly Tutorial. I'm trying to do all of the below:

It works fine in the Dos screen.

<?php
// Include our login information

$db_host='localhost';
$db_database='test';
$db_username='root';
$db_password='orlaith';

// Connect
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection)
{
  die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select)
{
  die ("Could not select the database: <br />". mysql_error());
}
// Assign the query
$query = "SELECT * FROM `books` NATURAL JOIN `authors`";
// Execute the query
$result = mysql_query( $query );
if (!$result)
{
  die ("Could not query the database: <br />". mysql_error());
}

// Fetch and display the results
while ($result_row = mysql_fetch_row(($result)))
{
      echo 'Title: '.$result_row[1] . '<br />';
      echo 'Author: '.$result_row[4] . '<br /> ';
      echo 'Pages: '.$result_row[2] . '<br /><br />';
}
/ /Close the connection
mysql_close($connection);
?>


Is there any way I can check that I'm connected. I nearly wish I could see an error message. At least I'd know where I was regarding this then. :(
Link to comment
Share on other sites

One thing I see, which might be causing some issues is the last comment at the bottom of the file.

[quote]
/ /Close the connection
[/quote]

You have a space after the first / - could that break the whole PHP script?

Other than that, I believe you could also do like this:

[code]
mysql_select_db($db_database) or die('Message.......');
[/code]

rather than doing:

[code]
$select_database = mysql_select_db($db_database);
if (!$database) { ..... }
[/code]

It's shorter and much cleaner.

[code]
<?php

    // login information
    $db_host='localhost';
    $db_database='test';
    $db_username='root';
    $db_password='orlaith';

    // Connect
$connection = mysql_connect( $db_host, $db_username, $db_password )
or die('Could not connect to the database: ' . mysql_error());

    // Select the database
    mysql_select_db($db_database)
    or die('Could not select the database: ' . mysql_error());

    echo "I am connected? This should, I believe, return the resource id: " . $connection;
   
    // Close the connection
    mysql_close($connection);
?>
[/code]

Try the above.
Link to comment
Share on other sites

Hi

Well thanks for the shortened code but it still doesn't work :(

When I comment out the database stuff I can print out the comment.

Am I accessing the database the right way?

When I use Dos, I type mysql -u root -p

I then type in the password orlaith

then use test

and then i use a select command that work fine.

Any idea on what goin wrong?
Link to comment
Share on other sites

Firstly, thanks for all the help man.

Do you think I should re-install some Apache myself? ???

Just one more point, when i for example change the database from test to test1, this should really cause an error coz no database called test1 exists but all I still get is a blank screen still :-\
Link to comment
Share on other sites

[quote author=bobafett24 link=topic=124438.msg515559#msg515559 date=1170012153]
Firstly, thanks for all the help man.

Do you think I should re-install some Apache myself? ???

Just one more point, when i for example change the database from test to test1, this should really cause an error coz no database called test1 exists but all I still get is a blank screen still :-\
[/quote]

You could try setting the debug and warning's to on in your php settings file. It kind of seems that you don't have mysql working, from PHP, and that you don't have any error/debug output so it's not going to display an error.
Link to comment
Share on other sites

Hi When I restart Apace I get the following error message.

Syntax error on line 134 of C:/Program Files/Apache Group/Apache2/conf/httpd.conf:
LoadModule takes two arguments, a module name and the name of a shared object file to load it from
Note the errors or messages above, and press the <ESC> key to exit.  20...

Is this causing the issue and what should I do to sort it out?

On line 134 of the httpd.conf file I have the following:

LoadModule php5_module C:\Program Files\PHP\php5apache2.dll

Is this incorrect?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.