我想用MySQL输出MySQL数据库中的一些分数。在我的select查询之后,我尝试回显该值,但我得到的只是RESOURCE ID 10#。这是我的代码:
$rank = mysql_query("SELECT 1+COUNT(*) FROM `class_ranking` WHERE overall_scores > (SELECT overall_scores FROM `class_ranking` WHERE ref_id='$id')");
While ($rk = mysql_fetch_array($rank)){
echo $rk['overall_scores'];
}
我需要你的帮助。感谢。
答案 0 :(得分:3)
以这种方式写1+COUNT(*) as overall_scores
$rank = mysql_query("SELECT 1+COUNT(*) as overall_scores FROM `class_ranking` WHERE overall_scores > (SELECT overall_scores FROM `class_ranking` WHERE ref_id='$id')");
While ($rk = mysql_fetch_array($rank)){
echo $rk['overall_scores'];
}
答案 1 :(得分:2)
设置计数的别名:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "tomestore.wixsite.com.tome"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-storage:11.0.4'
implementation 'com.google.firebase:firebase-auth:11.0.4'
implementation 'com.google.firebase:firebase-database:11.0.4'
implementation 'com.google.firebase:firebase-messaging:11.0.4'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
// RecyclerView
compile 'com.android.support:recyclerview-v7:26.1.0'
// CardView
compile 'com.android.support:cardview-v7:26.1.0'
}
apply plugin: 'com.google.gms.google-services'
答案 2 :(得分:1)
首先,SELECT 1+COUNT(*)
将返回类似
+------------+
| 1+count(*) |
+------------+
| 4 |
+------------+
因此,这不起作用
While ($rk = mysql_fetch_array($rank)){
echo $rk['overall_scores'];
}
因为没有overall_scores
。您可能必须将其别名为SELECT 1+COUNT(*) AS overall_scores FROM...
并执行
$result=mysql_query("SELECT 1+COUNT(*) AS overall_scores FROM ...");
$data=mysql_fetch_assoc($result);
echo $data['overall_scores']; // Or $data['1+COUNT(*)'] if you did not alias it
答案 3 :(得分:0)
你需要改变mysql_fetch_array()而不是mysql_fetch_assoc()看起来像这样
$rank = mysql_query("SELECT 1+COUNT(*) AS overall_scores FROM `class_ranking` WHERE overall_scores > (SELECT overall_scores FROM `class_ranking` WHERE ref_id='$id')");
$finalArr = [];
While ($rk = mysql_fetch_assoc($rank)){
$finalArr[] = $rk['overall_scores'];
}
echo '<pre>';
print_r($finalArr);