0
<?php
session_start();
ob_start();
$host="localhost"; // Host name 
$username="*"; // Mysql username 
$password="*"; // Mysql password 
$db_name="vragenlijst"; // Database name 
$tbl_name="leerlingen"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE gebruikersnaam='$myusername' and wachtwoord='$mypassword'";
$result=mysql_query($sql);
$sql1="SELECT * FROM leerlingen WHERE gebruikersnaam='$myusername' and wachtwoord='$mypassword'";
$geslacht=mysql_query($sql1);
$row = mysql_fetch_array($geslacht);
echo $geslacht['leraar'];

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername"] = $myusername;
$_SESSION["mypassword"] = $mypassword; 
/*if($geslacht = 'm') {
    header("location:vragen2m.html");
} else {
    header("location:vragen2v.html");   
}
*/
}
else {
echo "Verkeerde gebruikersnaam of wachtwoord";
}
ob_end_flush();
?> 

The mysql table 'leerlingen' contains a column, named 'geslacht'. 'geslacht' contains either the value 'm' or the value 'v'. I know the data is returned by the 'mysql_query($sql1)'-command and those values are stored in a array. When I echo to see the result of the query, the php code returns the error message 'Resource id #4'. I tried several ways of extracting the data, but every time we get error messages or just a blank page. Thanks in advance!

6
  • Google this >>> Resource id #4 Commented Mar 12, 2015 at 19:32
  • echo $geslacht['leraar']; >> echo $row['leraar']; and maybe remove that ob_* stuff if you dont really know what you are doing Commented Mar 12, 2015 at 19:33
  • 1
    Do not use deprecated mysql_*API. Use mysqli_* or PDO Commented Mar 12, 2015 at 19:33
  • possible duplicate of resource id #4 Why am I getting this? Commented Mar 12, 2015 at 19:34
  • I'm sorry, I've been Googleing (??) for the past few hours and every time I try to 'de-array' the data, I fail. Could you please tell me what I'm missing here? Commented Mar 12, 2015 at 19:35

1 Answer 1

2

you did this:

$row = mysql_fetch_array($geslacht);
echo $geslacht['leraar']; // this is the error because $geslacht is your resource and not your array

$geslacht // is your resource

$row //is your array

you can do a print_r($row); to output your array.

you can echo $row['leraar']; if your array has a field called leraar.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.