Jump to content


Photo

Class, Sql Variable - Resource Id #8


  • Please log in to reply
4 replies to this topic

#1 azraelGG

azraelGG

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 30 December 2012 - 11:51 AM

im trying to get 1 data column and store it in session variable so i can use it later
i tried to do that on line 47 but without success
when i try print_r($result) i get "resource id #8"

here is the class file
<?php
class korisnik{
private $_ime;
private $_prezime;
private $_oib;
private $_adresa;
private $_email;
private $_lozinka;
private $_database;

public function _construct(){

}

//metoda koja provjerava da li je korisnik logiran
public function isLogiran(){
//ukoliko je korisnik logiran
if(isset($_SESSION['logiran'])&& isset($_SESSION['email'])){
return true;
}
//ukoliko korisnik nije logiran idi na index.php i vrati na index.php
else {
if( basename($_SERVER['PHP_SELF']) != "index.php" && basename($_SERVER['PHP_SELF']) != "registracija.php"){
	 header('Location: index.php');
}
}
}

//metoda za logiranje
public function getKosrinikLogin($email, $lozinka){
//provjera da li je upisano nešto u polja
$lozinka = sha1($lozinka);
if(!empty($email) && (!empty($lozinka))){
//pravljenje novog korisnika i provjera sql upita
$korisnik = new Database();
$sql = "SELECT * FROM korisnik WHERE email = '$email' AND lozinka = '$lozinka'";
$result = $korisnik->query($sql);

//provjeriti da li je samo jedan rezultat sql upita - provjerava ispravnos podataka
$korisnikCount = mysql_num_rows($result);

if($korisnikCount == 1){
	 //podesit session
	 $_SESSION['logiran'] = "da";
	 $_SESSION['email'] = $_POST['email'];
	 print_r($result);
	 //ROW 45 HERE --------------------------------------------------- want get field ime from database table
	 echo $result[0]['ime'];
	
}
// ukoliko unos nije ispravan
else {
	 echo "Unesite ispravne podatke";
}
}
// ako je polje prazno
else {
echo "Unesite e-mail adresu i šifru";
}
}//end of getKorisnikLogin

//metoda za registraciju
public function registerKorisnik($ime, $prezime, $oib, $adresa, $email, $lozinka){
$lozinka = sha1($lozinka);
//provjera polja
if(!empty($ime) && (!empty($prezime)) && (!empty($oib)) && (!empty($adresa)) && (!empty($email)) && (!empty($lozinka))){
//ako su unešena sva polja provjeri podatke
$sql = "SELECT * FROM korisnik WHERE email = '$email'";
$korisnik = new Database();
$emailCheck = $korisnik->query($sql);
if(mysql_num_rows($emailCheck) == 0){
	 //ako email adresa nije zauzeta registriraj korisnika
	 $registracija = "INSERT INTO korisnik (ime, prezime, oib, adresa, email, lozinka) VALUES ('{$ime}', '{$prezime}', '{$oib}', '{$adresa}', '{$email}', '{$lozinka}')";
	
	 //ZAŠTO OVAJ RED UPISUJE U BAZU PODATAKA ako ima if
	 if($korisnik->zapis($registracija)){
	 echo "Uspješno ste se registrirali.";
	 }
	 else {
	 echo "Došlo je do pogreške prilikom registracije. <br>";
	 echo mysql_error();
	 }
}
else if (mysql_num_rows($emailCheck) != 0){
	 echo "Ova e-mail adresa je već zauzeta.";
}
else {
	 echo "Došlo je do pogreške, molimo pokušajte ponovo.";
}
}
else {
echo "Unesite sve podatke.";
}
}//end of registerKorisnik
}
?>

rest of the code works fine

Edited by azraelGG, 30 December 2012 - 11:59 AM.


#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,171 posts
  • LocationCheshire, UK

Posted 30 December 2012 - 12:10 PM

That's because result is a resource - you need to use the result to fetch the returned row
$row = $result->fetch_row();
echo $row[0];

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 azraelGG

azraelGG

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 30 December 2012 - 12:16 PM

i think that fetch_row() is for PDO only but my connecting is not over PDO in this case
so i used standard
$row = mysql_fetch_assoc($result)

thanks for tip

#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,171 posts
  • LocationCheshire, UK

Posted 30 December 2012 - 01:03 PM

When I saw
$korisnik->query($sql);
I thought you were using mysqli

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 30 December 2012 - 06:48 PM

MySQL is being thrown out the window, window locked and key melted so I strongly suggest you switch to MySQLi or PDO as per the php website :D.

You really need to break your code down and write English comments as many people will discard your post the minute they see a massive chunk of code. The Database::query method appears to return a resource ID so you can call a mysql_fetch_assoc/array() function to retrieve the next row in the result set - of course you're going to change to MySQLi though and use mysqli_fetch_assoc() ;)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com