Jump to content

Fetch data from db then process it. simple


Zephyris

Recommended Posts

I'm fetching data from the db but I'm trying to know if the data exist and if it is correct then if it's wrong echo an error msg and if it's right echo a success msg. I'm probably doing something wrong here...

 

I tried if(!result) and if(!row) both don't work.

 

$result = mysql_query("SELECT * FROM `users` WHERE cu='$uti' AND mdp= MD5('$mdp')") or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());

if (!$result) {
    	echo 'ERREUR DE CONNEXION 23-GO ' . mysql_error(); //HERE wrong name or Password
	echo '<meta http-equiv="refresh" content="5;url=index.php"> ';
} else {
	$_SESSION['utilisateur']=$row['cu']; //here great you've logged in
	$_SESSION['email']=$row['em'];

	echo "<br> Connexion établie... <br><b>Redirection vers la page d'accueil dans 3 secondes...</b>";
	echo '<meta http-equiv="refresh" content="3;url=index.php"> ';
} 
}

 

Loging in works perfectly however, if I enter bad username or password it never gets to the error msg. hmmm.

 

Thanks for the help in advance guys.

Try the following, more error checking :)

  $myQuery = "SELECT * FROM `users` WHERE cu='$uti' AND mdp= MD5('$mdp')";

  if($doQuery = mysql_query($myQuery)) {
    if(mysql_num_rows($doQuery)) {
      $row = mysql_fetch_assoc($doQuery);

      $_SESSION['utilisateur'] = $row['cu']; //here great you've logged in
      $_SESSION['email']       = $row['em'];
      echo '<br> Connexion établie... <br><strong>Redirection vers la page d\'accueil dans 3 secondes...</strong>';
      echo '<meta http-equiv="refresh" content="3;url=index.php"> ';

    } else {
      echo 'This query returned 0 rows: '.$myQuery;
    }
  } else {
    echo 'Error with the following query: '.$myQuery;
  }

 

Tell me how it goes buddy.

 

Regards, PaulRyan.

Wow thanks Paul got it to work with what you gave me.

 

What's the difference between mysql_fetch_assoc and mysql_fetch_array? Should I not use array?

 

	if($result = mysql_query("SELECT * FROM `utilisateurs` WHERE cu='$uti' AND mdp= MD5('$mdp')")){
	if(mysql_num_rows($result)) {
		$row = mysql_fetch_assoc($result);

		$_SESSION['utilisateur']=$row['cu'];
		$_SESSION['email']		=$row['em'];

		echo "<br> Connexion établie... <br><strong>Redirection vers la page d'accueil dans 3 secondes...</strong>";
		echo '<meta http-equiv="refresh" content="3;url=index.php"> ';
	} else {
	    echo '<br>ERREUR DE CONNEXION 30-GO ' . mysql_error();
		echo '<meta http-equiv="refresh" content="5;url=index.php"> ';
	}
} else {    
	echo 'Erreur avec la demande. 50-GO';
}

 

Thanks for the help!

The difference is that mysql_fetch_array() return both numerical and textual resulting arrays.

So you can call the first element bt using $row['0'] or $row['firstElement'].

 

Using just mysql_fetch_assoc() just return the textual resulting array.

So you call the first element by using $row['firstElement'] which is better as you know what value the variable holds without needing to see the database layout etc.

 

Neither is the correct way to do though, just down to preference.

 

Regards, PaulRyan.

Archived

This topic is now archived and is closed to further replies.

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