usman07 Posted May 13, 2012 Share Posted May 13, 2012 for some reason my database will not connect, here's the code: <?php require("mstr_ref2.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } // Opens a connection to a MySQL server $connection=mysql_connect (localhost, $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } // Set the active MySQL database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ('Can\'t use db : ' . mysql_error()); } // Select all the rows in the markers table $query = "SELECT * FROM markers WHERE 1"; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } header("Content-type: text/xml"); // Start XML file, echo parent node echo '<markers>'; // Iterate through the rows, printing XML nodes for each while ($row = @mysql_fetch_assoc($result)){ // ADD TO XML DOCUMENT NODE echo '<marker '; echo 'name="' . parseToXML($row['name']) . '" '; echo 'address="' . parseToXML($row['address']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo 'type="' . $row['type'] . '" '; echo '/>'; } // End XML file echo '</markers>'; ?> I think there is something wrong with this line of code. Where it says 'locat host' im not too sure about that because im not connecting to a local host, so what can i put in there instead?i think that is the problem. $connection=mysql_connect (localhost, $username, $password); This is the error I get: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'a2221438'@'localhost' (using password: NO) in /home/a2221438/public_html/map.php on line 44 Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/ Share on other sites More sharing options...
usman07 Posted May 13, 2012 Author Share Posted May 13, 2012 This is the mstr_ref2.php <?php define('__HOST', ''); //localhost = database host name (normaly this won't need changing) define('__USER', ''); //xxx = database username define('__PASS', ''); //xxx = database password define('__DEF_DB', ''); //xxx = database name $con = mysql_connect(__HOST, __USER, __PASS) or die(mysql_error()); $db = mysql_select_db(__DEF_DB, $con) or die (mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345141 Share on other sites More sharing options...
Barand Posted May 13, 2012 Share Posted May 13, 2012 Instead of "localhost" use either the name or IP address of the database server Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345146 Share on other sites More sharing options...
Pikachu2000 Posted May 13, 2012 Share Posted May 13, 2012 And since you're include()ing the file that makes a database connection, it's quite unlikely you need to create another connection in the script. Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345162 Share on other sites More sharing options...
usman07 Posted May 14, 2012 Author Share Posted May 14, 2012 I'v changed it to this: the website is the server // Opens a connection to a MySQL server $connection=mysql_connect ("mysql10.000webhost.com", $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } But it still comes up with this error: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'a2221438'@'10.1.1.33' (using password: NO) in /home/a2221438/public_html/map.php on line 44 Free Web Hosting Not connected : Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345282 Share on other sites More sharing options...
usman07 Posted May 14, 2012 Author Share Posted May 14, 2012 Ok iv changed the mstr_ref2.php to this: <? $username=""; $password=""; $database=""; ?> And this is in the map.php page: // Opens a connection to a MySQL server $connection=mysql_connect (localhost, $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } This is the error I receive: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'a2221438_usman'@'localhost' (using password: YES) in /home/a2221438/public_html/map.php on line 44 Free Web Hosting Not connected : Access denied for user 'a2221438_usman'@'localhost' (using password: YES) But when I change the map.php page code to this: // Opens a connection to a MySQL server $connection=mysql_connect ("mysql10.000webhost.com", $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } It them comes up with this error message: This page contains the following errors: error on line 56 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error. This is the line 56, what it wrong with it for the error message to appear?Thanks $query = "SELECT * FROM markers WHERE 1"; Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345289 Share on other sites More sharing options...
usman07 Posted May 14, 2012 Author Share Posted May 14, 2012 I honestly dont't understand this, any help is appreciated,heres the full code of the page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Mumtaz Properties</title> <link rel="stylesheet" href="cutouts/style.css"/> </head> <body> <!--Main Div Tag--> <div id="wrapper"> <div id="header"> <div id="logo"><a href="index.php"><img src="cutouts/Homepage/logo.png" alt=""/></a></div> </div> <div id="navigation"> <a id="Home" href="index.php" title="home"><span>home</span></a> <a id="Sale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="Rent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="Contact" href="contact.html" title="contact us"><span>contact us</span></a> </div> <div id="main"> <center><div id="pheader"><img src="cutouts/maps/pheader.png" alt=""/></div></center> <?php require("mstr_ref2.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } // Opens a connection to a MySQL server $connection=mysql_connect ("mysql10.000webhost.com", $username, $password); if (!$connection) { die('Not connected : ' . mysql_error()); } // Set the active MySQL database $db_selected = mysql_select_db($database, $connection); if (!$db_selected) { die ('Can\'t use db : ' . mysql_error()); } // Select all the rows in the markers table $query = "SELECT * FROM markers WHERE 1"; $result = mysql_query($query); if (!$result) { die('Invalid query: ' . mysql_error()); } header("Content-type: text/xml"); // Start XML file, echo parent node echo '<markers>'; // Iterate through the rows, printing XML nodes for each while ($row = @mysql_fetch_assoc($result)){ // ADD TO XML DOCUMENT NODE echo '<marker '; echo 'name="' . parseToXML($row['name']) . '" '; echo 'address="' . parseToXML($row['address']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo 'type="' . $row['type'] . '" '; echo '/>'; } // End XML file echo '</markers>'; ?> </div> <div id="footer"> <a id="fHome" href="index.php" title="homepage"><span>homepage</span></a> <a id="fSale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="fRent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="fContact" href="contact.html" title="contact us"><span>contact us</span></a> </div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345296 Share on other sites More sharing options...
DavidAM Posted May 14, 2012 Share Posted May 14, 2012 Access denied for user 'XXXXXXXX'@'localhost' (using password: YES) Usually means that that user does not have a login for the database server; OR The password provided is not the correct password for that user. Extra content at the end of the document I have never seen this message before. However, about 6 lines down you have: header("Content-type: text/xml"); You can NOT send a header after sending ANY content to the browser. The header would have to be sent BEFORE even the DOCTYPE is sent. I don't have any experience serving XML pages, but it would seem to me that your DOCTYPE, your <HTML> tag and this header all contradict each other. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> header("Content-type: text/xml"); Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345431 Share on other sites More sharing options...
usman07 Posted May 14, 2012 Author Share Posted May 14, 2012 Thanks mate, and yeah u was right it was the header that i had to take out. Link to comment https://forums.phpfreaks.com/topic/262480-database-wont-connect/#findComment-1345434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.