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>";            }

$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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.