Jump to content

[SOLVED] localhost Databse


Recommended Posts

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?

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.
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.