By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

how to connect php and mysql

P: 19
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.
Jan 2 '08 #1
Share this Question
Share on Google+
14 Replies


rpnew
100+
P: 188
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.
hi,
can you provide the code you are using for connection..??

Regards,
RP
Jan 2 '08 #2

P: 32
hi

there is some simple code to connect mysql and php



$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "databasename";

mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
Jan 2 '08 #3

P: 19
my code is
<?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']);
?>

use this code also the code offered above, it donnot show error, but it showed nothing at all.
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.
Jan 5 '08 #4

rpnew
100+
P: 188
my code is
[PHP]
<?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']);
?>
[/PHP]
use this code also the code offered above, it donnot show error, but it showed nothing at all.
also i try to use another code
[PHP]
<?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.
[/PHP]


Hi,
Try following in your first code...
[PHP]
$connect = mysql_connect($database['dbserver'], $database['dbuser'], $database['dbpass']);

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

If you get any of error mentioned above then there is something wrong with your 'username, password,servername or databasename' check them properly..

If you dont get any of above mentioned two error..

Then try querying your database through PHP like simple 'select' query and display the result..

If still have any problem. Get back here.. with the error or output you are getting..

Regards,
RP
Jan 5 '08 #5

P: 19
thank you for your help. after using your code, is 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");
?>
Jan 6 '08 #6

P: 19
i have got the wrong db name before. now i have connected scccessfully. thank you very much for your help until now.
Jan 6 '08 #7

P: 19
i have successed in connecting php with mysql, but i cannot insert value into the mysql through php. please help me.
below is my code
<?php
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$repeat_pwd=$_POST['repeat_pwd'];
$name=$_POST['name'];
$email=$_POST['email'];
if(!empty($username)){
$db=@new mysqli

("linux3","zongyan.huang","zongyan.huang","zongyan huang");

if(mysqli_connect_errno()){
echo"connect fail<br>\n";
echo mysqli_connect_error();
exit;
}

insert into t_user

('f_username','f_password','f_name','f_email') values

('$username','$password','$name','$email');

$rs=$db->query($sql);
if(!$rs){
$db->close();
echo 'insert fail';
exit;
}
echo"<font color='red'

size='5'>congulations</font><br>\n";
$db->close();
}
?>

<html>
<head>
<title>regisetr form</title>
</head>
<body>
<?php
if(!empty($username)){
echo "your information is<br>\n";
echo "username:$username<br>\n";
echo "pwd:$pwd<br>\n";
echo "repeat_pwd:$repeat_pwd<br>\n";
echo "name:$name<br>\n";
echo "email:$email<br>\n";
}
?>
<form name="frmRegister" method="post"

action="register.php">
<table width="330" border="0" align="center"

cellpadding="5" bgcolor="#eeeeee">
<tr>
<td width="40%">username</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>password</td>
<td><input name="pwd" type="password" id="pwd"></td>
</tr>
<tr>
<td>repeat password</td>
<td><input name="repeat_pwd" type="password"

id="repeat_pwd"></td>
</tr>
<tr>
<td>name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>email</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
Jan 6 '08 #8

Markus
Expert 5K+
P: 6,050
your 'insert into' statement isnt held in a variable and also, you aren't mysql_query'ing it.

e.g:
[php]
$query = "INSERT INTO `some_table` ... ";
mysql_query($query) or die("Error: ".mysql_error());
[/php]

Use CODE tags. PLEASE.

[PHP]
// code goes here
[/PHP ]
Jan 6 '08 #9

P: 19
after changing according to your advice, it still cannot insert value, below is my code. what is the problem about my code. please help me. thank you.
[php]
<?php
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$repeat_pwd=$_POST['repeat_pwd'];
$name=$_POST['name'];
$email=$_POST['email'];

if(!empty($username)){
$db=@new mysqli("linux3","zongyan.huang","zongyan.huang","z ongyanhuang");
$query = "insert into t_user('f_username','f_password','f_name','f_email ')
values('$username','$password','$name','$email')";
mysql_query($query) or die("Error: ".mysql_error());
?>
[/php]


<html>
<head>
<title>regisetr form</title>
</head>
<body>

[php]
<?php
if(!empty($username)){
echo "your information is<br>\n";
echo "username:$username<br>\n";
echo "pwd:$pwd<br>\n";
echo "repeat_pwd:$repeat_pwd<br>\n";
echo "name:$name<br>\n";
echo "email:$email<br>\n";
}
?>
[/php]

<form name="frmRegister" method="post" action="register.php">
<table width="330" border="0" align="center" cellpadding="5"
bgcolor="#eeeeee">
<tr>
<td width="40%">username</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>password</td>
<td><input name="pwd" type="password" id="pwd"></td>
</tr>
<tr>
<td>repeat password</td>
<td><input name="repeat_pwd" type="password" id="repeat_pwd"></td>
</tr>
<tr>
<td>name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>email</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
Jan 6 '08 #10

Markus
Expert 5K+
P: 6,050
You need to connect to your db first:

[php]
mysql_connect("localhost?", "username...", "password...") or die("Error connecting".mysql_error());
echo "MySQL Connected<br />";
mysql_select_db("database_name") or die("Error database ".mysql_error());
echo "Database Selected<br />";

// code for insert here

[/php]
Jan 6 '08 #11

P: 19
after using your code, it shows MySQL Connected,Database Selected. but after looking at phpMyAdmin, the t_user still didn't have any value, but in fact i have insert some value in the form which should pass to it. please help me. print $sql, i can only got insert into t_user ('f_username','f_password','f_name','f_email') values ('fsdg','','sdg','sdfg')
and the structure of my table is
Field Type Collation Attributes Null Default
f_username varchar(10) latin1_swedish_ci Yes NULL
f_password varchar(10) latin1_swedish_ci Yes NULL
f_name varchar(10) latin1_swedish_ci Yes NULL
f_email varchar(10) latin1_swedish_ci Yes NULL
Jan 7 '08 #12

rpnew
100+
P: 188
after using your code, it shows MySQL Connected,Database Selected. but after looking at phpMyAdmin, the t_user still didn't have any value, but in fact i have insert some value in the form which should pass to it. please help me. print $sql, i can only got insert into t_user ('f_username','f_password','f_name','f_email') values ('fsdg','','sdg','sdfg')
and the structure of my table is
Field Type Collation Attributes Null Default
f_username varchar(10) latin1_swedish_ci Yes NULL
f_password varchar(10) latin1_swedish_ci Yes NULL
f_name varchar(10) latin1_swedish_ci Yes NULL
f_email varchar(10) latin1_swedish_ci Yes NULL
HI,

According to you.. you are now able to connect to MySql, your database is also selected and now you are not able to insert values into table....

Ok tell me one thing first.. is following statement giving any error or not...

[PHP]mysql_query($query) or die("Error: ".mysql_error());[/PHP]

If yes then paste that error here if no then do one thing.. in your code you are printing your SQL statement. Copy that and run it from PHPMyAdmin or MySql command propt. If you are able to do that successfully i mean if you are able to insert the values or getting any other error or output get back here with that..

Regards,
RP
Jan 7 '08 #13

P: 19
thank you. it works.
Jan 7 '08 #14

Markus
Expert 5K+
P: 6,050
thank you. it works.
Lol, you're welcome :)

Post back whenever!
Jan 7 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.