Forum Moderators: coopster

Message Too Old, No Replies

Trouble connecting to MySQL with PHP

         

MapleStory

5:18 am on Apr 13, 2010 (gmt 0)

10+ Year Member



I keep getting this when I go onto 127.0.0.1


Warning: mysql_connect() [function.mysql-connect]: in C:\wamp\www\config\database.php on line 41

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in C:\wamp\www\config\database.php on line 42

I know that i can't connect and that theres something wrong with my database.php, but I'm not sure whats wrong with it. Please help me.

MapleStory

5:20 am on Apr 13, 2010 (gmt 0)

10+ Year Member



im currently using a php for interface and WAMP. sorry i forgot to add. If you need more details to answer my question jsut leave a reply. I need to get this problem fixed asap

Matthew1980

7:00 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there MapleStory,

Welcome to the forum ;-p

Could you post the lines of code that relate to your issue please ie:-

$user = "Myuser";
$host = "Localhost";//Try not to use 127.0.0.1 Localhost is preferred
$pass = "Pass";

$conn = mysql_connect($host,$user,$pass);
mysql_select_db("your_db", $conn);

Thats just an example :)

Try to exemplify though as you don't want to show everyone your login detail ;-p

Then we can sort you out,

Cheers,
MRb

MapleStory

10:59 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



localhost doesnt work for some reason...and yea sure

MapleStory

11:08 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



its really long...but basiclly its for a game called maplestory. And here is the part i changed (the odinms and 127.0.0.1

Database Username - Usually "root"..$user = "root";..// Database Password - Usually "root". If you don`t have a password, or are not sure, leave it blank...$pass = "";..// Database Name - Usually "odinms"..$database = "odinms";..// What is the server`s Wan IP..$serverip = "127.0.0.1:3306"

MapleStory

11:42 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



but now 120.0.0.7 doesnt work anymore....

brotherhood of LAN

11:47 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db("your_db", $conn) or die(mysql_error());

Try using that alteration to see if MySQL gives you a specific error, or whether it's connecting at all.

MapleStory

1:07 am on Apr 14, 2010 (gmt 0)

10+ Year Member



nvm the 127.0.0.1 works and here is my entire database.php
i changed the localhost part to 127.0.0.1 and the testv75 to odinms
<?php
/*
Copyright (C) 2009 Murad <Murawd>
Josh L. <Josho192837>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

if(basename($_SERVER["PHP_SELF"]) == "database.php"){
die("403 - Access Forbidden");
}

/* MySQL Connection Information */
// Where is MySQL Hosted? If you aren`t sure, just leave it as "localhost"
$host = "127.0.0.1";
// Database Username - Usually "root"
$user = "root";
// Database Password - Usually "root". If you don`t have a password, or are not sure, leave it blank.
$pass = "";
// Database Name - Usually "odinms"
$database = "odinms";
// What is the server`s Wan IP
$serverip = "127.0.0.1";
// What is your server`s log in port - Don`t change if you aren`t sure.
$loginport = "7575";
// What is your server`s world port - Don`t change if you aren`t sure.
$worldport = "8484";

/* Don`t touch. */
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($database, $conn) or die(mysql_error());

?>

MapleStory

1:10 am on Apr 14, 2010 (gmt 0)

10+ Year Member



When I tried to change the bottom part as brotherhood Lan told me i got this... Unknown database 'your_db'

MapleStory

1:15 am on Apr 14, 2010 (gmt 0)

10+ Year Member



Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\config\properties.php on line 26

Warning: include(/header.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\index1.php on line 55

Warning: include() [function.include]: Failed opening '/header.php' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\index1.php on line 55
Table 'odinms.cype_news' doesn't exist



this is the thing i get when i tried my code that i showed up there (before the unkown database your_db error)



MapleStory

1:23 am on Apr 14, 2010 (gmt 0)

10+ Year Member



Warning: mysql_connect() [function.mysql-connect]: in C:\wamp\www\config\database.php on line 41

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in C:\wamp\www\config\database.php on line 42

this is the error i get when i use this code

<?php
/*
Copyright (C) 2009 Murad <Murawd>
Josh L. <Josho192837>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

if(basename($_SERVER["PHP_SELF"]) == "database.php"){
die("403 - Access Forbidden");
}

/* MySQL Connection Information */
// Where is MySQL Hosted? If you aren`t sure, just leave it as "localhost"
$host = "localhost";
// Database Username - Usually "root"
$user = "root";
// Database Password - Usually "root". If you don`t have a password, or are not sure, leave it blank.
$pass = "";
// Database Name - Usually "odinms"
$database = "odinms";
// What is the server`s Wan IP
$serverip = "localhost";
// What is your server`s log in port - Don`t change if you aren`t sure.
$loginport = "7575";
// What is your server`s world port - Don`t change if you aren`t sure.
$worldport = "8484";

/* Don`t touch. */
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($database, $conn) or die(mysql_error());

?>

Matthew1980

7:19 am on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there MapleStory,

As Brotherhood of lan stated:-

$conn = mysql_connect($host,$user,$pass)or die(mysql_error());
$db = mysql_select_db($database, $conn) or die(mysql_error());


Attach an error handler to the end of the connect query as there seems to be an issue with what the function is returning. Get a connection, then select the DB, error handlers at this point are handy, but once the connection is established, and you are going public, remove the or die(... part :) Less info on display for hackers then..

Also:-


// Database Username - Usually "root"
$user = "root";
// Database Password - Usually "root". If you don`t have a password, or are not sure, leave it blank.
$pass = "";


Bad idea to had this as standard connection info, root access should NEVER be used when going public, because if you do and you dont protect your code queries, you can potentially let hackers inject code that will and can delete your databases. Just thought I would let you know.

Oh, and one final point: Make sure that the data you are after/requesting is actually spelt as it is in the DB, table names, and database names, field names etc, as this catches many people out (though they will never admit it :))

As you are using wamp, use the phpmyadmin utility and set yourself a new user account up, using the privelidge tab. Once this is set up you can use this new username/password combo for all your testing/development work, but I cannot stress enough how much of a good habit this will be in future for you if you get into the habit now. :)

Cheers,
MRb

MapleStory

9:22 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



and as for the server ip do i do localhost or 127.0.0.1? because the [localhost...] doesnt work.

Matthew1980

10:05 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there maplestory,

Right to answer the question, log into your phpmyadmin, on the main page right at the top, just above the databases tab, the server name is displayed. 99% of the time a wamp install *by default* installs all the programs and uses localhost as the default server name.

If localhost isn't displayed, use whatever is displayed next to the word server.

Hopefully this will answer your question :)

Cheers,
MRb

MapleStory

10:07 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



when i click on the phpmyadmin in wamp it doesnt take me there...Its like it doesnt exist.

MapleStory

10:12 pm on Apr 14, 2010 (gmt 0)

10+ Year Member




$conn = mysql_connect($host,$user,$pass)or die(mysql_error());
$db = mysql_select_db($database, $conn) or die(mysql_error());

The code you gave me above helped get the connection, but now I still have this error.

Warning: mysql_connect() [function.mysql-connect]: in C:\wamp\www\config\database.php on line 41

MapleStory

10:24 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



Now i changed it to this with the semi-collon...

$conn = mysql_connect($host,$user,$pass);or die(mysql_error());
$db = mysql_select_db($database, $conn) or die(mysql_error());


and I get this error...

Parse error: parse error in C:\wamp\www\config\database.php on line 41