bobafett24 Posted January 28, 2007 Share Posted January 28, 2007 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 connectionmysql_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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 28, 2007 Share Posted January 28, 2007 What kind of commands are you trying to put? If you are just trying to display something to the screen make sure you are using echo or print. Quote Link to comment Share on other sites More sharing options...
bobafett24 Posted January 28, 2007 Author Share Posted January 28, 2007 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 resultswhile ($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 connectionmysql_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. :( Quote Link to comment Share on other sites More sharing options...
irken Posted January 28, 2007 Share Posted January 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
bobafett24 Posted January 28, 2007 Author Share Posted January 28, 2007 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 orlaiththen use testand then i use a select command that work fine.Any idea on what goin wrong? Quote Link to comment Share on other sites More sharing options...
irken Posted January 28, 2007 Share Posted January 28, 2007 That's kinda strange. Hmm.. let me just finish setting up PHP on apache and I'll try this. Just looking at it I don't see anything wrong. Quote Link to comment Share on other sites More sharing options...
bobafett24 Posted January 28, 2007 Author Share Posted January 28, 2007 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 Link to comment Share on other sites More sharing options...
irken Posted January 28, 2007 Share Posted January 28, 2007 [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. Quote Link to comment Share on other sites More sharing options...
bobafett24 Posted January 29, 2007 Author Share Posted January 29, 2007 Hi manSo how can I go about doing that?Sorry to sound like such a fool, but I'm sorta new to this. Quote Link to comment Share on other sites More sharing options...
bobafett24 Posted January 29, 2007 Author Share Posted January 29, 2007 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 fromNote 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.dllIs this incorrect? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.