English 中文(简体)
如果有的话,则保持检查、检查和重复。 while in
原标题:If exists, keep on looking, check again and repeat. Do while loop in PHP
  • 时间:2012-01-13 22:08:30
  •  标签:
  • php
$number = 1;
do{
    $list_name = "Unnamed list ".$number;

    $list_name_exists = DB::select( name )->from( shoppinglists )->where( name ,  = , $list_name)->execute()->count(); // returns 1 or 0

    $number++;
}while($list_name_exists == 0);
echo $list_name; // I want it here to output a name, that doesnt exists in the db.

上文试图做的是,将清单——姓名——定为在b中已经不存在的名字。

但这似乎并不可行,页数相当长。

如何做到这一点?

问题回答

你们需要把名单单列一栏,以便你能够这样做。

SELECT MAX(list_number) + 1 AS new_number FROM lists

并立即找到你应该使用的数量。 仅仅提出10 000个问题就没有意义,以便你能够发现下一个现有数字是10001个。

That said, it is still possible that by the time you get to using new_number it will no longer be valid (because perhaps another request from another user has taken up the same number). This will depend on the transaction isolation level of the database and of course if you are using transactions or not.

如果你不想深入到交易领域,你可以简单地保证在<条码>上有一个<> UNI Request/code>或PRIMaire, 关键内容是<条码>。 你们还必须保持无选择的循环。 不同之处在于,现在该休息室将用几倍时间执行,可能还有数万次。

你的发言稿需要很长的时间才能完成,因为你重新对你<代码>do/while的每份通行证进行数据库查询。 我只是从你的数据库中挑选出最高身份识别器,并将(加上一)作为你的新名单。

Pseudo Code:

<?php

// initiate database connection
$sql = "SELECT MAX(id) FROM tbl_name";
$res = $db->query($sql);
$row = $res->fetch();

$new_list_name = (intval($row) + 1);

唯一的下载是,如果两人访问贵方,在把名单保留回数据库之前,他们可以找到一个种族条件。 在这种情况下,你只能使用一种独特的身份识别器。

<?php

// make a unique ID to use as list name
$new_list_name = uniqid();

简单。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签