query not working

Discussion in 'PHP' started by kharearch, Dec 11, 2007.

  1. #1
    I have written a query to find matching record from the table. At the end of query I want to find out whether any row is selected in response of query. I have written following query.

    <body>
    <?php
    $con = mysql_connect("localhost","root");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }mysql_select_db("shopping_cart", $con);
    $s="SELECT * FROM user where name ='" . $_POST[name] . "' and password = '" .$_POST[passwd]."'";
    $result = mysql_query($s);
    $row = mysql_fetch_array($result);
    if(mysql_num_rows($row>0))
    {
    echo "welcome";
    }
    else
    {
    echo "not a valid user";
    }

    ?>
    </body>

    But this query is giving error. I am not clear, what is error in following query. Please help me.


    Following error is coming.

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\xampplite\htdocs\practice\shop_cart\query.php on line 21

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampplite\htdocs\practice\shop_cart\query.php on line 22
    not a valid user
     
    kharearch, Dec 11, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try this:

    <?php
    $con = mysql_connect("localhost","root");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }mysql_select_db("shopping_cart", $con);
    $s="SELECT * FROM user where name ='" . $_POST[name] . "' and password = '" .$_POST[passwd]."'";
    $result = mysql_query($s);
    
    if(mysql_num_rows($result))
    {
    echo "welcome";
    }
    else
    {
    echo "not a valid user";
    }
    
    ?>
    PHP:
    Brew
     
    Brewster, Dec 11, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Dec 11, 2007 IP
  4. sm9ai

    sm9ai Active Member

    Messages:
    746
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Echo out the value of $s to see what it contains.

    Then paste it directly into sql on phpmyadmin (or equivalent)
     
    sm9ai, Dec 11, 2007 IP
  5. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You have no result to call mysql_fetch_array() for, your query returns 0 results - Brewster accounts for this in his code example.

    Once you fix this make sure you cleanse the $_POST's or you will have a huge security hole!
     
    Gawk, Dec 11, 2007 IP