l3rodey Posted December 23, 2014 Share Posted December 23, 2014 Hi, I only just worked out (a little slow) that mysql is not redundant and crap =\ Apparently I should be using mysqli. However I need help I tried following a tutorial online but it failed. This is my login script and it doesn't work <?php require_once '../inc/conn.php'; session_start(); if ($_POST['username']) { $username = $_POST['username']; $password = $_POST['password']; $requestLogin = mysqli_query( $retreat, "SELECT * FROM login WHERE username='$username' AND password='$password'"); while($row = mysqli_fetch_array($requestLogin)){ $userID = $row['userID']; $_SESSION['userID'] = $userID; $username = $row['username']; $_SESSION['username'] = $username; header('location: /administrator/'); } } ?> And this is the conn.php ( something hidden for safety, however it does connect correctly. <?php $hostname = 'localhost'; $username = ''; $password = ''; $database = ''; $retreat = mysqli_connect($hostname, $username, $password, $database) or die('Connecting to MySQL failed'); ?> The actual <form> is also correct as it's just normal html and worked before I tried converting to mysqli. After this login I have a script which is this that goes in the header of all admin pages. <?php require_once '../inc/conn.php'; session_start(); if (isset($_SESSION['userID'])) { $username = $_SESSION['username']; $getUser = mysqli_query( $retreat, "SELECT user_rights FROM login WHERE username='$username'"); while($row = mysqli_fetch_array($getUser)){ $user_rights = $row['user_rights']; } } else { include_once 'login.php'; exit(); } ?> I am no coding professional by any means but I get the job done. However this mysqli is sort of new ground. Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/ Share on other sites More sharing options...
hansford Posted December 24, 2014 Share Posted December 24, 2014 (edited) $retreat = new mysqli($hostname, $username, $password, $database); if ($retreat->connect_errno) { echo $retreat->connect_errno . ' : ' . $retreat->connect_error; } Edited December 24, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500540 Share on other sites More sharing options...
hansford Posted December 24, 2014 Share Posted December 24, 2014 (edited) Will need this too because you're using an object now..couple more changes. $getUser = $retreat->query("your query here"); while ($row = $retreat->fetch_assoc()) { // add your loop code here } Edited December 24, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500542 Share on other sites More sharing options...
l3rodey Posted December 24, 2014 Author Share Posted December 24, 2014 if ($_POST['username']) { $username = $_POST['username']; $password = $_POST['password']; $requestLogin = $retreat->query("SELECT * FROM login WHERE username='$username' AND password='$password'"); while ($row = $retreat->fetch_assoc()){ $userID = $row['userID']; $_SESSION['userID'] = $userID; $username = $row['username']; $_SESSION['username'] = $username; header('location: /administrator/'); } } Gives out this error Fatal error: Call to a member function query() on a non-object in /home/**/**/**/**/login.php on line 11 Which is this line: $requestLogin = $retreat->query("SELECT * FROM login WHERE username='$username' AND password='$password'"); Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500544 Share on other sites More sharing options...
hansford Posted December 24, 2014 Share Posted December 24, 2014 Yeah sorry, because I'm trying to write code and keep track of your variable names at the same time. $requestLogin = $retreat->query("your query here"); while ($row = $requestLogin->fetch_assoc()) Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500545 Share on other sites More sharing options...
l3rodey Posted December 24, 2014 Author Share Posted December 24, 2014 Same problem... <?php require_once '../inc/conn.php'; session_start(); if ($_POST['username']) { $username = $_POST['username']; $password = $_POST['password']; $requestLogin = $retreat->query("SELECT * FROM login WHERE username='$username' AND password='$password'"); while ($row = $requestLogin->fetch_assoc()){ $userID = $row['userID']; $_SESSION['userID'] = $userID; $username = $row['username']; $_SESSION['username'] = $username; header('location: /administrator/'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500546 Share on other sites More sharing options...
hansford Posted December 24, 2014 Share Posted December 24, 2014 If you're using the same database include file for all pages, then the code needs to use the same variable name of the database object created. So, if conn.php creates the object then: $requestLogin = $retreat->query("SELECT * FROM login WHERE username='$username' AND password='$password'"); The object created better be named $retreat if it's going to work on this page. Otherwise rename $retreat whatever variable name you used to create the object. conn.php should have this code or something similar. As you can see - the object created is named $retreat. $retreat = new mysqli($hostname, $username, $password, $database); if ($retreat->connect_errno) { echo $retreat->connect_errno . ' : ' . $retreat->connect_error; } Quote Link to comment https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/#findComment-1500548 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.