Daniël Z
Daniël Z

Reputation: 3

How to obtain data from an mysql table?

<?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!

Upvotes: 0

Views: 43

Answers (1)

steven
steven

Reputation: 4875

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.

Upvotes: 2

Related Questions