
|
View Full Version : Mysql_Num_Rows Problem
advancetheweb.com 02-18-2006, 12:44 PM My Problem: I have gotten this bit to function as it should by echoing "Please Change the Package Name" when the Name already exists however the problem still exists that anything i put after the if statement isn't working.... if i tell it to echo something after that if statement, it just shows a blank screen....help?
My Code:
$sql1 = "SELECT * FROM $table_name WHERE Name = '$_POST[name]'";
$check = @mysql_query($sql1, $connection) or die(mysql_error());
$rows = @mysql_num_rows($check) or die(mysql_error());
if ($rows >= 1) {
$exist = "Please Change The Package Name";
echo $exist;
exit;
}
Dan L 02-18-2006, 01:32 PM Firstly.. do
$name = mysql_real_escape_string($_POST['name']);
This will keep you from being succeptable to SQL injections.
Then, get rid of the @ signs before the mysql functions, since the script will die anyways.
Now, instead of the way you have it coded, try..
if($rows >= 1) {
exit('Please change the package name.');
}
And see if that works better.
Now, the other option is
SELECT count(`id`) AS `count` FROM..
and then do a mysql_fetch_assoc(mysql_query()) on it, and then do
if($rows['count'] >= 1)
Hope that helps.
advancetheweb.com 02-18-2006, 01:50 PM nope, no luck with that...:crap:
it still goes to a blank page after getting past that if statement...
Dan L 02-18-2006, 02:07 PM Can we see the rest of your code?
advancetheweb.com 02-18-2006, 02:20 PM Heres the full code, ideally before the $display part, I want to have an MYSQL INSERT...i had an insert there before but i took it out when i realized that something was wrong with the mysql_num_rows...i wanted to make it simplier so it could be fixed...please note, that this script ran with the display and the MYSQL INSERT before I added the $rows part
<?
$db_name = "mydb";
$table_name = "mytable";
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$sql1 = "SELECT * FROM $table_name WHERE Name = '$_POST[name]'";
$check = mysql_query($sql1, $connection) or die(mysql_error());
$rows = mysql_num_rows($check) or die(mysql_error());
$exist = "Please Change The Package Name";
if ($rows >= 1) {
exit("$exist");
}
$display = "
<HTML>
<HEAD>
<TITLE>My Code</TITLE>
</HEAD>
<BODY>
<STRONG>THIS IS MY CODE! WILL IT WORK?</STRONG>
</BODY>
</HTML>";
echo $display;
?>
advancetheweb.com 02-18-2006, 07:32 PM any ideas?:peace:
Burhan 02-19-2006, 02:41 AM exit() will stop execution of your code, which is why nothing is working past the if.
Idealws.com 02-19-2006, 03:21 AM As fyrestrtr said exit; will kill the code right there and not allow it to continue on. You could write you code with an error catcher and print only when needed like so:
<?
$db_name = "mydb";
$table_name = "mytable";
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$sql1 = "SELECT * FROM $table_name WHERE Name = '$_POST[name]'";
$check = mysql_query($sql1, $connection) or die(mysql_error());
$rows = mysql_num_rows($check) or die(mysql_error());
$error = 0;
$exist = "Please Change The Package Name";
if ($rows >= 1) {
$error = 1;
}
if ($error) {echo $exist; };
$display = "
<HTML>
<HEAD>
<TITLE>My Code</TITLE>
</HEAD>
<BODY>
<STRONG>THIS IS MY CODE! WILL IT WORK?</STRONG>
</BODY>
</HTML>";
echo $display;
?>
or
[PHP]
<?
$db_name = "mydb";
$table_name = "mytable";
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$sql1 = "SELECT * FROM $table_name WHERE Name = '$_POST[name]'";
$check = mysql_query($sql1, $connection) or die(mysql_error());
$rows = mysql_num_rows($check) or die(mysql_error());
$error = 0;
$exist = "Please Change The Package Name";
if ($rows >= 1) {
$error = 1;
}
if ($error) {
$display = "
<HTML>
<HEAD>
<TITLE>My Code</TITLE>
</HEAD>
<BODY>
<STRONG>$exist</STRONG>
<STRONG>THIS IS MY CODE! WILL IT WORK?</STRONG>
</BODY>
</HTML>";
} else {
$display = "
<HTML>
<HEAD>
<TITLE>My Code</TITLE>
</HEAD>
<BODY>
<STRONG>THIS IS MY CODE! WILL IT WORK?</STRONG>
</BODY>
</HTML>";
}
echo $display;
?>
The later of the 2 will allow you to add different error messages depending on what you want to show the user. You could make a error function and call it when you want but this should get you going.
Hope this helps.
Regards,
Ray
Burhan 02-19-2006, 07:19 AM You can combine the above with:
$sql1 = "SELECT COUNT(*) as `rowcount` FROM $table_name WHERE Name = '.mysql_real_escape_string($_POST['name'])."'";
$check = mysql_query($sql1, $connection) or die($sqll."<br />".mysql_error());
$data = mysql_fetch_row();
$count = $data[0];
if ($count >= 1) {
# we know that there is an error
echo $_POST['name'].' already exists in our system. Please select another.';
# set any other variables for failure here
} else {
echo 'Thank you';
# set any variables that you need here.
}
advancetheweb.com 02-20-2006, 11:59 AM exit() will stop execution of your code, which is why nothing is working past the if.
thats not true...because i have used exit() like this in scripts before and they dont just end... i have exit in if statements and as long as the if is false, the exit wont end the script
Dan L 02-20-2006, 01:09 PM Yes, but since you didn't have the () after exit, it might have performed differently.
Korvan 02-20-2006, 03:01 PM the () doesnt matter with the exit function. exit is a language construct so it does not require ()
the following will exit a php script normally
exit;
exit();
exit(0);
Once it hits the construct it terminates the php script, but it has to hit it. If it is embedded in an if that evaluates to false, it is obviously skipped.
b.t.w. are you sure that your query returning results? is there any data in the field Name match your posted data?
Do this to check:
$sql1 = "SELECT * FROM $table_name WHERE Name = '$_POST[name]'";
$check = @mysql_query($sql1, $connection) or die(mysql_error());
$rows = @mysql_num_rows($check) or die(mysql_error());
echo $sql1."<br>";
if ($rows >= 1) {
$exist = "Please Change The Package Name";
echo $exist;
}
Then copy the query and paste it in your PhpMyAdmin ;) and check if there is any result.
Regards
advancetheweb.com 02-20-2006, 10:15 PM the () doesnt matter with the exit function. exit is a language construct so it does not require ()
the following will exit a php script normally
exit;
exit();
exit(0);
Once it hits the construct it terminates the php script, but it has to hit it. If it is embedded in an if that evaluates to false, it is obviously skipped.
if it is skipped when false then why doesn't it skip the exit when there are no rows returned?
Burhan 02-21-2006, 01:32 AM i have exit in if statements and as long as the if is false, the exit wont end the script
If the condition of the if statement is not true, then statements inside it's blocks are not executed, which is why your exit doesn't work. Example:
$x = 1;
if ($x == 0)
{
exit();
}
echo 'Hello there!';
This script will always print Hello there! because the if condition can never be true, hence the exit() call isn't reached.
if it is skipped when false then why doesn't it skip the exit when there are no rows returned?
Not sure what you mean here, because your condition is checking for the return of one or more rows. Here is a simple example:
$num_rows = 0;
if ($num_rows >= 1)
{
# this will never execute, as your if condition will never be true
echo 'Sorry, too many rows';
exit();
}
echo 'I will always be printed no matter what!';
# However, this if loop will exit
if ($num_rows < 1)
{
# The above condition is true, so this will end the script
echo 'Sorry, no rows returned';
exit();
}
thartdyke 02-21-2006, 01:39 AM $rows = mysql_num_rows($check) or die(mysql_error());
What exactly are you trying to do here? Check the value of $rows after this, as it may not be what you expect.
Toby
Burhan 02-21-2006, 01:57 AM That is exactly what this line of code does. It will quit with an error message, if the LHS does not evaluate to true.
advancetheweb.com 02-21-2006, 12:28 PM WHy does that exit not work and this one work:
if (($_POST[blah1] == "") || ($_POST[blah2] == "") || ($_POST[blah3] == "")) {
if ($_POST[blah1] =="") {
$msg .= "Please Enter a Bloo for the Blah blah<BR>";
}
if ($_POST[blah2] =="") {
$msg .= "Please Enter a Blah for the Blah Blah<BR>";
}
if ($_POST[blah3] =="") {
$msg .= "Please Enter a blee for the blah blah<BR>";
}
$msg .= "<BR><BR><a href=\"blahblah.php\">Writing</a>";
echo $msg;
exit;
}
and i checked the value of rows and it appears as nothing...i thought that it was supposed to be FALSE when there was no rows...
Korvan 02-21-2006, 06:02 PM If you dont use the identity operator (===) in php, any of the following will evaluate to false
0
NULL
'' (a string with nothing in it, thats 2 single quotes there to signify that)
false (any case)
'0'
Ref: http://us2.php.net/manual/en/types.comparisons.php
Here is the thing that is happeing:
If $rows in your script is blank, when it is compared with the >= operatior it is treated AS IF it was 0 because 0 >= 1 evaluates to false, it skips that exit.
thartdyke 02-21-2006, 07:51 PM Exactamundo :-)
advancetheweb.com 02-21-2006, 07:58 PM alright, thanks Korvan
|