Miko Posted April 14, 2009 Share Posted April 14, 2009 Hello, I'm quite new to PHP development so it could be that I'm going to ask some stupid questions So I'm making a login script and I'm running to a small problem I'm getting this error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/my_projects/first_test_project/auth.php on line 15 This comes when I perform the login: my php code: <?php require "header.php"; function controle($username,$password){ $sql = "SELECT * FROM klant WHERE Username = '$username' AND Password = '$password'"; } $username = $_POST['username']; $password = $_POST['password']; $login = $_POST['login']; $result = mysql_query($sql); if($login && $username && $password){ $num_rows = mysql_num_rows($result); if($num_rows == 1){ $_SESSION['username'] = $username; $_SESSION['password'] = $password; } } if(controle($_SESSION['username'], $_SESSION['password']) == false){ ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <table width="200px"> <tr> <td>Username:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password"></td> </tr> <tr> <td><input type="submit" name="login" value="Login"></td> </tr> </table> </form> <?php } require "footer.php"; ?> I'm thinking that I made a mistake somewhere, but don't know where ??? Quote Link to comment https://forums.phpfreaks.com/topic/154093-solved-login-script/ Share on other sites More sharing options...
premiso Posted April 14, 2009 Share Posted April 14, 2009 $sql must be used where it is defined. Since it is not a global character and your function does not return it, it is pointless to define like you have, that and you did not call the function anyhow. What do you have it in a function in the first place? PHP Functions Manual I would suggest looking at proper usage of functions. Quote Link to comment https://forums.phpfreaks.com/topic/154093-solved-login-script/#findComment-810006 Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 You have your $sql variable in a function and it never gets to: $result = mysql_query($sql); Not sure why you have it in a function when if($num_rows == 1){ is true then the username exists with a matching password. Quote Link to comment https://forums.phpfreaks.com/topic/154093-solved-login-script/#findComment-810007 Share on other sites More sharing options...
will35010 Posted April 14, 2009 Share Posted April 14, 2009 ! Quote Link to comment https://forums.phpfreaks.com/topic/154093-solved-login-script/#findComment-810009 Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 You need to change $result on line 15 to $sql No, he doesn't need to do that. He just needs to take $sql out of the function and change his logic a bit. Quote Link to comment https://forums.phpfreaks.com/topic/154093-solved-login-script/#findComment-810011 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.