检查并更新数据库条目php

检查并更新数据库条目php

问题描述:

How do i check the value of a database entry and when its 0 update it to 1 in php?

At the moment it always comes back with Number Not used even when i set the 0 to 1 in the database my self....

Table:

========================================================

MasterNumbers:
number          |   used
01513242        |   0
01012233        |   0
23566545        |   0

========================================================

code:

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

我如何检查数据库条目的值以及何时将其更新为1时将其更新为1? p >

目前它始终以 Number Not used code>返回,即使我在数据库中将0设置为1我自己.... p> \ ñ

表: p>

================================= ======================= p>

  MasterNumbers:
number | 使用
01513242 |  0 
01012233 |  0 
23566545 |  0 
 代码>  PRE> 
 
 

================================ ======================== p>

代码: p>

  $ name =“01513242”; 
 $ checkForUsed = mysql_query(“SELECT * FROM MasterNumbers WHERE`used` ='$ name'”); 
 $ numrowsUsed = mysql_num_rows($ checkForUsed); 
 
if($ numrowsUsed)  == 0)
 {
 //更新使用(在01513242上)表格0到1 
骰子(“未使用数字”); 
} 
else 
 {
 die(“使用数字”)  ; 
} 
  code>  pre> 
  div>

I did not check the following class, since I don't want to set up a database for this. Some class like this should do the job.

<?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.';
            }
        }
    }
?>

Would be used like:

<?php
    require_once('update_numrow_class.php');
    $update_object = new update_numrow_class(01513242);
    $update_response = $update_object->update_number();
?>