Unstoppable Domains โ€” Get your daily AI drops report

Basic Php, Mysql question and problem - ish

SpaceshipSpaceship
Watch

-bank-

Established Member
Impact
15
Hi all,

I'm having a slight problem with a query, have researched every where to find an answer although thought I should now ask. So basically I am trying to define a variable, $user, from the database, with the id in the database being 1 so I presumed the following should work:

$user = mysql_query ("select `Username` from `Admin` where ID = 1");

Therefore getting the username from the database so when some one tries to log in it compares what they have entered with whats in the database, I have checked with fixed variables in the php and all works fine although it doesn't seem to want to work with the above.

By the way the database does contain all the items needed to work it, so i'm not sure, a little help would be good, thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
hmm make sure your connected to the database, then you would do something like this:

Code:
//DATABASE INFORMATION
$dbuser = "databaseusername"; //DATABASE USERNAME
$dbpass = "databasepassword"; //DATABASE PASSWORD
$dbname = "databasename"; //DATABASE NAME
//END DATABASE INFORMATION
//CONNECT TO DATABASE
$connection = mysql_connect("localhost", $dbuser, $dbpass) or die ('Error: ' . mysql_error()); mysql_select_db ($dbname);
//END CONNECTING
$user = mysql_query("select `Username` from `Admin` where ID = 1");
$userinfo = mysql_fetch_object($user);
if($userinfo->Username==$_POST['Username']) {
echo "Yup... same username";
} else {
echo "Nope... differen't username";
}
 
0
•••
Test the query in PHPAdmin, letter for letter. If that works, then you must not be connecting to the database correctly.

Jason
 
0
•••
thanks for that although it seems to respond with:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/fireback/public_html/php/secure.php on line 25

Hmm, any other ideas?

The script is definitly connecting correctly to the database as I have managed to display all the information in the table Admin although I can't seem to get individual values out of it.

Thanks
 
0
•••
-bank- can you supply some code. The error you have posted indicates either of the following problems:-

1) You are not connecting to the database properly.
2) You have used the wrong variable in mysql_fetch_object (it should be the variable that holds the mysql_query() command
3) The sql failed for some reason (easyiest way to check this is to actually echo out the query and trying it in ssh or using phpmyadmin).

It also indicates that you are not handling error's properly.
 
0
•••
Ok, the code is as follows:

PHP:
$_POST['username'] = $username;
$_POST['password'] = $password;

$serveraddress = 'localhost'; // location of the server you are connecting to
$databaseusername = 'username'; // username of the database you are accessing
$databasepassword = 'password'; // password of the database you are accessing
$databasename = 'databasename'; // name of the database you are acessing
$databaseactive = '1'; // whether to access database or not

if ($databaseactive == '1') {
	$con = mysql_connect($serveraddress,$databaseusername,$databasepassword);
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	mysql_select_db($databasename, $con) or die('Could connect to database');
	$userinfo = mysql_query ("select `Username` from `Admin` where ID = 2");
	$user = mysql_fetch_object ($user);
}
else {
}

Thanks
 
0
•••
Change $user = mysql_fetch_object ($user); to $user = mysql_fetch_object ($user,$con);
 
0
•••
JRBHosting said:
Change $user = mysql_fetch_object ($user); to $user = mysql_fetch_object ($user,$con);

That shouldn't make a difference, if the second argument isn't specified it uses the last mysql connection made
 
0
•••
Hmm, thanks for that although it is still producing the same error
 
0
•••
$user = mysql_fetch_object ($user);

should be

$user = mysql_fetch_object ($userinfo);
 
0
•••
That shouldn't make a difference, if the second argument isn't specified it uses the last mysql connection made

Sometimes, PHP throws a fit if you don't specify the connection very specifically. Sometimes it doesn't.
 
0
•••
EDIT: Flexi was right :) You seem to have switched the variables by accident :P
 
0
•••
Ok, thanks for that, have played around to get the variables correct, etc, now have removed the error although $user doesn't seem to equal anything, tried echo'ing it :( Any other ideas, else I'll post the whole code :)
 
0
•••
when you echo it you mean it does not show anything at all? by rights if $user is still the same line then it should display object if you changed it to the way beaver6813 had it, it would tell you the resource id.

You are probably best however reposting the code so we can see.
 
0
•••
Don't echo $user :P echo $user->Username :)
 
0
•••
Hmm, still nothing, heres my code:
PHP:
<?php

$_POST['username'] = $username;
$_POST['password'] = $password;

$server = 'secure.php';

$emailaddress = 'myemail'; //your personal email address
$siteemail = 'site@localhost'; //the websites email address

$serveraddress = 'localhost'; // location of the server you are connecting to
$databaseusername = '***'; // username of the database you are accessing
$databasepassword = '***'; // password of the database you are accessing
$databasename = '***'; // name of the database you are acessing
$databaseactive = '1'; // whether to access database or not

if ($databaseactive == '1') {
	$con = mysql_connect($serveraddress,$databaseusername,$databasepassword);
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	mysql_select_db($databasename, $con) or die('Could connect to database');
	$userinfo = mysql_query ("select `Password` from `Admin` where ID = 1");
	$user = mysql_fetch_object ($userinfo);
}
else {
}

$header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Untitled Document</title>
	<style type="text/css">
	<!--
	body {
		font-family: Verdana, Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	-->
	</style>
	</head>
	
	<body>';
	
$footer = '</body>
	</html>';

$page;

if ($page == 'forgot') {
$user->Username;
	echo $header;
	echo 'if you have forgotten you password or username please <a href="';
	echo $server;
	echo '?page=sendpass">click here</a> to have it send to you email address.';
	echo $footer;
}
elseif ($page == 'sendpass') {
	$subject = "Forgotted Username / Password";
	$message = $user . " " . $pass ;
	$headers = "From: $siteemail";
	mail($emailaddress,$subject,$message,$headers);
	echo $header;
	echo 'Your Username and Password have been emailed to your selected inbox.<br/>';
	echo 'To return to the login page <a href="';
	echo $server;
	echo '">click here</a>';
	echo $footer;
}
else {

if(!isset($_COOKIE["account"])) {

	if(!isset($username)) {
			echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Untitled Document</title>
	<style type="text/css">
	<!--
	.style1 {
		font-family: Verdana, Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	-->
	</style>
	</head>
	
	<body>
	<form action="';
	echo $server;
	echo '" method="post" name="" class="style1" id="">
	  <label for="username">
	  <div align="center">username: </div>
	  </label>
	  <div align="center">
		<input type="text" name="username" id="username" />
	  </div>
	  <label for="textfield">
	  <div align="center">password: </div>
	  </label>
	  <div align="center">
		<input type="password" name="password" id="password" />
		<br/>
		<input type="submit" name="Submit" value="Submit" id="Submit" />
	  </div>
	  <div align="center">Forgotten your password? <a href="';
	  echo $server;
	  echo '?page=forgot">Click here</a></div>
	</form>
	</body>
	</html>';
			}
	else {
	if ($username == $user) {
		if ($password == $pass) {
			setcookie("account", $user, time()+3600);
			echo $header;
			echo 'you are now logged in<br/><a href="';
			echo $server;
			echo '">Click here</a> to use website';
			echo $footer;
			}
		else {
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Untitled Document</title>
	<style type="text/css">
	<!--
	.style1 {
		font-family: Verdana, Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	-->
	</style>
	</head>
	
	<body>
	<div style="width:100%; height:20px; padding:4px; text-align:center; color:#ffffff; font-weight:bold; background-color:#E3170D; margin-bottom:20px;">the password you entered was incorrect, please try again</div>
	<form action="';
	echo $server;
	echo '" method="post" name="" class="style1" id="">
	  <label for="username">
	  <div align="center">username: </div>
	  </label>
	  <div align="center">
		<input type="text" name="username" id="username" />
	  </div>
	  <label for="textfield">
	  <div align="center">password: </div>
	  </label>
	  <div align="center">
		<input type="password" name="password" id="password" />
		<br/>
		<input type="submit" name="Submit" value="Submit" id="Submit" />
	  </div>
	  <div align="center">Forgotten your password? <a href="';
	  echo $server;
	  echo '?page=forgot">Click here</a></div>
	</form>
	</body>
	</html>';
			}
		}
	else {
	
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Untitled Document</title>
	<style type="text/css">
	<!--
	.style1 {
		font-family: Verdana, Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	-->
	</style>
	</head>
	
	<body>
	<div style="width:100%; height:20px; padding:4px; text-align:center; color:#ffffff; font-weight:bold; background-color:#E3170D; margin-bottom:20px;">the username you entered was incorrect, please try again</div>
	<form action="';
	echo $server;
	echo '" method="post" name="" class="style1" id="">
	  <label for="username">
	  <div align="center">username: </div>
	  </label>
	  <div align="center">
		<input type="text" name="username" id="username" />
	  </div>
	  <label for="textfield">
	  <div align="center">password: </div>
	  </label>
	  <div align="center">
		<input type="password" name="password" id="password" />
		<br/>
		<input type="submit" name="Submit" value="Submit" id="Submit" />
	  </div>
	  <div align="center">Forgotten your password? <a href="';
	  echo $server;
	  echo '?page=forgot">Click here</a></div>
	</form>
	</body>
	</html>';
		}
	}
}
else {
	$id;
	$adminnav = '<a href="?id=home">home</a> | <a href="?id=addcontent">add content</a> | <a href="?id=editcontent">edit content</a> | <a href="?id=delcontent">delete content</a> | <a href="?id=addcatagory">add catagory</a> | <a href="?id=editcatagory">edit catagory</a> | <a href="?id=delcatagory">delete catagory</a> | <a href="?id=logout">log out</a><br/>';
if (!isset($id)) {
	echo $header;
	echo $adminnav;
	echo 'you are already logged in, to logout <a href="?id=logout">Click Here</a><br/>';
	echo $footer;
}
else {
	switch ($id) {
		case logout:
			setcookie ('account', $user, time()-3600);
			header ('location: secure.php');
		case home:
			echo $header;
			echo $adminnav;
			echo 'welcome to the home page you can manage your website content here';
			echo $footer;
		break;
		case addcontent:
			echo $header;
			echo $adminnav;
			echo 'If you would like to add more content to your website please use the following forms';
			echo $footer;
		break;
		case editcontent:
			echo $header;
			echo $adminnav;
			echo 'If you would like to change and edit the content on your website use the following forms';
		break;
		case delcontent:
			echo $header;
			echo $adminnav;
			echo 'To remove content from your website select the catagory you would like to remove it from, then click the delete button beside it.';
			echo $footer;
		break;
		case addcatagory:
			echo $header;
			echo $adminnav;
			echo 'If you would like to add a new catagory to your website please enter its name below and select submit';
			echo $footer;
		break;
		case editcatagory:
			echo $header;
			echo $adminnav;
			echo 'To change a catagory, select and edit the catagory of your choice';
			echo $footer;
		break;
		case delcatagory:
			echo $header;
			echo $adminnav;
			echo 'If you want to delete a catagory select the one you would like to remove then hit delete.';
			echo $footer;
		break;
		default:
			echo $header;
			echo $adminnav;
			echo 'The page you are looking for cannot be found please use the above links.';
			echo $footer;
	}
}
}
}
?>

By the way I am trying to make a basic cms,
 
0
•••
PHP:
if ($page == 'forgot') {
$user->Username;

That part there should be:
PHP:
if ($page == 'forgot') {
echo $user->Username;

I haven't checked the rest yet
 
0
•••
Alrite, thanks will have a try later.
 
0
•••
ok, after a little fiddling, and noticing one stonking mistake I have the script working.

Thanks alot for all the help.
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back