Jump to content

mySQL to mySQLi problems :(


l3rodey

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/293313-mysql-to-mysqli-problems/
Share on other sites

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'");

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/');
		}
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.