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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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