excel11 Posted April 16, 2007 Share Posted April 16, 2007 Very new to this. I am just trying to get the basics of php now. I have programmed with ASP so not new to concepts of programming. Any way I can't seem to get any errors or anything displayed on the screen when trying to connect to the mysql database I can connect to the mysql db by using the command line with no problems. But when issuing the below code I get nothing on the screen. No errors, nothing. In asp if your code can't do something IE, will prompt you with some time of error. Can't php do this as well. Do I have to configure my apache server somehow. <html> <body> <?php $username="excel11"; $password="asdf"; $hostname="localhost"; $database="my_db"; mysql_connect($hostname,$username,$password); mysql_select_db($database); $query="Select * from users"; echo $query; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 16, 2007 Share Posted April 16, 2007 try something like <?php $con = mysql_connect("$hostname", "$username", "$password") or die(mysql_error()); mysql_select_db('$database', $con) or die(mysql_error()); $query="Select * from users"; echo $query; ?> the or die(mysql_error()); will display an error message if it can't connect Quote Link to comment Share on other sites More sharing options...
excel11 Posted April 16, 2007 Author Share Posted April 16, 2007 Still nothing. The page is completly blank. This is the output from the command line to indicate something is within the db. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.37-community-nt MySQL Community Edition (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use my_db Database changed mysql> select * from users; +---------+-------+------+ | fname | lname | age | +---------+-------+------+ | Michael | Duff | 28 | +---------+-------+------+ 1 row in set (0.16 sec) Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 16, 2007 Share Posted April 16, 2007 change $query="Select * from users"; to $query=mysql_query("SELECT * FROM users"); Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 Are you trying to echo the actual query string, or the results of the query? If you want the query results to be displayed, you will need to actually run the query with mysql_query(): <?php $con = mysql_connect("$hostname", "$username", "$password") or die(mysql_error()); mysql_select_db('$database', $con) or die(mysql_error()); $query="Select * from users"; $result = mysql_query($query); if($result) { while($row = mysql_fetch_array($result)){ $fname = $row["fname"]; $lname = $row['lname']; $age = $row['age']; echo "Name: ".$fname." ".$lname.", ".$age."<br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
excel11 Posted April 17, 2007 Author Share Posted April 17, 2007 I don't know what could be the possible cause for nothing to be displayed on the screen but I still get nothing. If the connect statement can't connect you are supposed to be issued the mysql_error() message, but nothing. Just a blank page. It doesn't seem that this is a normal problem. Was there something I forgot to install with PHP? Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 17, 2007 Share Posted April 17, 2007 just to check.. if you've used what heyray2 said, have you switched the variables with ones from your table? $fname = $row["fname"]; $lname = $row['lname']; $age = $row['age']; echo "Name: ".$fname." ".$lname.", ".$age."<br />"; fname, lname and age may not be columns in your table... and because you have no error message for the echo statement, if it can't find any information for the set variables it will just be blank. are you sure that 'users' is your table name? it's case sensative.. so might be Users, or USERS etc.. Quote Link to comment Share on other sites More sharing options...
excel11 Posted April 18, 2007 Author Share Posted April 18, 2007 Solved!!!!!! I checked Apache error logs and found the following: Call to undefined function mysql_connect() Found out that PHP5+ doesn't come with MySQL installed. You have to manually configure this. Create Directory within your php directory such as: c:\Program Files\PHP\ext Within your php.ini file edit the line: extension_dir with extension_dir= "c:\Program Files\PHP\ext" Make sure that you have the following files php_mysql.dll and libmysql.dll on your computer else download them here: http://dev.mysql.com/downloads/connector/php/ If you have them on your computer make sure that you copy them into the above directory that you created. Everything should work now! I appreciate all the help that you guys gave me and hopefully now I can proceed in my learning php. 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.