Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,162 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,876 people online right now. Registration is fast and FREE... Join Now!




how to connect php and mysql

 
Reply to this topicStart new topic

how to connect php and mysql

linzhiyi
1 Jan, 2008 - 11:22 PM
Post #1

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
i am a beginner in php. i have created user and use grant command to give user rights to access mysql, why i still cannot connect php with mysql. please help me.
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: How To Connect Php And Mysql
2 Jan, 2008 - 07:33 AM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
you have to connect to db before you can use it. this code does that for u just add your info. there are 2 ways to do this. you can either paste this code on every page you need db connection or save it in a separate php file and include it on your other pages with this line: require_once($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');//assuming you name your php db_connect


CODE

<?php

$database[dbserver]="localhost";//most of the time this is localhost if u get an error check with your hosting co
$database[dbuser]="user";//username for db
$database[dbname]="dbname";//db name
$database[dbpass]="pass";//password

$connect = mysql_connect($database['dbserver'], $database['dbuser'], $database['dbpass']);

$select= mysql_select_db($database['dbname']);
?>

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How To Connect Php And Mysql
2 Jan, 2008 - 07:38 AM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
In the future linzhiyi, it is much easier for us to diagnose problems if you show us the code you are using.
We cannot determine the problem with your PHP without seeing it.
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: How To Connect Php And Mysql
2 Jan, 2008 - 11:13 AM
Post #4

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 214



Thanked: 3 times
Dream Kudos: 50
My Contributions
PHP questions should be posted in the PHP forum, no?
User is offlineProfile CardPM
+Quote Post

linzhiyi
RE: How To Connect Php And Mysql
4 Jan, 2008 - 11:48 PM
Post #5

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
i have tried to use your code, it didn't show error, but it showed nothing.
also i try to use another code
<?php
$link = mysqli_connect("localhost", "ruby", "123456","test");
echo '<br/>';
if($link){
echo 'connect successful';
mysqli_close($link);
}
else
echo'connect fail';
?>
it showed nothing at all, even not error. i feel very confused. please help me.
thank you.
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: How To Connect Php And Mysql
5 Jan, 2008 - 01:11 PM
Post #6

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 214



Thanked: 3 times
Dream Kudos: 50
My Contributions
If you ask your PHP questions in the PHP forum you will probably get more help.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Connect Php And Mysql
5 Jan, 2008 - 05:38 PM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Moved
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: How To Connect Php And Mysql
5 Jan, 2008 - 08:02 PM
Post #8

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
well the code i posted isnt supposed to show anything. all it does is connect to ur db. there could really be a lot of reasons for your problem. did u create the test db to connect to? is ur php file extension .php? is ur server setup for .php extension? sometimes it can be .php4 or php5. if ur running php and sql on ur personal computer, i can see a number of problems that can happen there too. i think mysqli_ is for php5 so if ur running php4 that wont work. maybe if u can give more details some 1 will be able to figure this 1 out.
User is offlineProfile CardPM
+Quote Post

linzhiyi
RE: How To Connect Php And Mysql
5 Jan, 2008 - 08:23 PM
Post #9

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
thank you for your help. after changing a little of your code, it shows that Cant connect to databaseUnable to select database. why? can you tell me. thank you.
below is what i have used changing from your code.
<?php
$connect = mysql_connect($database['linux3'], $database

['zongyan.huang'], $database['zongyan.huang']);

if(!$connect)
{
echo "Cant connect to database";
}
mysql_select_db($database['Book']) || die("Unable to

select database");
?>

User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: How To Connect Php And Mysql
5 Jan, 2008 - 08:53 PM
Post #10

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
ok heres what u do follow these steps exactly.

1 create a db name it test set username for it testuser and pass testpass
2 paste this code into index.php file do not change any code paste it as it is the only thing you can change is localhost to whatever it is on ur server

CODE

$link = mysql_connect('localhost', 'testuser', 'testpass')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('test') or die('Could not select database');


3 upload and call ur index.php
now if u get an error ull know what it is

This post has been edited by dontKnowJava: 5 Jan, 2008 - 08:54 PM
User is offlineProfile CardPM
+Quote Post

linzhiyi
RE: How To Connect Php And Mysql
5 Jan, 2008 - 09:56 PM
Post #11

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
thank you very much. i have connected scccessfully.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:53PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month