Forum Moderators: coopster

Message Too Old, No Replies

MYSQL syntax problem? mysql result() ?

         

lukie_luke

3:21 pm on May 27, 2009 (gmt 0)

10+ Year Member



Hi chaps having a problem here and wondering if you could help me..

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?

LifeinAsia

3:34 pm on May 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Welcome to Webmaster World?

What error message are you getting? What do you get if you echo $level?

raheelajk

3:36 pm on May 27, 2009 (gmt 0)

10+ Year Member



lukie_luke,

what error are you getting?

try

echo mysql_result($result, 0);

lukie_luke

4:29 pm on May 27, 2009 (gmt 0)

10+ Year Member



Wow very quick reply sorry for mine being a little slow :)

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'

lukie_luke

4:40 pm on May 27, 2009 (gmt 0)

10+ Year Member



Here is the whole file if it helps...


<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>

LifeinAsia

6:37 pm on May 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Wow, you're hitting the database 3 times? Why not do the following:
SELECT unlock
FROM data
WHERE username='$user' AND passw0rd='$pass'

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.

lukie_luke

7:23 pm on May 27, 2009 (gmt 0)

10+ Year Member



Yeah your right about too many times Asia,
I will remove the second connection.

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
?

lukie_luke

7:53 pm on May 27, 2009 (gmt 0)

10+ Year Member



Okay I found a work around which should work for any user that logs in, I hope...


$level = "SELECT * FROM data WHERE username='$user'";
$result = mysql_query($level);
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['unlock'];

surely there has to be a better way of doing this?

idfer

10:03 pm on May 27, 2009 (gmt 0)

10+ Year Member



"unlock" is a reserved word in SQL so your original SQL statement is producing a syntax error. You may want to change that field's name to something else to avoid further problems.

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. ;-)