/    Sign up×
Community /Pin to ProfileBookmark

PHP MySQL communication

I am having some problems understanding the php, and mysql communication thing. I have looked at a script off some forum, and coppied it, but I get the error that came from this line: [COLOR=Red]echo (‘error :: could not connect to ‘ . $dbname . ‘ table’);[/COLOR]

Any help? Any places out there that is short reading, and can help me with learning the mysql, and php communication thing?

[code=php]<?php
//Login Script
// declare variables
$username=$_POST[‘username’];
$password=$_POST[‘password’];
//
// mysql connection variables
$host = ‘localhost’;
$dbuser = ‘dtoporg_dtop’;
$dbpass = ‘abc123’;
$dbname = ‘dtoporg_login’;
$table = ‘login’;

//
// connect to db
$db = @mysql_connect($host,$dbuser,$dbpass) or die(“error :: could not connect to $host”);
$db = mysql_select_db($dbname);
if(!$db)
{
echo (‘error :: could not connect to ‘ . $dbname . ‘ table’);
exit;
}
//
// check table
$query = mysql_query(“SELECT * FROM $table WHERE USERname = ‘$username’ AND USERpass = ‘$password'”);
$num = mysql_num_rows($query);
if($num>0)
{
echo(‘user :: ok’);

} else {
echo (‘error :: not ok’);
}
?>[/code]

Anything that relates to login scripts would be very helpfull, Thanks.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Mar 14.2006 — You can't reassign $db
[code=php]
$db = @mysql_connect($host,$dbuser,$dbpass) or die("error :: could not connect to $host");
mysql_select_db($dbname,$db);
if(!$db)
{
echo ('error :: could not connect to ' . $dbname . ' table');
exit;
}
[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorMar 14.2006 — OK, when I run It I get this:

[B]Warning[/B]: mysql_num_rows(): supplied argument is not a valid MySQL result resource in [B]/home2/dtoporg/public_html/catolog/logged.php[/B] on line [B]26[/B]
Copy linkTweet thisAlerts:
@Mester_PedizMar 14.2006 — ah nothing anyways....
Copy linkTweet thisAlerts:
@chazzyMar 14.2006 — You can't reassign $db
[code=php]
$db = @mysql_connect($host,$dbuser,$dbpass) or die("error :: could not connect to $host");
mysql_select_db($dbname,$db);
if(!$db)
{
echo ('error :: could not connect to ' . $dbname . ' table');
exit;
}
[/code]
[/QUOTE]


Yes, you can. It's stupid, I know, but in PHP you can reassign it like that.

Try this:

[code=php]
$db = mysql_connect($host,$dbuser,$dbpass) or die("error :: could not connect to $host MySql Error:".mysql_error());
$db=mysql_select_db($dbname) or die("error :: could not connect to database. MySql Error:".mysql_error());
[/code]


This won't fix the problem, but this shouldt ell you why you can't connect.
Copy linkTweet thisAlerts:
@Mester_PedizMar 14.2006 — [code=php]// check table
$query = mysql_query("SELECT * FROM $table WHERE USERname = '$username' AND USERpass = '$password'");
$num = mysql_num_rows($query); [/code]


This needs to be:

[code=php]
// check table
$query = "SELECT * FROM $table WHERE USERname = '$username' AND USERpass = '$password'";
$result = mysql_query($query);
$num = mysql_num_rows($result); [/code]


Actually never mind, i think my brains fried today..... ?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorMar 15.2006 — OK, Now it looks like this:
[code=php]
<?php
//My login Script
// declare variables
$username=$_POST['username'];
$password=$_POST['password'];
//
// mysql connection variables
$host = 'localhost';
$dbuser = 'dtoporg_dtop';
$dbpass = 'abc123';
$dbname = 'dtoporg_login';
$table = 'login';

//
// connect to db
$db = mysql_connect($host,$dbuser,$dbpass) or die("error :: could not connect to $host MySql Error:".mysql_error());
$db=mysql_select_db($dbname) or die("error :: could not connect to database. MySql Error:".mysql_error());

//
// check table
$query = "SELECT * FROM $table WHERE USERname = '$username' AND USERpass = '$password'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num>0)
{
echo('user :: ok');

} else {
echo ('error :: not ok');
}
?>
[/code]


When It is run, this line is printed to the screen:

[I]error :: could not connect to database. MySql Error:Access denied for user 'dtoporg_dtop'@'localhost' to database 'dtoporg_login'[/I]

From Here:

[I]$db=mysql_select_db($dbname) or die("error :: could not connect to database. MySql Error:".mysql_error()); [/I]
Copy linkTweet thisAlerts:
@chazzyMar 15.2006 — well that's your problem. dtoporg_dtop does not have access to dtoporg_login

you have to fight your grant statements.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorMar 15.2006 — Ahhh, THanks, It works now!
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...