-1

Possible Duplicate:
Warning when using mysql_fetch_assoc in PHP

i am having a problem with the following codes, i am new in encountering this error here is the code

session_start();
$uname=$_SESSION['login'];

$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);

$teacherid  = $row['teacherID'];

it gives me a "mysql_fetch_assoc() expects parameter 1 to be resource, boolean" error, how do i deal with this error?? i have used this code already a few times in other files and it worked perfectly except now, i checked the names of the rows and it was correct

i already tried using other commands such as mysql_fetch_array, mysql_result, mysql_fetch_row and it gives the same error

Community
  • 1
  • 1
Patrick Narcelles
  • 169
  • 2
  • 3
  • 12

4 Answers4

5

You seem to be using a variable that is a string, you need to encapsulate it in quotes:

SELECT * FROM tblteacher WHERE teacherName='$uname'

On that note, I see that it is coming from a Session variable, I take it that it is already cleansed to make sure there are no possible injection attacks within it - yes?

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • ohhh thank you for the help, didnt know i should add quotes to it – Patrick Narcelles Sep 20 '12 at 10:12
  • @PatrickNarcelles Anytime That's what we are here for right? :) – Fluffeh Sep 20 '12 at 10:13
  • oh sir how can do i deal with dates?? $sql="SELECT SchedTimeTo FROM tblsched WHERE teacherID=$teacherid and SchedDateFrom<=$cd1 and SchedDateTo>=$cd1"; cd1 is the current date – Patrick Narcelles Sep 20 '12 at 10:43
  • Dates should be with `'`s same as the string. Thus your query should look: `$sql="SELECT SchedTimeTo FROM tblsched WHERE teacherID=$teacherid and SchedDateFrom<='$cd1' and SchedDateTo>='$cd1'";` If cd1 is the current date tho, just use mysql's function NOW() to speed up things. – Andrius Naruševičius Sep 20 '12 at 11:59
  • @AndriusNaruševičius If the dates are in a datetime format already, I see nothing wrong at all with using the inbuilt functions (I would recommend it) :) – Fluffeh Sep 20 '12 at 12:01
  • Well, I see nothing wrong either. It's just more work to have additional variable only for the query which contains current date but, obviously, in many cases this is not an option :) – Andrius Naruševičius Sep 20 '12 at 12:04
3

Try

$sql = "SELECT * FROM tblteacher WHERE teacherName='$uname'";
Miroslav
  • 1,960
  • 1
  • 13
  • 26
2

The problem is in this line

$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";

change to

$sql = "SELECT * FROM tblteacher WHERE teacherName='$uname'";

the uname is string and it should be quoted using single or double quotes.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0
Try This // user index no 

session_start();
$uname=$_SESSION['login'];

$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);

**$teacherid  = $row[0];** 
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54