Jump to content

Query failing on Upper/lowercase


ProcalX

Recommended Posts

Morning all, I'm trying to query a user table and am receiving errors. In the mysql statement I am trying to SELECT all data from the table that matches the username of the person logged in.

 

The error I am receiving is this: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\123\myaccount.php

 

The database query is written correctly, if I am logged in as the user Admin then I recieve this message as well:

Unknown column 'Admin' in 'where clause' from the or die(mysql_error());.

 

In the database the username is: admin but I can login using Admin, is this an issue of upper and lowercase?

 

The PHP code is below:

<?php
require_once('connect.php');

$query = mysql_query("SELECT * FROM user WHERE username = $_SESSION[gatekeeper]");
while ($row = mysql_fetch_array($query) or die(mysql_error()));
{
	echo"<div class='BlockContent'>";
	echo"	<table id='UserList'>";
	echo"		<tr>";
	echo"		  <th>My Account</th>";
	echo"		</tr>";
	echo"		<tr>";
	echo"		  <td id='left'>";
	echo"		  </td>";
	echo"		</tr>";
	echo"	</table>";								
	echo"</div>";
}
?>

Link to comment
Share on other sites

Hi

 

Try moving the variable out of the brackets and concatenating it.  I have the same issue sometimes and find that when usingf a variable from an array it crashes.

<?php
require_once('connect.php');
$query = mysql_query("SELECT * FROM user WHERE username = " . $_SESSION[gatekeeper]);

 

Stu

Link to comment
Share on other sites

You must wrap your $_SESSION['gatekeeper'] in single quotes as it is a string.  Also, you'r or die statement should be against the mysql_query() NOT the mysql_fetch_array().

 

in addition it is better practice to assign the query text to a variable before running the mysql_query() against it.

here:

$sql = "SELECT * FROM user WHERE username = '$_SESSION[gatekeeper]'";
$query = mysql_query($sql) or die ('ERROR :<br>'.mysql_error().'<br><br>When Running<br><br>'.$sql);
while ($row = mysql_fetch_array($query){
...

 

Also, try to avoid using SELECT *

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.