dqyuipw44576 2014-03-27 01:26
浏览 19
已采纳

选择数据库后没有返回任何内容

I try to return the id_selecao with this query:

SELECT id_selecao FROM selecao WHERE nome = 'Brasil'

but nothing happens, but should return id_selecao = 3. But if I do this query:

SELECT id_selecao FROM selecao WHERE nome = ''

returns the corrects id that is id_selecao = 4.

Here is my table on databe:

id_selecao  nome
1           Portugal
2           Espanha
3           Brasil
4    
5           Portugal
6           Espanha
7           Brasil  

And heres go my php files:

initdb.php

<?php
include 'services.php';

function leFicheiroSelecoes(){
    $file_handle = fopen("selecoes.txt", "r");
    while (!feof($file_handle)) {
        $line = fgets($file_handle);

        mysql_set_charset("utf8");
        insertSelecoes($line);
    }
    fclose($file_handle);
}
leFicheiroSelecoes();
disconnect();
?>

services.php

<?php
    /* INCLUDES */
    require('config.php'); //contains the user, pass, dbname to connect and access to database
    require('database.php');

/* DATA BASE */
$db = new database($dbhost, $dbuser, $dbpass, $dbname);



function insertSelecoes($nome){
    global $db;
    $query =  "INSERT INTO selecao (nome) VALUES ('$nome');";
    $db->setQuery($query);
    $db->query();
}

function disconnect(){
    global $db;
    $db->Disconnect();
}
?>

database.php

<?php
/*
 * Classe que gere a base de dados
 */
class database{

    var $sql = "";
    var $cursor = null;
    var $resource = "";

    function database($host, $user, $password, $db_name) {
        $this->Connect($host, $user, $password, $db_name);
    }

    function Connect($host, $user, $password, $db_name) {
        $this->resource = mysql_connect($host, $user, $password) or die ('Error_connecting_to_my_sql');
        if (!$this->resource) {
            die("Problema na Conex&atilde;o com Base de Dados 1");
        }
        elseif (!mysql_select_db($db_name,$this->resource)) {
            die("Problema na Conex&atilde;o com a Base de Dados 2");
        }
    }

    function Disconnect(){
        return mysql_close($this->resource);
    }

    function query(){
        $this->cursor = mysql_query($this->sql);
        if(!$this->cursor){
            die("Problema em executar a consulta na Base de Dados".mysql_error());
            //return false;
        }
        return $this->cursor;
    }

    function setQuery($sql){
        $this->sql=$sql;
    }

    function loadResult() {
        if (!($result = $this->query())) {
            return null;
        }
        $ret = null;
        if ($row = mysql_fetch_row( $result )) {
            $ret = $row[0];
        }
        mysql_free_result( $result );
        return $ret;
    }

    function loadObjectList( $key='' ) {
        if (!($result = $this->query())) {
            return null;
        }
        $array = array();
        while ($row = mysql_fetch_object( $result )) {
            $array[] = $row;
        }
        mysql_free_result( $result );
        return $array;
    }

}

?>

I can't see where is the error, I have been looking for this hours. Can anyone help me, please.

  • 写回答

3条回答 默认 最新

  • douhuang4166 2014-03-27 01:31
    关注
    $line = fgets($file_handle);
    

    This will return the line, including the newline character at the end. From the documentation:

    Reading ends when length-1 bytes have been read, or a newline (which is included in the return value), ...

    If you want to pass that to a select query, you should cut off the newline character beforehand, such as with:

    $line = trim ($line);
    

    If that doesn't work, execute the following:

    select concat ('[', nome, ']') from selecao where id_selecao = 3
    

    to check if there's any extra stuff in the database table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)