Jump to content

Using a variable in database querie does not work


jhvn147

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.