error123 Posted April 24, 2010 Share Posted April 24, 2010 Im trying to make a sorta thing where you do lic?=12-12-12-12 and i need it to return "Invalid" if you submit a rong licence and Correct if you have a good licence this is the code i have it seems to either always Return Invalid or always Correct <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $lic = $_GET['lic']; $result = mysql_query("SELECT lic FROM info"); if (!$result){ // the query failed to execute, get debugging information - die('The SELECT query failed' . mysql_error()); } else { // the query executed, check if there are any rows in the result set if(mysql_num_rows($result) <1){ echo "Correct"; // code to execute when there are any row(s) in the result set } else { echo "Invalid"; // code to execute when there are no row(s) in the result set } } ?> Any help would be really appreciated. Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/ Share on other sites More sharing options...
cags Posted April 24, 2010 Share Posted April 24, 2010 mysql_num_rows($result) < 1 This says if less than one row was returned from the database, then output correct. I rather suspect you want that to be a greater than sign not a less than sign. Also you are currently selecting all rows from the database you will want to add a WHERE lic = '$lic' clause to your SQL if you wish to check if the value entered is actually in the database. Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047557 Share on other sites More sharing options...
error123 Posted April 24, 2010 Author Share Posted April 24, 2010 $lic = $_GET['lic']; $result = mysql_query("SELECT lic FROM info WHERE lic = '$lic'"); if (!$result){ // the query failed to execute, get debugging information - die('The SELECT query failed' . mysql_error()); } else { // the query executed, check if there are any rows in the result set if(mysql_num_rows($result)>1 ){ echo "Correct"; // code to execute when there are any row(s) in the result set } else { echo "Invalid"; // code to execute when there are no row(s) in the result set } } Still not working this time its just Invalid for everything :/ Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047561 Share on other sites More sharing options...
cags Posted April 24, 2010 Share Posted April 24, 2010 Since you have the WHERE clause you are only going to return 1 row. So you need to check if the number of rows returns is 1 (not greater than 1). Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047562 Share on other sites More sharing options...
error123 Posted April 24, 2010 Author Share Posted April 24, 2010 Please can you edit the code for that im not a php guru like you ive only just started learning <3 thanks in advanced Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047565 Share on other sites More sharing options...
cags Posted April 24, 2010 Share Posted April 24, 2010 $lic = $_GET['lic']; $result = mysql_query("SELECT lic FROM info WHERE lic = '$lic'"); if (!$result){ // the query failed to execute, get debugging information - die('The SELECT query failed' . mysql_error()); } else { // the query executed, check if there are any rows in the result set if(mysql_num_rows($result)==1 ){ echo "Correct"; // code to execute when there are any row(s) in the result set } else { echo "Invalid"; // code to execute when there are no row(s) in the result set } } Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047567 Share on other sites More sharing options...
error123 Posted April 25, 2010 Author Share Posted April 25, 2010 Thanks for that ive got one more question I have another field called name and i want it to echo "name" on the correct bit how would i do that? thanks Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047903 Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 Edit the SQL query to fetch that field, then fetch the fields from the result set and echo it. $lic = $_GET['lic']; $result = mysql_query("SELECT lic, name FROM info WHERE lic = '$lic'"); if (!$result){ // the query failed to execute, get debugging information - die('The SELECT query failed' . mysql_error()); } else { // the query executed, check if there are any rows in the result set if(mysql_num_rows($result)==1 ){ $row = mysql_fetch_assoc($result); echo $row['name']; // code to execute when there are any row(s) in the result set } else { echo "Invalid"; // code to execute when there are no row(s) in the result set } } Link to comment https://forums.phpfreaks.com/topic/199576-wierd-problem/#findComment-1047914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.