Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 php connect mysql~

views
     
TSlucashii
post Jan 28 2006, 08:53 AM, updated 20y ago

Getting Started
**
Junior Member
288 posts

Joined: Nov 2004
From: msia/uk


I am new toward php and i have just install wamp trying out the online tutorial~I came across this script which i just cant get it to work...with this script it shud add the tables and fields which could be access in myphpadmin right?Thanks to pros here in advance~

<?
$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>

alvinz_c
post Jan 28 2006, 09:55 AM

Casual
***
Junior Member
407 posts

Joined: Oct 2004
QUOTE(lucashii @ Jan 28 2006, 08:53 AM)
I am new toward php and i have just install wamp trying out the online tutorial~I came across this script which i just cant get it to work...with this script it shud add the tables and fields which could be access in myphpadmin right?Thanks to pros here in advance~

<?
$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>
*
Hi, try change this

mysql_connect("localhost",$user,$password);

you can use mysql_error() for detail of error.

Hope this helps...

se7en
post Jan 28 2006, 12:45 PM

resistance is futile
Group Icon
Admin
1,806 posts

Joined: Jan 2003
From: Captain's Cabin, Black Pearl

it should be mysql_connect('localhost',$user,$password);

you can add in the following lines below that to see if it connected or what is the error (database permission etc etc)

if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

Seventh7
post Jan 28 2006, 01:00 PM

Regular
******
Senior Member
1,490 posts

Joined: Sep 2004

please make sure that u change the $user, $password, $database according to ur username, password and database name u have created.
TSlucashii
post Jan 29 2006, 12:45 AM

Getting Started
**
Junior Member
288 posts

Joined: Nov 2004
From: msia/uk


thanks for the reply but i still cant seem to get the code to work.

I created this php code and save it as create.php...so i run this code on my explorer and this table that contains the field shud be accessible in myphpadmin?or do i need to do it manually in myphpadmin and then access it from php?

how do i change the password?cuz i am still using the default setting of the install mysql~

se7en: the code u gave me...where shud i put it?

i am still new to this php so any advice would be appreciated~Thanks~
Seventh7
post Jan 29 2006, 01:30 AM

Regular
******
Senior Member
1,490 posts

Joined: Sep 2004

lets try with simple code first

go to mysql, create a new database and a table

in this example, i put a new database name is mydb and the table name is mytab

so u try put the code in here ur php and see if it can connect or not

<?php
$conn = mysql_connect("localhost");
mysql_select_db("mydb") or die("error connecting to dbase");

?>

try that..and come back to us to update...........


TSlucashii
post Jan 30 2006, 08:32 AM

Getting Started
**
Junior Member
288 posts

Joined: Nov 2004
From: msia/uk


haha my bad cuz i am really a beginner and i havent really read through the whole myphpadmin configuration,its working ok now~thanks all the pros~

 

Change to:
| Lo-Fi Version
0.0150sec    0.71    5 queries    GZIP Disabled
Time is now: 23rd December 2025 - 02:34 AM