search
HomeBackend DevelopmentPHP Tutorial这样建入口文件哪里错了呢

我在discuz根目录下建了一个名为test.php的文件,主要是仿制forum.php这个文件里面的内容,有些删了,代码如下

<?php/** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subject to license terms * *      $Id: forum.php 33828 2013-08-20 02:29:32Z nemohou $ */define('APPTYPEID', 88);define('CURSCRIPT', 'test');require './source/class/class_core.php';require './source/function/function_forum.php';$siteuniqueid = C::t('portal_category')->fetch('catname');while($value=DB::fetch($siteuniqueid)){	$data[]=$value;	}print_r($data);echo $data[0];?>

建这个文件的目录是做测试用的,比如有一个表portal_category,里面有一个字段catname,我想用这个新建文件做试验,打印出catname字值下的所有的值,但是在地址栏中输入http://localhost/test.php 后,结果是空白,但不报错,不知为啥


回复讨论(解决方案)

本人也是个菜鸟,最开始遇到过变白的原因变量没加 $,后来是函数声明的那个文件没有引进来。。

自己在认为可能有错的地方 echo "" 一些东西,看看具体是哪错了,慢慢试把。。

本人也是个菜鸟,最开始遇到过变白的原因变量没加 $,后来是函数声明的那个文件没有引进来。。

自己在认为可能有错的地方 echo "" 一些东西,看看具体是哪错了,慢慢试把。。


可是我都加了,这是其一,另外你说慢慢试试,用不着提醒吧,象这种没啥意思的帖子尽量别回.有意义吗?

建discuz的入口文件,只需要如下两句

require_once './source/class/class_core.php';C::app()->init();

你没有C::app()->init();


本人也是个菜鸟,最开始遇到过变白的原因变量没加 $,后来是函数声明的那个文件没有引进来。。

自己在认为可能有错的地方 echo "" 一些东西,看看具体是哪错了,慢慢试把。。


可是我都加了,这是其一,另外你说慢慢试试,用不着提醒吧,象这种没啥意思的帖子尽量别回.有意义吗?


sorry

建discuz的入口文件,只需要如下两句

require_once './source/class/class_core.php';C::app()->init();

你没有C::app()->init();


<?php/** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subject to license terms * *      $Id: forum.php 33828 2013-08-20 02:29:32Z nemohou $ */define('APPTYPEID', 88);define('CURSCRIPT', 'test');require './source/class/class_core.php';require './source/function/function_forum.php';C::app()->init();$siteuniqueid = C::t('portal_category')->fetch('catname');while($value=DB::fetch($siteuniqueid)){	$data[]=$value;	}print_r($data);?>

我这样加上了,还是打印不出来,空白,没有提示报错

$siteuniqueid = C::t('portal_category')->fetch('catname');
fetch括号里面只能是主键id,也就说只能为数字(可以查看\source\class\discuz\discuz_table.php里面的fetch)
用C类执行后不需要,返回的不是资源类型,你不需要用DB::fetch
实际上只有用DB::query查询的语句,返回的才是资源类型
所以,你应该改为:
$data = C::t('portal_category')->fetch(1);
print_r($data);

$siteuniqueid = C::t('portal_category')->fetch('catname');
fetch括号里面只能是主键id,也就说只能为数字(可以查看\source\class\discuz\discuz_table.php里面的fetch)
用C类执行后不需要,返回的不是资源类型,你不需要用DB::fetch
实际上只有用DB::query查询的语句,返回的才是资源类型
所以,你应该改为:
$data = C::t('portal_category')->fetch(1);
print_r($data);


这回行了,下面是正确的代码,还有一点有疑惑:你说 实际上只有用DB::query查询的语句,返回的才是资源类型
但是我的代码中没有去掉DB::query为啥还能正确打印呢
<?php/** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subject to license terms * *      $Id: forum.php 33828 2013-08-20 02:29:32Z nemohou $ */define('APPTYPEID', 88);define('CURSCRIPT', 'test');require './source/class/class_core.php';require './source/function/function_forum.php';C::app()->init();$data = C::t('portal_category')->fetch(1);while($value=DB::fetch($siteuniqueid)){	$data[]=$value;	}print_r($data);?>

你在fetch(1)中放的是1,如果我想把这个表中所有的数据都打印出来怎么办呢
我试着写fetch('catid'')
显示的打印结果是Array呢


$siteuniqueid = C::t('portal_category')->fetch('catname');
fetch括号里面只能是主键id,也就说只能为数字(可以查看\source\class\discuz\discuz_table.php里面的fetch)
用C类执行后不需要,返回的不是资源类型,你不需要用DB::fetch
实际上只有用DB::query查询的语句,返回的才是资源类型
所以,你应该改为:
$data = C::t('portal_category')->fetch(1);
print_r($data);


这回行了,下面是正确的代码,还有一点有疑惑:你说 实际上只有用DB::query查询的语句,返回的才是资源类型
但是我的代码中没有去掉DB::query为啥还能正确打印呢
<?php/** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subject to license terms * *      $Id: forum.php 33828 2013-08-20 02:29:32Z nemohou $ */define('APPTYPEID', 88);define('CURSCRIPT', 'test');require './source/class/class_core.php';require './source/function/function_forum.php';C::app()->init();$data = C::t('portal_category')->fetch(1);while($value=DB::fetch($siteuniqueid)){	$data[]=$value;	}print_r($data);?>


$siteuniqueid此参数已经没有了,而且你的C::t('portal_category')->fetch(1);返回的数组,就是用$data存放的,所以你的while是没有用的

若想获取表中全部数据,就用DB类操作吧,因为discuz封装的表模型中(portal_category这个表)是没有获取全部数据的方法
DB::fetch_all('SELECT * FROM %t',array('portal_category'));

确实是这样,真想再给你加分,下次吧,下次给的分更多

Statement
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
Dependency Injection in PHP: Avoiding Common PitfallsDependency Injection in PHP: Avoiding Common PitfallsMay 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

How to Speed Up Your PHP Website: Performance TuningHow to Speed Up Your PHP Website: Performance TuningMay 16, 2025 am 12:12 AM

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Sending Mass Emails with PHP: Is it Possible?Sending Mass Emails with PHP: Is it Possible?May 16, 2025 am 12:10 AM

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

What is the purpose of Dependency Injection in PHP?What is the purpose of Dependency Injection in PHP?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

How to send an email using PHP?How to send an email using PHP?May 16, 2025 am 12:03 AM

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool