We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,324 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP to Mysql Problem

hi guys, please help me out on this issue.

i had this code to connect to mysql using php but there is no any message displayed.

nothing is displayed on the screen, whether the connection is successful or there is an error.

any ideas?

i can connect to mysql using command prompt without a problem, but through this code below nothing is displayed on the screen.

any help is greatly appreciated.

<html>
<body>


<?php

$link = mysql_connect('localhost', 'my_username', 'my_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

?>

</body>
</html> 
5
Contributors
7
Replies
4 Days
Discussion Span
9 Months Ago
Last Updated
8
Views
Question
Answered
cguan_77
Nearly a Posting Virtuoso
1,361 posts since Apr 2007
Reputation Points: 19
Solved Threads: 117
Skill Endorsements: 0

You didn't select the database you are going to use.. Do it like this..

<?php
$link=mysql_connect("localhost","my_username", "my_password");
if(!$link)
{
    die("Could not connect:" .mysql_error());
}

mysql_select_db("name_of_your_database"); //here goes the database selection
echo "Connected successfully!";
?>

Hope this helped.. :))))

xjshiya
Light Poster
28 posts since Aug 2012
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

Selecting the database is optional, not required. Anyway, check your error log, to see if perhaps the mysql extension is not installed, or perhaps some other configuration errors. The code the OP provided is correct.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

check sql extension care fully so it is good habit.

ankitaray
Light Poster
48 posts since Aug 2012
Reputation Points: 6
Solved Threads: 1
Skill Endorsements: 0

hi guys, thanks for your replies.

there's nothing on the error log as well, I was hoping that there would be an error so there's something i could start with but i got no clue.

i'm just starting on this please bear with me, the extension we're talking about is it
the extension_dir on php.ini?

if that's the extension_dir on php.ini, i had set that one already.

i had checked the php info, i could see that mysql is loaded.

i'm really stucked, any ideas? thanks :)

cguan_77
Nearly a Posting Virtuoso
1,361 posts since Apr 2007
Reputation Points: 19
Solved Threads: 117
Skill Endorsements: 0

any ideas?

Sorry. The code is fine, so it has to be a configuration thing, but that's difficult to guess.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

have u tried using double quotes instead of single quote.

vaayaa1
Newbie Poster
7 posts since Aug 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

hi guys, thanks for all your help.
i had installed WAMP and it works fine.
it's a bit troublesome installing the package one by one.

installing WAMP does everything. :) thanks again..

cguan_77
Nearly a Posting Virtuoso
1,361 posts since Apr 2007
Reputation Points: 19
Solved Threads: 117
Skill Endorsements: 0
Question Answered as of 9 Months Ago by pritaeas, xjshiya, vaayaa1 and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0905 seconds using 2.87MB