我如何检查数据库条目的值以及何时将其更新为1时将其更新为1?
目前它总是以Number Not used
回来,即使我在数据库中将0设置为1我自己....
表:
=============================================== =========
MasterNumbers:
number | used
01513242 | 0
01012233 | 0
23566545 | 0
=============================================== =========
代码:
$name = "01513242";
$checkForUsed = mysql_query("SELECT * FROM MasterNumbers WHERE `used`= '$name'");
$numrowsUsed = mysql_num_rows($checkForUsed);
if ($numrowsUsed == 0 )
{
//update used (on 01513242) form 0 to 1
die("Number Not used");
}
else
{
die("Number is used");
}
答案 0 :(得分:1)
我没有检查以下class
,因为我不想为此设置database
。像这样的一些班级应该做的工作。
<?php
class update_numrow_class{
// Database settings
protected $_mysqli = false;
protected function __construct($number){
$this->username => 'db_username',
$this->password => 'db_password',
$this->host => 'localhost',
$this->database => 'db_name',
$this->charset => 'utf8',
$this->number => $number
}
// Function to connect to the database
function connect(){
$mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if ( $mysqli->connect_errno ) {
$this->_mysqli = false;
}else {
$mysqli->set_charset($this->charset);
$this->_mysqli = $mysqli;
}
}
// Function to execute an sql query
function execute_query($sql){
if($results = $this->_mysqli->query($sql)){
return $results;
}else{
return false;
}
}
// Function to check if the number was used
function check_number(){
$this->connect();
$number_result = $this->execute_query("SELECT used FROM MasterNumbers WHERE number =" . $this->number);
if($number_result == 0){
return false;
}else{
return true;
}
}
// Function to update the used value
function update_number(){
$number_check = $this->check_number();
if(!$number_check){
$this->connect();
$update_result = $this->execute_query("UPDATE MasterNumbers SET used = 1 WHERE number = " . $this->number);
return 'Number: ' . $this->number . ' successful updated.';
}else{
return 'Number: ' . $this->number . ' was already used.';
}
}
}
?>
将被用作:
<?php
require_once('update_numrow_class.php');
$update_object = new update_numrow_class(01513242);
$update_response = $update_object->update_number();
?>