PHP MySQL的覆瓦状排列的SELECT语句返回null


胡阿里·Zegai:

我试图执行查询,但结果却是零,我执行在phpMyAdmin相同的查询并返回结果。

问题是,当查询包含SELECT内的另一个SELECT语句(覆瓦状),所以当查询只包含一个SELECT语句,它工作正常,并返回结果。下面是代码:

db_config.php

<?php    
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "university_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

get_planning.php

<?php
require 'db_config.php';

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT cours.jour, 
TIME_FORMAT(cours.heure_debut, '%H:%i') AS debut, 
TIME_FORMAT(cours.heure_fin, '%H:%i') AS fin, 
enseignant.nom_ens as prof, modules.nom_mod as module, salles.nom_salle as salle
FROM cours, promotion, enseignant, modules, salles 
WHERE cours.id_promo = (SELECT id_promo FROM promotion WHERE promotion.niveau = '2' 
AND promotion.id_speci = (SELECT id_speci FROM spécialité WHERE nom_speci = 'MGL')) 
AND cours.id_promo = promotion.id_promo AND cours.id_ens = enseignant.id_ens AND cours.id_salle = salles.id_salle AND cours.id_mod = modules.id_mod 
ORDER BY cours.id_cours;";

$result = $conn->query($sql);

if($result == null)
    echo "Result is empty!";

输出:

Result is empty!

信息:

  • PHP:7.3.5版本
  • 数据库:MySQL的
O.琼斯:

为解决此问题的三点建议:

  1. 更改您cours.id_promo = (SELECT ...cours.id_promo IN (SELECT...和之后做同样的线路。为什么?如果您使用=与内SELECT语句返回多个结果,热潮。查询失败。SQL是,在它的心脏,一组处理语言,并this IN that检查这this是一组中的一员that

  2. 您呼应$sql值,以确保本声明它是正确的。尝试通过phpMyAdmin的运行确切的语句,以确保它给你你所期望的。

  3. 你有这样的

  if($result == null) 
      echo "Result is empty!";

将其更改为这

  if(!$result) 
      echo 'sql failure:', $conn->error, ': ', $sql;  

query()方法只返回一个falsey如果查询失败的结果。如果它成功,但发现没有匹配行,query()返回一个空的结果集。所以,任何时候你的$结果是falsey,您在查询中所做的一个编程错误。echo我提到的诊断会为你。

临提示请务必检查SQL操作错误。

临提示2带上你的SQL技能带入21世纪。使用明确的联接操作,而不是带点儿怀旧逗号连接操作。更改...

SELECT cours.jour, whatever, whatever 
  FROM cours 
  JOIN promotion   ON cours.id_promo = promotion.id_promo
  JOIN enseignant  ON cours.id_ens = enseignant.id_en
  JOIN modules     ON cours.id_mod = modules.id_mod
  JOIN salles      ON cours.id_salle = salles.id_salle
 WHERE cours.id_promo IN (SELECT id_promo FROM promotion WHERE promotion.niveau = '2')
   AND promotion.id_speci IN (SELECT id_speci FROM spécialité WHERE nom_speci = 'MGL') 
 ORDER BY cours.id_cours

你的表之间的关系更容易阅读和理解这种方式。而且,您可以更改JOINLEFT JOIN,如果你的应用需要。

Related


PHP MYSQL SELECT returns NULL

Jiatai Technology I execute the following code to create company_id as UUID_SHORT in temp table. This company_id will then be used to insert records into multiple tables with the UUID as the primary key. My problem is that when I try to retrieve company_id as

PHP MYSQL SELECT returns NULL

Jiatai Technology I execute the following code to create company_id as UUID_SHORT in temp table. This company_id will then be used to insert records into multiple tables with the UUID as the primary key. My problem is that when I try to retrieve company_id as

PHP MySQL SELECT statement returns null

Amir Memari I want to select data from MySQLaa where condition . I'm sure the condition is false, that's why it shouldn't return any data or any row, but it returns null. I checked it and $stmt2->num_rows > 0this condition is true. How can I prevent this from

PHP MySQL tickle SELECT statement returns null

Houari Zegai: I tried to execute the query but the result is nil, I execute the same query in phpMyAdmin and it returns the result. The problem is that when the query contains another SELECT statement inside the SELECT (ragged), so when the query contains only

PHP MySQL tickle SELECT statement returns null

Houari Zegai: I tried to execute the query but the result is nil, I execute the same query in phpMyAdmin and it returns the result. The problem is that when the query contains another SELECT statement inside the SELECT (ragged), so when the query contains only

PHP MySQL tickle SELECT statement returns null

Houari Zegai: I tried to execute the query but the result is nil, I execute the same query in phpMyAdmin and it returns the result. The problem is that when the query contains another SELECT statement inside the SELECT (ragged), so when the query contains only

PHP MySQL tickle SELECT statement returns null

Houari Zegai: I tried to execute the query but the result is nil, I execute the same query in phpMyAdmin and it returns the result. The problem is that when the query contains another SELECT statement inside the SELECT (ragged), so when the query contains only

PHP MySQL SELECT WHERE NOT IN

elder Having trouble implementing some code that works in phpMyAdmin but fails in PHP. (This is my first PHP project and can't find an answer that seems to work) Basically I have 3 tables: memberdetails = mem_id, lastname, firstname ..... classdetails = class_

PHP mysql select MAX()

PB Music Tried to select MAX()in PHP MySQL, but no results and no error (error display is on) error_reporting(E_ALL); ini_set('display_errors', 1); )。 I tried: $user_home = new USER(); $select_max = $user_home->runQuery("SELECT MAX(Gems) AS highest FROM user

PHP and MySQL select a value

User 3055501 I would like to know how to select a value from a MySQL table. The table includes columns usernameand idother columns ( idwhich are auto-incrementing and usernameunique). Given a username, I want to set a session variable $_SESSION['myid']equal to

MYSQL PHP select order

bone I have sample table date_table as base for data filter 2 columns - day and day_of_week I do query in SQL select distinct day_of_week,day from date_table order by day_of_week no problem day_of_week day 1 Monday 2

MYSQL PHP select order

bone I have sample table date_table as base for data filter 2 columns - day and day_of_week I do query in SQL select distinct day_of_week,day from date_table order by day_of_week no problem day_of_week day 1 Monday 2

MySQL SELECT or SUBSELECT with PHP

CapitanFindus I have a small problem with a query that I don't remember how to manage. This is my database structure on phpmyadmin: table users hid(PK) Form requirements id (PK), hid, word, time table click id, rid (FK) The problem is, I need to get the num

PHP MySQl chain select

Lloyd Owen I have two dropdown lists populated from MySQL tables. In MySQL tables, they have foreign key constraints, so table "assets" has column "department" linked to department table. Therefore, each asset has an associated department. My first dropdown is

PHP MySQL select by HTML

blues I am trying to select from a table based on the posted value of an HTML select box. I get nothing and I have no problem giving back the value of the post. The statement itself works, but when I populate it with a select form it doesn't work. This is just

MYSQL select query in PHP

username I am new to Laravel and I am trying to receive clasa_id from clasa table in elevi table. $clasa = new Clasa(); $clasa->cifra = \Auth::user()->clasa_cifra; $clasa->litera = \Auth::user()->clasa_litera; $clasa->save(); $elev = new

PHP MySQL query select

Sumit kumar I have 3 tables like this news_table: newsID int auto_increment, title varchar(256), Records are like: (1, 'some_title'), (2, 'some_title' ), (3, 'some_title' ), (4, 'some_title' ), (5, 'some_title' ), (6, 'some_title' ), (7, 'some_title' ), and s

MYSQL PHP select order

bone I have sample table date_table as base for data filter 2 columns - day and day_of_week I do query in SQL select distinct day_of_week,day from date_table order by day_of_week no problem day_of_week day 1 Monday 2

Select, Insert MySQL and PHP

yun I have created the search function and I am able to display the rows. But I don't know what to do: For example, I searched for Jnames starting with J and it listed all names starting with J and when I was logged in, but not logged out. This means, timeOut

PHP MySQL select by HTML

blues I am trying to select from a table based on the posted value of an HTML select box. I get nothing and I have no problem giving back the value of the post. The statement itself works, but when I populate it with a select form it doesn't work. This is just

php/mysql ignore input if null

no Is there anyway to ignore the input if it is null and still successfully update the value of the input? For example, if I only give itemID1 a value, and itemID2 is null. $upd = "UPDATE booking SET status='$status', CalDate='$CalDate', DueDate='$DueDate' WHE

php/mysql ignore input if null

no Is there anyway to ignore the input if it is null and still successfully update the value of the input? For example, if I only give itemID1 a value, and itemID2 is null. $upd = "UPDATE booking SET status='$status', CalDate='$CalDate', DueDate='$DueDate' WHE

Chain select with php mysql jQuery

IBMdig I have a problem with a chained select script in my mvc structure, I have a form view with the following: <div class="col-md-6 col-md-offset-3"> <form action="" method="post" role="form"> <div class="form-group"> <label>Nom de l'annonce:</la

MYSQL SELECT using PHP loop

Orda style I need to select data from MYSQL table. The data I need to select is defined by a record in another column of the table. In the code, these records are defined by $Array. I am trying to use this code: $Array=Array("red","green","blue","white"); $st

select query in mysql/php not working

Pedram I wrote a simple code for login page using PHP and Mysql, but it always returns false value in condition (if). What's wrong? Here is my code: ..... (successfully connected to database) $User = $_POST['username']; $Pass = md5($_POST['password']);

PHP MySQL select length wrong

Crewe The MySQL database contains over 11,000 rows and 3 assigned columns: price, UPC code and description. Running LAMP on the same machine (Ubuntu 18.04), I'm using PHP to capture every column of the database on page load and echo it as a javascript array wh

PHP Mysql Select query with BETWEEN

George Young I'm trying to use between with two queries but when I add the between code it doesn't work. Here is the code that worked for me before using BETWEEN. $result = mysql_query("SELECT name , liking , id , address , description FROM locations WHERE lik