Forum Moderators: coopster
I am trying to get one value from my database and use it as a variable.
$level = "SELECT unlock FROM data WHERE username='$user'";
$result = mysql_query($level);
echo mysql_result($result); Doesn't seem to work and I have tried other ways but to no avail.
Does anyone have any suggestions or need anymore info to help?
When I try echo mysql_result($result, 0); I get error message :
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\includes\navi.php on line 27"
without the zero I get Wrong parameter count for mysql_result() in C:\wamp\www\includes\navi.php
if I echo $level I get : SELECT unlock FROM data WHERE username='luke'
<div class="navi"><form action="../index.php" method="post">
<a href="passresest.php">forgot password</a> / <a href="reg.php">register</a><br />
Username<br />
<input type="text" name="user" maxlength="15" /><br />
Password<br />
<input type="password" name="pass" maxlength="15" />
<br />
<?php
@$user = $_POST['user'];
@$pass = $_POST['pass'];
//checks username
include($_SERVER['DOCUMENT_ROOT'] . '/includes/dbcon.php');
if(isset($_POST['user'],$_POST['pass']))
{mysql_select_db("userdata");
$check = "SELECT username FROM data WHERE username ='$user'";
$result = mysql_query($check);
//check to see if get username matches password
if(mysql_num_rows($result)==0) {echo "<span id=\"formerror\">Incorrect username<br />or password</span>";}
else{$checkpass = "SELECT passw0rd FROM data WHERE username='$user' AND passw0rd='$pass'";
$result = mysql_query($checkpass);
// if password matchs get the level unlock, echo and set varible
if(mysql_num_rows($result) == 1)
{echo "<span id=\"formok\">Login okay</span>";
$level = "SELECT unlock FROM data WHERE username='$user'";
$result = mysql_query($result);
;}
else{echo "<span id=\"formerror\">Incorrect username<br />or password</span>";} ;} ;}
?>
<br />
<input type="submit" value="login" />
</form>
<hr />
Your Level of<br />
enlightenment <br />
<a href="../level1/index.php">Level 1</a><br />
<a href="../level2/index.php">Level 2</a><br />
<a href="../level3/index.php">Level 3</a><br />
<a href="../level4/index.php">Level 4</a><br />
<a href="../level5/index.php">Level 5</a><br />
<a href="../level6/index.php">Level 6</a><br />
<a href="../level7/index.php">Level 7</a><br />
<a href="../level8/index.php">Level 8</a><br />
<a href="../level9/index.php">Level 9</a><br />
<a href="../level10/index.php">Level 10</a><br />
<br />
<a href="contact.php">Contact us</a>
</div>
If you get 1 result, you're done.
If zero, then the user submitted an invalid username/password combo. If you wanted to be nice, you could do a second call:
SELECT unlock
FROM data
WHERE username='$user'
Then you would know if it's a bad user or a wrong password.
What I am stuck on though is getting unlock value from the db and returning the value here is how it is meant to go...
Check username and pass -> if okay get the unlock level of the user from the db and echo it and keep as a varible,
by default the user starts off with the value of one "1" when they get further they can get a higher number which will allow them to get to different pages which were locked before.
It's a quiz game basically with different levels:)
Do you think that mysql_result() is the best function to use
?
BTW, the warning "supplied argument is not a valid MySQL result resource" means that your SQL statement couldn't be executed, either because of a syntax error or typo in a table or field name. In your code, i don't see any checks to see if the query was executed properly, so try adding this code after every call mysql_query(), it'll save you a lot time and guess-work:
$result = mysql_query($sql); //existing call
if($result === false) {
echo 'Unable to execute SQL query '.htmlentities($sql);
echo '<br>'.htmlentities(mysql_error());
exit;
}
If you want to be clever, write a function called db_query (or whatever you like) that implements the code above and returns $result if ok, then call that function instead of mysql_query() everywhere: instant hassle-free error reporting. ;-)