943,192 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 904
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 24th, 2010
0

Cannot connect to MySQL from PHP. No error message is displayed

Expand Post »
Hi,

I am trying to connect to mysql from php Below is the sample code i used.

<?php
<?php
$username = "root";
$password = "pwd";
$hostname = "localhost";

$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
mysql_close($dbh);

?>


The problem here is I am not getting any error message. The page does not display anything. But when i comment the mysql_coonect() line i can see the prints and echo's. I tried checking if $dbh is valid and also tried putting the code in an exception block. But nothing helps. I have tried many variations of the above code to no effect.

Please help me to identify the problem.

I am able to execute php scripts which shows that my php install is fine.
I am also able to login to mysql db using (locaalhost, root,pwd) in command prompt and using mysql workbench.


Php version = php-5.2.13-win32-
MySql version = mysql-essential-5.1.48-win32



Thanks
Richtofen
Last edited by Richtofen345; Jul 24th, 2010 at 10:45 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richtofen345 is offline Offline
4 posts
since Jul 2010
Jul 24th, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
if you have two <?PHP commands at the start as shown, get rid of one of them and see what you get.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Jul 24th, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
Its a type. There is only one <?php command actually.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richtofen345 is offline Offline
4 posts
since Jul 2010
Jul 24th, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
Maybe the PHP configuration on the server running your script tells PHP not to display any errors (although I don't see any errors in your script.) Try adding the following line to the beginning of your script:
ini_set('display_errors', 1);
http://www.wallpaperama.com/forums/h...bled-t453.html
Last edited by d5e5; Jul 24th, 2010 at 3:56 pm.
Reputation Points: 153
Solved Threads: 140
Master Poster
d5e5 is offline Offline
735 posts
since Sep 2009
Jul 24th, 2010
1
Re: Cannot connect to MySQL from PHP. No error message is displayed
You can also add:
PHP Syntax (Toggle Plain Text)
  1. error_reporting (E_ALL);

Which is similar but a little different.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Aug 2nd, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
After adding the lines mentioned by d5e5 & Chrishea i can see the error message. Thanks both of you.
I am getting this error message
Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\loginValid.php on line 10

Looks like php cannot recognize my sql. My php install is php-5.2.13-win32-installer.msi.

After googling i found that i need to uncomment the following line from php.ini
;extension=php_mysql.dll

But i dont have the above line in my php.ini. It means something is missing even if i put the line i will not be able to get it to work.

I also do not have the extension_dir in my php home ( the line extension_dir is not there in php.ini)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richtofen345 is offline Offline
4 posts
since Jul 2010
Aug 3rd, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
Use the code below to connect to the datsbase. It looks like you dont have mysql_select_db("dataBase_Name",$con); in your code.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $con = mysql_connect("localhost","user_Name","passWord") or die("Check connection");
  4.  
  5. mysql_select_db("dataBase_Name",$con);
  6. ?>
Last edited by Chi-Town; Aug 3rd, 2010 at 3:16 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Chi-Town is offline Offline
5 posts
since Aug 2010
Aug 3rd, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
u r missing the mysql_select_db()
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ijazkhattak is offline Offline
5 posts
since Nov 2009
Aug 3rd, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
create a short test this will tell you errors atleast, unless it is infact your php not installed.
PHP Syntax (Toggle Plain Text)
  1. //connect to server
  2. $link = mysqli_connect('localhost', 'user', 'pass');
  3. if (!$link)
  4. {
  5. $error = 'Unable to connect to the database server.';
  6. include 'error.html.php';
  7. exit();
  8. }
  9.  
  10. //set database encoding
  11. if (!mysqli_set_charset($link, 'utf8'))
  12. {
  13. $output = 'Unable to set database connection encoding.';
  14. include 'output.html.php';
  15. exit();
  16. }
  17.  
  18. //select database
  19. if (!mysqli_select_db($link, 'tools'))
  20. {
  21. $error = 'Unable to locate the tools database.';
  22. include 'error.html.php';
  23. exit();
  24. }
Reputation Points: 10
Solved Threads: 1
Light Poster
atrueresistance is offline Offline
30 posts
since Aug 2010
Aug 3rd, 2010
0
Re: Cannot connect to MySQL from PHP. No error message is displayed
Your code looks fine.
It appears your php or mysql has not been installed correctly.
try using a differen connect function such as mysql_pconnect
and re-install your php or mysql.
f it is only that file just download that particular file and see if it works better.
perhaps try a different version of that file.
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Join my sql-urgent
Next Thread in PHP Forum Timeline: How insert image into mysql database and retrieve





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC