Home Backend Development PHP Tutorial 头一次写递归函数,烦请各位看看错哪里了,该如何处理

头一次写递归函数,烦请各位看看错哪里了,该如何处理

Jun 13, 2016 pm 01:53 PM
check mysql nbsp quot

头一次写递归函数,烦请各位看看错哪里了
function   check($num) {
                  $sn=$num;
$sql= "select   *   from   `a_dinghuo`   where   `num`= '$num ' ";
$q=mysql_query($sql);
$row=mysql_num_rows($q);
if($row==1) {
$sn=($num+1);
check($num+1);
}
return   $sn;
}

我的思路是如果数据库本身有这个数值,那么该数值+1
可是,我最后返回的还是原来的参数...请问哪里错了
小弟在此多谢了


------解决方案--------------------
因为你的数据库里没有那个数。
------解决方案--------------------
function check($num) {
$sn=$num;
$sql= "select * from `a_dinghuo` where `num`= '$num ' ";
$q=mysql_query($sql);
$row=mysql_num_rows($q);
if($row==1) {
$sn = check($num+1);// 修改这里
}
return $sn;
}
这样修改一下,不过在这里看不出递归的作用来
------解决方案--------------------
如果有的话,你的递归就会死循环,出不来了
------解决方案--------------------
确实是一个死循环!应该加上一个递归跳出!比如说判定2次或者超过某个数就跳出!

function check($num){
static $array = array(1,2,3,4,5,6,7,8,9,10);
$sn=$num;
if(in_array($num, $array)){
print $num. ' ';
$sn=($num+1);
check($num+1);
}
return $sn;
}
print check(3);
?>


输出:3 4 5 6 7 8 9 10 4
如果你的数据库num记录很多且是序列的话,你会发现你的mysql语句执行的很高
------解决方案--------------------
楼上死循环不会吧,仔细看下
$num+1 ......
------解决方案--------------------
$sql= "select * from `a_dinghuo` where `num`= '$num ' ";

check($num+1);

$num应该还是要类型统一下吧..
------解决方案--------------------
function check($num){
static $sn;
$sql= "select * from `a_dinghuo` where `num`= '$num ' ";
$q=mysql_query($sql);
$row=mysql_num_rows($q);
if($row==1){
$sn .= ($num+1);
check($num+1);
}
return $sn;
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

Docker builds LNMP environment: Is a single Dockerfile or Docker Compose better? Docker builds LNMP environment: Is a single Dockerfile or Docker Compose better? Apr 01, 2025 pm 02:09 PM

Dockerfile Best Practice for Building LNMP Environment Learning During Docker, many developers try to build their own LNMP (Linux, Nginx, MySQL, PHP)...

Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? Apr 01, 2025 pm 02:24 PM

Comparison of Redis queues and MySQL stability: Why is Redis prone to data loss? In the development environment, using PHP7.2 and ThinkPHP frameworks, we often face the choice of cooperation...

When using Django and MySQL to process hundreds of thousands to one or two million pieces of data, what kind of cache solution should a 4-core 8G memory server choose? When using Django and MySQL to process hundreds of thousands to one or two million pieces of data, what kind of cache solution should a 4-core 8G memory server choose? Apr 01, 2025 pm 11:36 PM

Using Django and MySQL to process large data volumes When using Django and MySQL databases, if your data volume reaches hundreds of thousands to one or two million...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

How to apply Debian Strings in a website How to apply Debian Strings in a website Apr 02, 2025 am 08:21 AM

This article discusses how to optimize website performance on Debian systems. "DebianStrings" is not a standard term and may refer to tools or technologies used in Debian systems to improve website performance. The following are some practical tips: 1. It is recommended to use the Pagoda panel to simplify the installation and configuration process for web server and PHP environment configuration. It is recommended to install Nginx1.22.1 as the web server, PHP8.2 as the script interpreter, and MySQL10.7.3-MariaDB as the database system. Be sure to enable the necessary PHP extensions, such as fileinfo, opcache, memcached, red

See all articles