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
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
Echo out the value of $s to see what it contains. Then paste it directly into sql on phpmyadmin (or equivalent)
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!