Jump to content

Using a PHP variable in an MySQL query


nickholt1972

Recommended Posts

I want to select items from my database where the id for the record is equal to the PHP variable being passed from the previous page. In theory the code should look something like...

[b]$productdetails = @mysql_query('SELECT * FROM stocks WHERE id = $_SESSION['cart'][$i]');[/b]

But I don't seem to be able to use a variable in a query.

Does anyone know of an alternative method for getting round this problem?

Thanks,

Nick.
Link to comment
Share on other sites

Enclose {$var} or dot ' . $var . '...

example (1)

// enclose method

[code]$productdetails = @mysql_query ( 'SELECT * FROM stocks WHERE id = {$_SESSION['cart'][$i]}' );[/code]

example (2)

// dot method

[code]$productdetails = @mysql_query ( 'SELECT * FROM stocks WHERE id = ' . $_SESSION['cart'][$i] );[/code]

Notice on the right side of the variable, there is no * [b]. '[/b] *, because it isn't needed when a variable beigins or ends a function(s) param value or a echo and print statement or any $varible assignment!

me!

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.