chaddsuk Posted January 8, 2008 Share Posted January 8, 2008 Hi im new to PHP and am trying to connect to my MYSQL which is setup on my local machine using apache server. I am using the following code to try and connect but all i seem to get is a blank page... <?php $dbhost = 'localhost'; $dbuser = 'php'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'chadd'; mysql_select_db($dbname); echo "connection made"; ?> anyone any idea what im doing wrong? cheers chris Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/ Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Does this work? <?php echo "connection made"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433736 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 hi yes that works fine thanks Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433737 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Run a phpinfo() and see if you have mysql support installed. Check your error log for any errors. Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433738 Share on other sites More sharing options...
chronister Posted January 8, 2008 Share Posted January 8, 2008 <?php $dbhost = 'localhost'; $dbuser = 'php'; $dbpass = '*****'; $dbname = 'chadd'; function connectdb($host, $user, $pass, $db) { mysql_connect($host, $user, $pass); mysql_select_db($db) or die('Unable to select .com database!'); echo 'Successful Connection'; } connectdb($dbhost, $dbuser,$dbpass,$db); ?> Not sure why yours won't connect.. .try this one Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433739 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 put the following as the first line of php code and see if any more errors are printed: ini_set('display_errors','on'); Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433740 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 thanks for your help guys , my phpinfo() doesnt even mention mysql, does this mean its not installed correctly for mysql? cheers chris Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433742 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 Installation instructions can be found at http://us.php.net/mysql Basically, remove the ';' before the following line in your php.ini file: extension=php_mysql.dll oh...and don't forget to restart apache Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433746 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 Thanks for all your help guys, reinstalled PHP with teh mysql ext enabled and it works fine now...... Well i still cant connect to my database but i assume that isnt a php issue any mysql experts out there? lol chris Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433792 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 First...what OS/MySql version are you working with. Second...did you follow any instructions when setting it up? If so, please link. Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433796 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Did you setup users in MySQL? Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433799 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 hi im using MySQL 5.0.45, i added the user correctly and can log in fine through command prompt but just dont seem to be able to connect through PHP? Thanks for your help guys, this is a really great forum, much better than the others ive been on Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433807 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 what is the output from this script: <?php ini_set('display_errors','on'); $dbhost = 'localhost'; $dbuser = 'php'; $dbpass = '*****'; $dbname = 'chadd'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); mysql_select_db($dbname) or die('Error selecting database'); echo "connection made"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433813 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10050) in C:\Apache\htdocs\new.php on line 8 Error connecting to mysql Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433819 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 Again...which OS? Windows, Linux, etc? If Windows, try running the MySQL Instance Config Wizard again and make sure TCP/IP Networking is enabled. Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433829 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 Thanks for your reply, TCP/IP is running (im using windows xp) cant get my head around this one! Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433832 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 hi thanks for your reply, im running windows XP and TCP/IP is running Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433833 Share on other sites More sharing options...
chaddsuk Posted January 8, 2008 Author Share Posted January 8, 2008 hmm seems to have fixed itself, not sure how though!...thanks for all your help guys, really apreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433839 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 It was probably a firewall issue. Glad it works now though. Quote Link to comment https://forums.phpfreaks.com/topic/85048-newbie-help-please-connecting-to-my-sql/#findComment-433840 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.