Jump to content
Sign in to follow this  
Bird

FOR LOOP not working

Recommended Posts

Both of these are using the same DB connection($conn) and both currency and level are in the table, yet when I run my page, it shows currency, but level comes up blank.

 

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else{
	echo 'Connected! ';
$sql = "SELECT * FROM KJE WHERE username = '$user' AND password = '$pass' ";
$result = mysqli_query($conn,$sql);
$check = mysqli_fetch_array($result);

if(isset($check)){
	$_SESSION['username'] = $user;
	//BELOW WAS WORKING. WHILE TROUBLESHOOTING I CHANGED IT. WHEN I CTRL+Z BACK TO IT'S ORIGINAL IT STOPPED WORKING. I GUESS I'M DOING SOMETHING FUNDAMENTALLY WRONG AND GOT LUCKY?
	//------currency------
	$sql2 = ("SELECT currency FROM KJE WHERE username = '{$_SESSION['username']}' ");
	$result2 = mysqli_query($conn,$sql2);
	$currency = mysqli_fetch_array($result2);
	for($a = 0 ; $a < count($_SESSION['currency']) ; $a++){
		$_SESSION['currency'] = $currency[$a];
	}
	//BELOW DOES NOT WORK
	//------level------
	$sql3 = ("SELECT `level` FROM KJE WHERE username = '{$_SESSION['username']}' ");
	$result2 = mysqli_query($conn,$sql3);
	$level = mysqli_fetch_array($result2);
	for($b = 0 ; $b < count($_SESSION['level']) ; $b++){
		$_SESSION['level'] = $level[$b];
	}
}

 

Edited by Bird

Share this post


Link to post
Share on other sites

Why are you using a for loop at all?  It's an array with a single value in it, you don't need to loop.  And if there's nothing in $_SESSION, then it won't loop to set the value at all.

Share this post


Link to post
Share on other sites
1 hour ago, justsomeguy said:

Why are you using a for loop at all?  It's an array with a single value in it, you don't need to loop.  And if there's nothing in $_SESSION, then it won't loop to set the value at all.

You're absolutely right. I'm very new at this and thought I had to use a loop for an array. Realized I was already returning the row when I set the $_SESSION['user'].

 

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else{
	echo 'Connected! ';
$sql = "SELECT * FROM KJE WHERE username = '$user' AND password = '$pass' ";
$result = mysqli_query($conn,$sql);
$check = mysqli_fetch_array($result);
$_SESSION['currency'] = $check['currency'];
$_SESSION['level'] = $check['level'];

^much better

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...