Simple mysql code doesn't run, why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luke14free
    New Member
    • Apr 2007
    • 79

    Simple mysql code doesn't run, why?

    Hello, i have projected a page for voting my website, i have created a mysql tab with 2 INT fields (total votes and number of voters) and an id (working). Now I have problems running this code, why?
    [PHP]<?php
    $user="root";
    $password="";
    $database="mydb ";
    $db=mysql_conne ct('localhost', $user,$password );
    mysql_select_db ($database) or die( "Unable to select database");
    $sql = 'SELECT * FROM `vote1` LIMIT 0, 30 ';
    $a = mysql_query($sq l) or die("e");
    print "1";
    while ($row = mysql_fetch_arr ay($a)){
    print "2";
    $val=$row['val'];
    $part=$row['part'];
    print $val."--".$part;
    }
    print "3";
    if (isset($_GET['vote'])){
    print "4";
    $newval=$val+$_ GET['vote'];
    $part=$part+1;
    $q="UPDATE vote1 SET val = '$newval'";
    $a = mysql_query($q, $db);
    $q="UPDATE vote1 SET part = '$part'";
    $a = mysql_query($q, $db);
    echo "<a href=\"vote.php \">Results</a>";
    }else{
    echo "Media from 0( - ) to 4 ( + ) is: ".$val/$part;
    }
    ?>[/PHP]
  • rpnew
    New Member
    • Aug 2007
    • 189

    #2
    Originally posted by luke14free
    Hello, i have projected a page for voting my website, i have created a mysql tab with 2 INT fields (total votes and number of voters) and an id (working). Now I have problems running this code, why?
    [PHP]<?php
    $user="root";
    $password="";
    $database="mydb ";
    $db=mysql_conne ct('localhost', $user,$password );
    mysql_select_db ($database) or die( "Unable to select database");
    $sql = 'SELECT * FROM `vote1` LIMIT 0, 30 ';
    $a = mysql_query($sq l) or die("e");
    print "1";
    while ($row = mysql_fetch_arr ay($a)){
    print "2";
    $val=$row['val'];
    $part=$row['part'];
    print $val."--".$part;
    }
    print "3";
    if (isset($_GET['vote'])){
    print "4";
    $newval=$val+$_ GET['vote'];
    $part=$part+1;
    $q="UPDATE vote1 SET val = '$newval'";
    $a = mysql_query($q, $db);
    $q="UPDATE vote1 SET part = '$part'";
    $a = mysql_query($q, $db);
    echo "<a href=\"vote.php \">Results</a>";
    }else{
    echo "Media from 0( - ) to 4 ( + ) is: ".$val/$part;
    }
    ?>[/PHP]
    Hi,
    From your code it seems that you are not using any password or blank password so instead of

    [PHP]
    $db=mysql_conne ct('localhost', $user,$password );
    [/PHP]

    use
    [PHP]
    $db=mysql_conne ct('localhost', $user);
    [/PHP]

    you can also remove the
    [PHP]
    $password="";
    [/PHP]
    line from the code....

    RP

    Comment

    • luke14free
      New Member
      • Apr 2007
      • 79

      #3
      I think that it doesnt matter, password or not password the code should run, and i dont need the password working in local apache. Thanks, maybe any idea about the db structure? I don't know how I could help you more helping me....

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        don't know how I could help you more helping me....
        Well how about a little more info than "it should run".
        What is happening.
        What errors are you getting.
        What is output to screen

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          I couldn't get a clear picture on your application yet. by going through your code snippet, I would like say this.

          Instead of assigning the UPDATE queries on a different variables that uses same memory location, how about using a single update query.

          Comment

          • luke14free
            New Member
            • Apr 2007
            • 79

            #6
            Oh yes i forgot to explain what it's supposed to do:

            1)No error, simply it doesn't run the code inside the foreach($row as my_sql...


            2)it's a voting system and it work like that

            Main Page--link--var vote in get-->vote.php
            That page takes the vote if is isset the var vote in get, else it gives you the results of the votes

            Please, could you make an example of using single query instead of double?

            3) working on php 4 and apache (easyphp1.8) in local

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              [CODE=sql]UPDATE TABLE_NAME SET
              COL1 = 'VALUE1',
              COL2 = 'VALUE2'
              WHERE COL3 = 'SOME_ID'[/CODE]

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                No error, simply it doesn't run the code inside the foreach($row as my_sql...
                Well that could be because you don't have a foreach loop.
                Lets assume you mean this while loop.
                [PHP]while ($row = mysql_fetch_arr ay($a)){[/PHP]
                If that is the case then this query retuned 0 records.
                Code:
                SELECT * FROM `vote1` LIMIT 0, 30
                But I am only guessing because you told me what you HOPE to see output.
                Not the actual output.
                May I suggest putting in some error trapping and echoing of variables,
                ie [PHP]$a = mysql_query($sq l) or die("e");
                print_r($a);[/PHP]

                Comment

                • luke14free
                  New Member
                  • Apr 2007
                  • 79

                  #9
                  Thanks for help, hu yes i meant the while one, sorry...
                  My problem is that when i execute my code
                  "SELECT * FROM vote WHERE 1" from phpmyadmin i get the right result but when i try to do that from the php file i get null result...even if the query should return at least one result.
                  The sintax is correct but i don't have any output. That's is...Thanks for collaborating

                  Comment

                  • jx2
                    New Member
                    • Feb 2007
                    • 228

                    #10
                    Originally posted by luke14free
                    Thanks for help, hu yes i meant the while one, sorry...
                    My problem is that when i execute my code
                    "SELECT * FROM vote WHERE 1" from phpmyadmin i get the right result but when i try to do that from the php file i get null result...even if the query should return at least one result.
                    The sintax is correct but i don't have any output. That's is...Thanks for collaborating
                    i think you should start from [php]
                    echo mysql_error();
                    [/php]
                    at this line at the end of your script it should help ypou to find your error
                    regards
                    jx2

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      If I am adding something to the jx2's suggestion. Just add this line instead of your one. Since you haven't use any password to log in to the mySQL server may be you are passing empty space as the password may be not, any way you can make sure what is going on there with this line.
                      [CODE=php]
                      $db=@mysql_conn ect('localhost' ,$user,$passwor d) or die(mysql_error ());
                      [/CODE]

                      If you are not passing any password.
                      Code:
                      Access denied for user 'ODBC'@'localhost' (using password: NO)
                      If there is a space on your password variable.
                      Code:
                      Access denied for user 'ODBC'@'localhost' (using password: YES)

                      Comment

                      • luke14free
                        New Member
                        • Apr 2007
                        • 79

                        #12
                        Hi guys, thanks for help but this morning with a hot cup of coffy and a bit of determination I've had this code running
                        [PHP]<?php
                        $user="root";
                        $password="";
                        $database="mydb ";
                        $db=mysql_conne ct("localhost", $user,$password );
                        @mysql_select_d b($database);
                        $sql = 'SELECT val,part FROM `vote` WHERE id=1 LIMIT 0, 30 ';
                        $a = mysql_query($sq l, $db);
                        while ($row=mysql_fet ch_array($a)){
                        $val=$row['val'];
                        $part=$row['part'];
                        }
                        if (isset($_GET['vote'])){
                        $val+=$_GET['vote'];
                        $part+=1;
                        $q="UPDATE vote SET part = '$part',val = '$val' WHERE id=1";
                        $a = mysql_query($q, $db) or die("e");
                        echo "<a href=\"vote.php \">Results</a>";
                        }else{
                        echo "Media of votes from 0( - ) to 4 ( + ) is: ".$val/$part;
                        }
                        ?>[/PHP]
                        Thanks another time for help!

                        Comment

                        • ak1dnar
                          Recognized Expert Top Contributor
                          • Jan 2007
                          • 1584

                          #13
                          Glad you got it working.
                          Good luck, may be next time :D

                          Comment

                          Working...