Crim Posted July 8, 2023 Share Posted July 8, 2023 So i have 2 errors and i have no real clue why, is it something php8 switched or is it Visual Studio? (I have the php extension) function invalidUid($username) { $result; if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) { $result = true; } else { $result = false; } } The $result; gives and error "Use of unassigned variable '$result'" but it should not be needed to be assigned or? function uidExists($conn, $username, $email) { $sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;"; $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql)) { header("location: ../signup.php?error=stmtfailed"); exit(); } mysqli_stmt_bind_param($stmt, "ss", $username, $email); mysqli_stmt_execute($stmt); $resultData = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($resultData)) { return $row; } else { $result = false; return $result; } mysqli_stmt_close($stmt); } mysqli_stmt_close($stmt); Shows as an error why? this is the error message (Unreachable code detected) Thanks in advance i appriciate all help. Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/ Share on other sites More sharing options...
Barand Posted July 8, 2023 Share Posted July 8, 2023 You reference $result on line 1 before defining it on line 3 or 7 A function exits when return is called. Whether the if() returns true or false you return from the function, so the final line can never be reached. Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610385 Share on other sites More sharing options...
Crim Posted July 8, 2023 Author Share Posted July 8, 2023 (edited) 9 minutes ago, Barand said: You reference $result on line 1 before defining it on line 3 or 7 A function exits when return is called. Whether the if() returns true or false you return from the function, so the final line can never be reached. It does not have a definition, it did not use to require any since it is a super global. Did this change? Sorry i get confused at 2 with my dyslexia. localhost/signup.php?error=invalidUid <--- is what the brower says. <?php function emptyInputSignup($name,$email,$username,$pwd,$pwdRepeat) { $result; if (empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdRepeat) ) { $result = true; } else { $result = false; } return $result; } function invalidUid($username) { $result; if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) { $result = true; } else { $result = false; } } function invalidEmail($email) { $result; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $result = true; } else { $result = false; } return $result; } function pwdMatch($pwd, $pwdRepeat) { $result; if ($pwd !== $pwdRepeat) { $result = true; } else { $result = false; } return $result; } function uidExists($conn, $username, $email) { $sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;"; $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql)) { header("location: ../signup.php?error=stmtfailed"); exit(); } mysqli_stmt_bind_param($stmt, "ss", $username, $email); mysqli_stmt_execute($stmt); $resultData = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($resultData)) { return $row; } else { $result = false; return $result; } mysqli_stmt_close($stmt); } function createUser($conn, $name, $email, $username, $pwd) { $sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd) VALUES (?,?,?,?);"; $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql)) { header("location: ../signup.php?error=stmtfailed"); exit(); } $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT); mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $username, $hashedPwd); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); header("location: ../signup.php?error=none"); exit(); } So do i need that statement or can i skip it or? 😛 Edited July 8, 2023 by Crim Adding Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610386 Share on other sites More sharing options...
Barand Posted July 8, 2023 Share Posted July 8, 2023 Your second function needs to return $result, but all you need is function emptyInputSignup($name,$email,$username,$pwd,$pwdRepeat) { return empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdRepeat); } function invalidUid($username) { return !preg_match("/^[a-zA-Z0-9]*$/", $username) ; } 6 minutes ago, Crim said: since it is a super global. Did this change? It never was. Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610388 Share on other sites More sharing options...
Crim Posted July 8, 2023 Author Share Posted July 8, 2023 So then i could use return the same way for Email and Pwdmatch? Awesome help so far. Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610389 Share on other sites More sharing options...
Solution Barand Posted July 8, 2023 Solution Share Posted July 8, 2023 if (expression) return true else return false is equivalent to return expression Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610390 Share on other sites More sharing options...
Crim Posted July 8, 2023 Author Share Posted July 8, 2023 1 minute ago, Barand said: if (expression) return true else return false is equivalent to return expression Thanks a bunch mate, sent you a gift. 😃 Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610392 Share on other sites More sharing options...
Barand Posted July 8, 2023 Share Posted July 8, 2023 Oooh! thanks. Christmas in July. Quote Link to comment https://forums.phpfreaks.com/topic/317069-two-different-problems-stmt-and-result/#findComment-1610393 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.