jbashir Posted August 26, 2006 Share Posted August 26, 2006 I am having problem in making database connection with mysql database. I have searched the web and my code looks exactly the same as many have written. But still there is no success. Can someone look into it and guide me where I am wrong. Here is the code: $dbhost = 'localhost:3306'; $dbuser = 'testuser'; $dbpass = 'testpass'; $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error()); $dbname = 'testdb'; mysql_select_db($dbname); I am using Apache 2.2. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/ Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 $dbhost = 'localhost:3306';needs to just be$dbhost = 'localhost';regardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80716 Share on other sites More sharing options...
jbashir Posted August 26, 2006 Author Share Posted August 26, 2006 LiamIn the start I was doing it the you are telling:$dbhost = 'localhost';and I had this problem. And then someone suggested it this way (as now):$dbhost = 'localhost:3306';and still there is the same problem.Joseph Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80719 Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 are you sure mysql is running? dont you even get an error?that is the way to specify a port but should be needed to be honest.. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80720 Share on other sites More sharing options...
jbashir Posted August 26, 2006 Author Share Posted August 26, 2006 Yes mysql is running. And there are no errors even. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80727 Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 so it would seem to be connecting then.. what makes you think/know that it isnt? could just be somthign wrong with code you typed try this[code]$dbhost = 'localhost:3306'; $dbuser = 'testuser'; $dbpass = 'testpass'; $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error()); $dbname = 'testdb'; mysql_select_db($dbname); $query=mysql_query('show tables') or die(mysql_error());[/code]if you get no error then it is connected and if there is a problem then it should tell us it. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80730 Share on other sites More sharing options...
wildteen88 Posted August 26, 2006 Share Posted August 26, 2006 Remove the @ symbol from infront of mysql_connect to retrieve the PHP error, rather than the MySQL error being returned by mysql_error.Also I have closed your other thread in the PHP SQL forum. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80735 Share on other sites More sharing options...
jbashir Posted August 26, 2006 Author Share Posted August 26, 2006 LiamI have used your code but still the same results.I have also remove @ symbol as 'wildteen88' has just told, but still it is not showing any results, not even errors. All am getting is in the access.log file (Apache2.2/logs/access.log) is:192.168.0.21 - - [26/Aug/2006:19:30:26 +0500] "GET /test.php HTTP/1.1" 200 - Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80745 Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 what about running this code[code]<?phpinclude('../opendb.php');$result=mysql_query("SHOW TABLES") or die(mysql_error());while ($query_data = mysql_fetch_row($result)) { echo $query_data[0]."<br>";}?>[/code]hoiw come your looking at the apache access log? that wouldnt show a connection to Mysql server as the connection is from phpsee if the code abobe lists ok.. if it does it should list all tables store in your database called testdb Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80752 Share on other sites More sharing options...
wildteen88 Posted August 26, 2006 Share Posted August 26, 2006 If you are getting no errors then its working. Is that your full code. If its then that code wont display anythink. All it does is connect and select the database thats it. It wont return anythink. The only time it'll return something is when you start to query the database. Like so:[code=php:0]<?php$dbhost = 'localhost:3306'; // this is fine, as '':3306 specifies the port number MySQL is running on$dbuser = 'testuser';$dbpass = 'testpass';$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error());$dbname = 'testdb';mysql_select_db($dbname); // now query the database$sql = "SELECT * FROM table_name";$result = mysql_query($sql, $conn) or die('Unable to perform query - ' . $sql . ' - ' . mysql_error());// start the table:echo '<table>';// now display the results:while($row = mysql_fetch_assoc($result)){ echo '<tr><td>'; echo implode('</td><td>', $result); echo '</td></tr>';}// close the table:echo '</table>';[/code]Now change [i]table_name[/i] from the SQL query ($sql) to the table that is in the database (testdb).It'll now run the query and display everthing that is in that table. Quote Link to comment https://forums.phpfreaks.com/topic/18721-problem-in-database-connection-phpmysqlapache-on-windows/#findComment-80753 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.