Page 1 of 1

Can you please help me debug this code?

Posted: Mon Sep 08, 2008 6:13 pm
by zunebuggy
I get an unexpected T_WHILE error. Obviously I'm very new at PHP.

Code: Select all

 
<?php
$hostname =  "mysql.mydomain.com";
$username =  "myusername";
$password =  "mypassword";
$dbName =  "mydbname";
$table = "mytablename";
 
MYSQL_CONNECT($hostname, $username, $password) OR DIE( "Unable to connect
to database");
 
    cond == False
    while(!cond)    {
        srand((unsigned)time(NULL));
        int a1 = rand() % 26 + 1;
        srand((unsigned)time(NULL));
        int a2 = rand() % 26 + 1;
        srand((unsigned)time(NULL));
        int a3 = rand() % 26 + 1;
        srand((unsigned)time(NULL));
        int n1 = rand() % 10;
        srand((unsigned)time(NULL));
        int n2 = rand() % 10;
        srand((unsigned)time(NULL));
        int n3 = rand() % 10;
        srand((unsigned)time(NULL));
        int n4 = rand() % 10;
        srand((unsigned)time(NULL));
        int n5 = rand() % 10;
        srand((unsigned)time(NULL));
        int n6 = rand() % 10;
        srand((unsigned)time(NULL));
        int n7 = rand() % 10;
        srand((unsigned)time(NULL));
        int n8 = rand() % 10;
        $rndaccount = chr(a1+ 65) . chr(a2 + 65) . chr(a3 + 65) . n1 . n2 . n3 . n4. n5 . n6 . n7 . n8
        if(mysql_num_rows(mysql_query("SELECT account_num FROM mytablename WHERE account_num = '$rndaccount'"))){
            // do nothing
                                                        }
        else
            $finaccount = $rndaccount
            cond = true
            echo $finaccount    
            }
?>
 

Re: Can you please help me debug this code?

Posted: Mon Sep 08, 2008 6:22 pm
by jayshields
You need to download a syntax highlighting IDE and learn some very basic PHP syntax. Variables must be preceeded by a dollar sign "$" and every statement must end with a semi-colon ";".

Re: Can you please help me debug this code?

Posted: Mon Sep 08, 2008 7:45 pm
by 8ennett
Basically, from what i can see (and I have had a couple but it's obvious without looking through the rest of the code) at the very end your statement is EXACTLY;

Code: Select all

#         else
#             $finaccount = $rndaccount
#             cond = true
#             echo $finaccount   
#             }
You are forgetting the { after ELSE, haven't looked through the rest of the code but i am new to PHP myself and noticed that straight off (as any good language programmer should!)!

# <?php
# $hostname = "mysql.mydomain.com";
# $username = "myusername";
# $password = "mypassword";
# $dbName = "mydbname";
# $table = "mytablename";
#
# MYSQL_CONNECT($hostname, $username, $password) OR DIE ( "Unable to connect
# to database");
#
# $cond == False
# while(!cond) {
# srand((unsigned)time(NULL));
# int a1 = rand() % 26 + 1;
# srand((unsigned)time(NULL));
# int a2 = rand() % 26 + 1;
# srand((unsigned)time(NULL));
# int a3 = rand() % 26 + 1;
# srand((unsigned)time(NULL));
# int n1 = rand() % 10;
# srand((unsigned)time(NULL));
# int n2 = rand() % 10;
# srand((unsigned)time(NULL));
# int n3 = rand() % 10;
# srand((unsigned)time(NULL));
# int n4 = rand() % 10;
# srand((unsigned)time(NULL));
# int n5 = rand() % 10;
# srand((unsigned)time(NULL));
# int n6 = rand() % 10;
# srand((unsigned)time(NULL));
# int n7 = rand() % 10;
# srand((unsigned)time(NULL));
# int n8 = rand() % 10;
# $rndaccount = chr(a1+ 65) . chr(a2 + 65) . chr(a3 + 65) . n1 . n2 . n3 . n4. n5 . n6 . n7 . n8
# if(mysql_num_rows(mysql_query("SELECT account_num FROM mytablename WHERE account_num = '$rndaccount'"))){
# // do nothing
# }
# else {
# $finaccount = $rndaccount
# $cond = true
# echo $finaccount
# }
# ?>

Here is the amended code as far as I can see, I have added { to the end of the last ELSE statement, also I have added a $ to the COND variable because that is NOT a PHP function!

How is that, better or not? Am guessing that COND is a custom function or variable change, if not AND i am wrong then please tell me as again, I am new to PHP myself but not new to coding language! but I can tell you now it does NOT appear to be correct! I have added the $ sign to the two COND variables that appear in the above code, one is in the IF statement and one is in the ELSE statement!

The code above should work properly now, depending on wether your variables are accurate or not of course!

Re: Can you please help me debug this code?

Posted: Mon Sep 08, 2008 8:15 pm
by 8ennett
also, jayshields, to a new php programmer what you said is a very obvious over-the-head issue. The guy stated he was a newbie to the code same as I am but you didn't give an obvious statement as to what you meant or a proper coding syntax, help the guy out a bit and not just show your understanding of the environment compared to his!

Show him where he has gone wrong, correct him and help him to develop his skills and tactics and not just assume he knows the lingo or not. Vague tactics are the work of a Jedi, it seems to make sense to him but he doesn't have a fook what you are going on about!

Re: Can you please help me debug this code?

Posted: Mon Sep 08, 2008 8:21 pm
by 8ennett
oops, missed out on

cond == false

am guessing there should be some kind of statement there too unless am incorrect. If I am wrong then please don't hesitate to correct me, I'm only learning as well!

Re: Can you please help me debug this code?

Posted: Tue Sep 09, 2008 5:26 am
by jayshields
8ennett wrote:also, jayshields, to a new php programmer what you said is a very obvious over-the-head issue.
I could have fixed his entire snippet in the same time it took me to post my previous comment, but he won't learn. His mistakes are very simple, and could have been rectified had he taken about 2 minutes to read any PHP tutorial on the internet, including the one in the PHP manual.

By the way, there are still quite a few errors in your revised snippet.

Re: Can you please help me debug this code?

Posted: Tue Sep 09, 2008 8:57 am
by zunebuggy
OK. I got it to work finally. It prints out the random account number and does not seem to duplicate any already in my database. But along with displaying the acount number, I get the following warning:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /nfs/cust/8/76/13/831678/web/joes.script.php on line 27

I think it is a matter of adding a die command to the If/SQL Query line to supress this warning, but I cannot figure out the proper syntax. My updated code is here:

Code: Select all

 
<?php
    $hostname =  "mysql.mydomain.com";
    $username =  "myusername";
    $password =  "mypassword";
    $dbName =  "mydbname";
    $table = "mytablename";
 
    $conn = mysql_connect($hostname, $username, $password) OR 
 
DIE( "Unable to connect to database");
 
    $cond = False;
    while(!$cond) {
        srand((int)time(NULL));
        $a1 = rand() % 26 + 1;
        $a2 = rand() % 26 + 1;
        $a3 = rand() % 26 + 1;
        $n1 = rand() % 10;
        $n2 = rand() % 10;
        $n3 = rand() % 10;
        $n4 = rand() % 10;
        $n5 = rand() % 10;
        $n6 = rand() % 10;
        $n7 = rand() % 10;
        $n8 = rand() % 10;
        
        $rndaccount = chr($a1 + 65) . chr($a2 + 65) . 
 
chr($a3 + 65) . $n1 . $n2 . $n3 . $n4. $n5 . $n6 . $n7 . $n8;
        
        if(mysql_num_rows(mysql_query("SELECT account_num 
 
FROM mytablename WHERE account_num = '" . $rndaccount . 
 
"'",$conn))) {
            // do nothing
        } else {
            $finaccount = $rndaccount;
            $cond = true;
            echo $finaccount;
        }
    }
?>
 
Thank you fo all your help.

Re: Can you please help me debug this code?

Posted: Tue Sep 09, 2008 9:24 am
by jayshields
Probably a query error. Changing this

Code: Select all

if(mysql_num_rows(mysql_query("SELECT account_num
FROM mytablename WHERE account_num = '" . $rndaccount .
"'",$conn))) {
to this

Code: Select all

$query = "SELECT account_num FROM mytablename WHERE account_num = '" . $rndaccount ."'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)) {
will accomplish the same thing but help you debug it.

mysql_num_rows() returns an integer, so I don't really understand what you're testing for in your if() statement. With what you've got now, I think the if() statement will be true if any amount of rows are returned other than 0, but I'm not sure without testing it.

Re: Can you please help me debug this code?

Posted: Tue Sep 09, 2008 12:13 pm
by zunebuggy
I changed it and the error went away but now MySQL is simply returning "No database selected".

Re: Can you please help me debug this code?

Posted: Tue Sep 09, 2008 12:37 pm
by zunebuggy
Got it! instead of mytabelname I used mydbname.mytabelname and it worked. Thank you.

Re: Can you please help me debug this code?

Posted: Mon Sep 22, 2008 8:38 pm
by 8ennett
jayshields wrote:
8ennett wrote:also, jayshields, to a new php programmer what you said is a very obvious over-the-head issue.
I could have fixed his entire snippet in the same time it took me to post my previous comment, but he won't learn. His mistakes are very simple, and could have been rectified had he taken about 2 minutes to read any PHP tutorial on the internet, including the one in the PHP manual.

By the way, there are still quite a few errors in your revised snippet.
Yeah I'm sorry about that, I'd had a few drinks earlier in the night and wasn't really thinking straight, thought I could jump straight in with the big boys lol

But yeah, sorry again about the previous posts in this topic!