Web Analytics Made Easy -
StatCounter Mysterious error - CodingForum

Announcement

Collapse
No announcement yet.

Mysterious error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mysterious error

    I am just learning coding, but I have a friend who is helping me with learning. But anyway, I have this code, it's supposed to add a random number 1-10 in a database, and I am having an error with it. He is the one who told me to add "or die mysql_error();" at the end of the mysql_queries. But before that I had some error (I can't remember what it was) when I clicked on the button. But now I have an error just when loading the page. The error is "Parse error: parse error, unexpected T_STRING in H:\Program Files (x86)\xampp\htdocs\Test.php on line 12" And here's the code:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    if (isset($_POST['update']))
    {
        
    mysql_connect('localhost''root''');
        
    mysql_select_db('a');
        
    mysql_query('CREATE TABLE IF NOT EXISTS `b` (
            `d` INT NOT NULL AUTO_INCREMENT ,
            `c` INT NOT NULL ,
            PRIMARY KEY ( `d` )
            ) TYPE = innodb;'
    ) or die mysql_error();
        
    mysql_query('INSERT INTO `b`(`c`, `d`) ;
            VALUES (' 
    mt_rand(110) . ', 1) 
            ON DUPLICATE KEY UPDATE `c` = `c` + ' 
    mt_rand(1,10)) or die mysql_error();
        
    $q mysql_query('SELECT `c` 
            FROM `b` 
            WHERE `d` = 1 
            LIMIT 1'
    ) or die mysql_error();
        
    $r mysql_fetch_assoc($q);
        
    print_r($r);
    }

    ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>This is my basic page!</title>
    Hello, if you got to this, you are hacking my computer. Please exit in a timely fashion otherwise I will be forced to <b>eat</b> your family. And I'll do it too. Don't believe me? Stay here then, when you get up to go to get something to eat, you'll see me cooking up your sister.
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input name="update" type="submit" value="Test" />
    </form>
    </body>
    </html>

  • #2
    here is line 12
    PHP Code:
    TYPE innodb;') or die mysql_error(); 

    Comment


    • #3
      ... or die(mysql_error());
      Though die() is not really a function it requires the argument, in this case the value returned by mysql_error(), in parentheses.
      I'm not sure if this was any help, but I hope it didn't make you stupider.

      Experience is something you get just after you really need it.
      PHP Installation Guide Feedback welcome.

      Comment


      • #4
        Thanks, but now when I try it, I don't get an actual error, but I get "No database selected"

        Comment


        • #5
          is your database called 'a'? or is it something else?

          I'd say to change the name of the database if it is to something meaningful. DO the same with the table name and table columns.

          Comment


          • #6
            It is indeed a, but I'll try changing it.

            I changed it and it's still the same situation. Here's the updated code though:

            PHP Code:
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <?php
            if (isset($_POST['update']))
            {
                
            mysql_connect('localhost''root''');
                
            mysql_select_db('TestA');
                
            mysql_query('CREATE TABLE IF NOT EXISTS `TestB` (
                    `TestD` INT NOT NULL AUTO_INCREMENT ,
                    `TestC` INT NOT NULL ,
                    PRIMARY KEY ( `TestD` )
                    ) TYPE = innodb;'
            ) or die (mysql_error());
                
            mysql_query('INSERT INTO `TestB`(`TestC`, `TestD`) ;
                    VALUES (' 
            mt_rand(110) . ', 1) 
                    ON DUPLICATE KEY UPDATE `Testc` = `TestC` + ' 
            mt_rand(1,10)) or die (mysql_error());
                
            $q mysql_query('SELECT `TestC` 
                    FROM `TestB` 
                    WHERE `TestD` = 1 
                    LIMIT 1'
            ) or die (mysql_error());
                
            $r mysql_fetch_assoc($q);
                
            print_r($r);
            }

            ?>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <title>This is my basic page!</title>
            Hello, if you got to this, you are hacking my computer. Please exit in a timely fashion otherwise I will be forced to <b>eat</b> your family. And I'll do it too. Don't believe me? Stay here then, when you get up to go to get something to eat, you'll see me cooking up your sister.
            </head>

            <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input name="update" type="submit" value="Test" />
            </form>
            </body>
            </html>
            Last edited by Cannibal_Monkey; Sep 26, 2006, 07:53 PM.

            Comment


            • #7
              change this line:
              PHP Code:
              mysql_select_db('a'); 
              to
              PHP Code:
              mysql_select_db('a') or die(mysql_error()); 
              otherwise you aren't generating an error message when you try to connect to your database.

              Comment


              • #8
                I did that and now I have "Unknown database 'a'" Or "Unknown database 'testa'" depending on which code I am using it with, the one where I say test before the letter or without.

                Comment


                • #9
                  Any other answers?

                  Comment


                  • #10
                    The error means that the database you're tryingto select does not exist. You'd have to create it first.
                    I'm not sure if this was any help, but I hope it didn't make you stupider.

                    Experience is something you get just after you really need it.
                    PHP Installation Guide Feedback welcome.

                    Comment


                    • #11
                      How do I do that?

                      Comment


                      • #12
                        If you have the permissions you can just run the query:
                        Code:
                        CREATE DATABASE `database_name`;
                        I'm not sure if this was any help, but I hope it didn't make you stupider.

                        Experience is something you get just after you really need it.
                        PHP Installation Guide Feedback welcome.

                        Comment


                        • #13
                          is your table called a or your database? be carefull not to make the mistake the difference between the 2, a database has tables in, tables have information!

                          Comment

                          Working...
                          X