SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have a few basic or more advanced questions that seam quite newbie like although I always wanted a professional answer. So here I go:

    1. Is it possible to use an include file on the die function:

    mysql_connect("localhost", "bob", "bobs_pass") or die(include(die.html));

    Is the above possible in any way?


    2. How do you normally do if statements in the below case and which is 'better'

    $get = mysql_query("SELECT * FROM bobs_address");
    if(!get) {
    print "Hahahah";
    print mysql_error();
    } else {

    // OTHER STUFF HERE AND THEN ADD A } AT THE VERY END

    }


    or...

    $get = mysql_query("SELECT * FROM bobs_address");
    if(!get) {
    print "Hahahah";
    print mysql_error();
    exit;
    }


    3. I must check something for mutliple aspects:

    Check that $name isn't blank, doesn't contain numbers, and doesn't exist in the database. How would I easily do this. The way I currently do it would require } at the end of the script and at the end of a script there are about 1 million of them!

    They may seam simple and with different people there are different opinions, but if there a right way?

    Thanks
    Pete

  2. #2
    SitePoint Member
    Join Date
    Mar 2001
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pete:

    (1) You can't include anything within an or die statement with exception to mysql_error(); I believe you can set $die = "The same thing that is in die.html" and then do or die($die);.

    (2) What I would do for two is

    $get = mysql_query("SELECT * FROM bobs_address");
    if(!get) {
    print "Hahahah";
    die (mysql_error());

    Unless you need to continue using the script in which case I would use exit;

    (3) You could do that in one single if statement, but I like easy to read code:

    if ($name == "") { echo "something"; }
    if ( ereg ("([0-9])", $name) {echo "something"; }

    finally to check that it isn't in the database just call to the database.

    $call_to_database = "SELECT Name FROM tablename WHERE name='$name'";

    There are probably easier ways to do the above but off the top of my head that should work.

    Hope this helps!

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a very handy tip for keeping up with all of the { } - I was having huge trouble with them until a friend who is a Java programmer showed me this. Simply put each { on a new line and indent stuff after it - like this:
    Code:
    if (something)
    {
        // More code here
    
        while (something else)
        {
            // Yet more code
            if (thing)
            {
                // code
            }
            else
            {
                // more code
            }
        }
        // Even more code
    }
    As you can see, that does stretch code out over a few more lines. However the fact that every block is seperated makes it extremely easy to keep track of how many levels of { } you have used

    As for the die() question, although you can't use an include within a die there is no reason not to write your own version of the die function:
    PHP Code:
    function my_die()
    {
        
    // include the die.html file, then exit
        
    include('die.html');
        exit;

    Now instead of using die you can call my_die instead like this:
    PHP Code:
    mysql_connect("localhost""bob""bobs_pass") or my_die(); 
    Hope that helps

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, both of your replies help and I'll try that indenting things.

    Any more comments on the last question.
    Basically, if it fails any I need it to print "wrong, none entered" or "already in database" or "invalid." in an easy way.

    peter

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. This is taken from php.net hope helps
    void die (string message)

    die() outputs message and terminates parsing of the script. It does not return anything.

    Alternatively, die() will also accept a function as a parameter. That function will be executed before die() terminates parsing of the script
    but when you use include "filename.inc" it will print 1

    3. Make a function to check variable, because one day you will decide to change one of comparision style and have to change hundreds of line, if you use function, you will change only in function.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •