Jump to content

help in storing a database value to a php variable


fsl4faisal

Recommended Posts

i want to store a value from a database

and use it as variable in php code

 

can anyone help me out

in this code i want to store value of copies

in a php variable

 

and want that it should be more than 0 (zero)

 

for furthur calculations

 

 

 

$update_book="update book set copies=copies-1 where bookid='$bookid'";        $result=mysql_query($update_book,$linkID1);            if($result)            {            print "<html><body background=\"header.jpg\">            <p>book successfully subtracted from database</p></body></html>";            }            else {            print "<html><body background=\"header.jpg\">            <p>problem occured</p></body></html>";            }
Link to comment
Share on other sites

$select_copies = 'SELECT copies FROM book WHERE bookid= \''.$bookid.'\'';
$select_result = mysql_query($select_copies, $linkID1);
if ($select_result && mysql_num_rows($select_result) > 0) {
   $copies_array = mysql_fetch_assoc($select_result);
   $num_of_copies = $copies_array['copies']; // $num_of_copies is the variable that contains the number of copies
}
else {
// Invalid $bookid, process error here
}

 

This code assumes that every $bookid is for a unique book and therefor will only return 1 or 0 results (0 results if that $bookid does not exist).

 

That what you're looking for?

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.