LordOrange Posted November 24, 2007 Share Posted November 24, 2007 I've just set up a localhost with Apache. I've installed PHP and MYSQL. I downloaded a database manager and it connects to the database fine with username-root password-orange server-localhost but when I run a php script to connect: PHP Code: $con = mysql_connect("localhost","root","orange"); mysql_select_db("mysql", $con); $result = mysql_query("SELECT * FROM info"); while($row = mysql_fetch_array($result)) { echo "123 <br>"; echo $row['name']." ".$row['age']; echo "<br>"; } mysql_close($con); nothing happens. I am sure the database (mysql) and table in it (info) are correct aswell as the row names. Can anyone help me understand what I'm doing rong? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 24, 2007 Share Posted November 24, 2007 mysql_connect("localhost", "root", "orange") or die(mysql_error()); that might help Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 24, 2007 Author Share Posted November 24, 2007 still no no code working after connect line Quote Link to comment Share on other sites More sharing options...
revraz Posted November 24, 2007 Share Posted November 24, 2007 I think he meant that it would help if you were to get a connection error, since it would give you the error. Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 24, 2007 Author Share Posted November 24, 2007 But i'm not getting any errors Quote Link to comment Share on other sites More sharing options...
revraz Posted November 24, 2007 Share Posted November 24, 2007 Post your code exactly as you have it. Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 24, 2007 Author Share Posted November 24, 2007 <?php echo "Welcome ".$_POST['name'].". You have been giving off carbon dioxide for ".$_POST['age']." years."; $con = mysql_connect("localhost","root","orange") or die(msql_error()); echo "Test"; mysql_select_db("mysql", $con); $result = mysql_query("SELECT * FROM info"); while($row = mysql_fetch_array($result)) { echo "123 <br>"; echo $row['name']." ".$row['age']; echo "<br>"; } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2007 Share Posted November 24, 2007 Start with making sure the basics are working - First, mysql_error() is spelled wrong in the code you just changed. Does the echo "test"; show up? If not, php is probably not working. Turn on full php error reporting (display_errors and error_reporting) in php.ini to get php to help you. Stop and start your web server to get any changes made to php.ini to take effect. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 24, 2007 Share Posted November 24, 2007 Another basic thing, make sure the filename has a .php extension. Also, modify this line mysql_select_db("mysql", $con); and add the die statement mysql_select_db("mysql", $con) or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 24, 2007 Author Share Posted November 24, 2007 nope... still no errors and file is definately .php and I'm prety sure php is workin as the first echo line is working Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 mysql_error is mispelt on this line: $con = mysql_connect("localhost","root","orange") or die(msql_error()); change msql_error to mysql_error Do you still get a blank page? If you do then this makes me believe that the mysql extension is not enabled and thus when you use mysql_connect or any other mysql_* function your code dies. In order to confirm this create a new script called info.php with the following code: <?php phpinfo(); ?> Run info.php you should get a page full of info about PHP setup. Scroll down the page and look for a main heading called mysql. If you can't find a mysql heading then the mysql extension is not loaded and thus your code will not work. You will need to enable the mysql extension within the php.ini in order for your code to work. There is an FAQ here for enabling that extension however that is for Windows. If you are using *nix/Mac then you'll need to recompile PHP with the --with-mysql=[path/to/mysql/here] command. Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 24, 2007 Author Share Posted November 24, 2007 Once I've confugured the ini file like you said all my PHP scripts stop working Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 Once I've confugured the ini file like you said all my PHP scripts stop working Ugh! What did you do? If you did everything correctly then you should not have any problems. Also what http server do you have installed? Apache, IIS or something else and what OS? Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 25, 2007 Author Share Posted November 25, 2007 runnin MySQL, Windows XP I also reinstalled PHP including the extension (remembered I hadn't) and found all the line you said I needed and it diidn't work Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2007 Share Posted November 25, 2007 Stating that something "diidn't work" tells a forum no useful information to help you with. We only see exactly what you post. What is working? What do you see when you browse to a file? What have you done to troubleshoot what is or is not working on your system? Following the instructions in the link that wildteen88 gave in his post, as they apply to your setup, would not have stopped php from working or would have required you to reinstall php. The reason I emphasized "as they apply to your setup" is, if you completely overwrote your existing php.ini (which the instructions stated to do only if you don't have an existing php.ini) or you moved a file to a location where it had never been before, then yes, your system is probably going to stop working. If you cannot specifically list each thing you did, you are going to have to go back to step one - Make sure that the web server works. Get PHP working. Get the mysql extension enabled, which is what your original problem was (along with not checking your web server log for errors and turning on full php error reporting to get php to help you.) Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 25, 2007 Author Share Posted November 25, 2007 nothing was showing what so ever.... it's working now though... hav eno idea why it is now and wasn't just now Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2007 Share Posted November 25, 2007 BTW. The advice given at that link on coping various files around is out of date with the more simple current instructions that php.net gives. Short version of php.net installation instructions for PHP5 on Windows and Apache - To allow easier upgrades, all files should be left where they are at in the php installation folder. It is not necessary to copy any php core or extension files around to get anything to work. Add the path of your php installation folder (typically c:\php) to the Windows PATH statement. Restart your computer to get any changes made to the Windows PATH statement. Change Apache's httpd.conf per the php.net installation instructions, making sure that there is a PHPIniDir setting that points to where your php.ini is located, for example - PHPIniDir "c:/php" For php as an Apache module, the following lines need to be in your httpd.conf file (change existing lines when present or add these whole lines as needed) - Add this line where other LoadModule statements are at: LoadModule php5_module "c:/php/php5apache2_2.dll" Add this line (immediately below the LoadModule line): PHPIniDir "C:/php" Find and modify this line: DirectoryIndex index.html index.php Add this line where other AddType statements are at (parses .html files as php too): AddType application/x-httpd-php .php .html Stop and start your web server to get any changes made to httpd.conf to take effect. Modify the php.ini for the proper extension_dir = setting to where the extension dll's are located and uncomment the extension=php_mysql.dll line. Stop and start your web server to get any changes made to php.ini to take effect. When upgrading php versions, there are often new core settings and new default settings in the the php.ini-recommended, so you should start with it as your php.ini and modify it with your own custom settings from your existing php.ini. Basically, account for any differences between what you have now and what is in a new version's php.ini. I have found that using the Winmerge program makes finding and copying settings between an existing php.ini and a new one easy and invaluable - http://winmerge.org/ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2007 Share Posted November 25, 2007 nothing was showing what so ever.... it's working now though... hav eno idea why it is now and wasn't just now If you were making changes to php.ini or httpd.conf, it requires stopping and starting the web server to get any changes to take effect. Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 25, 2007 Author Share Posted November 25, 2007 It's working now.... don't now why or what I did to fix it but It still gives me an error not allowing me to connect the database..... My configuration settings in the php.ini file are definately correct now Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2007 Share Posted November 25, 2007 Post the actual error and the code with the line causing the error. Create a php script with the following in it and browse to it. <?php phpinfo(); ?> Near the top of the output there is a line that says what (if any) php.ini is being used. Make sure it is the one you are making changes to. Also, as has already been said in this thread, find the mysql section in the output to make sure the mysql extension is getting loaded. Quote Link to comment Share on other sites More sharing options...
LordOrange Posted November 25, 2007 Author Share Posted November 25, 2007 Can't connect to MySQL server on 'localhost' (10061) <?php echo "Welcome ".$_POST['name'].". You have been giving off carbon dioxide for ".$_POST['age']." years."; $con = mysql_connect("localhost","root","orange") or die(mysql_error()); echo "Test"; mysql_select_db("mysql", $con) or die(mysql_error()); $result = mysql_query("SELECT * FROM info"); while($row = mysql_fetch_array($result)) { echo "123 <br>"; echo $row['name']." ".$row['age']; echo "<br>"; } mysql_close($con); ?> The error didn't give a line but I'm pretty sure it's on the connect line (line 4) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2007 Share Posted November 25, 2007 From the mysql manual - The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, the network port you specified is the one configured on the server, and that the TCP/IP port you are using has not been blocked by a firewall or port blocking service. 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.