jhvn147 Posted July 13, 2012 Share Posted July 13, 2012 Hi, Can you please show me how to querie my database with the following function correctly - please. The function: function price($tablename, $panname, $db, $host, $id, $pwd){ $conn = mysql_connect($host, $id, $pwd); //Connect to the database $sql = 'SELECT (price/100) FROM pans WHERE panname = $panname'; mysql_select_db('jhvn2'); $retval = mysql_query( $sql, $conn ); while($row = mysql_fetch_array($retval, MYSQL_NUM)) { print "Price: GBP $row[0]";//print the price on screen } mysql_free_result($retval); mysql_close($conn); } // Call the function above $price = price (pans, coppercasserole, jhvn2, localhost, jhvn2, R773716X); It seems that the use of the variable $panname is not right. Is the format correct? Please help. Thank you. jhvn147 Quote Link to comment https://forums.phpfreaks.com/topic/265646-using-a-variable-in-database-querie-does-not-work/ Share on other sites More sharing options...
scootstah Posted July 14, 2012 Share Posted July 14, 2012 If panname is not an int, you need quotes around it. Also, you are using single quotes but not concatenating - single quotes does not parse variables. $sql = "SELECT (price/100) FROM pans WHERE panname = '$panname'"; On a side note, you really shouldn't be defining your database connection in a function like that. The database connection should be defined in a static location, and only once. Furthermore, you don't need to explicitly close the database connection - PHP does that for you. Quote Link to comment https://forums.phpfreaks.com/topic/265646-using-a-variable-in-database-querie-does-not-work/#findComment-1361434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.