我尝试使用此查询返回id_selecao
:
SELECT id_selecao FROM selecao WHERE nome = 'Brasil'
但没有任何反应,但应该返回id_selecao = 3
。但是如果我这样做的话:
SELECT id_selecao FROM selecao WHERE nome = ''
返回id_selecao = 4
的更正ID。
这是关于数据库的表格:
id_selecao nome
1 Portugal
2 Espanha
3 Brasil
4
5 Portugal
6 Espanha
7 Brasil
继续我的php文件:
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ão com Base de Dados 1");
}
elseif (!mysql_select_db($db_name,$this->resource)) {
die("Problema na Conexã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;
}
}
?>
我无法看到错误在哪里,我一直在寻找这个时间。请有人帮帮我。
答案 0 :(得分:1)
你可能在该领域有任何前导或尾随空格?试试这个:
SELECT id_selecao FROM selecao WHERE nome like '%Brasil%'
答案 1 :(得分:1)
很可能你在文字'Brasil'周围有一些空白。你可以尝试的是在文本周围添加一些通配符:
SELECT id_selecao FROM selecao WHERE nome like '%Brasil%'
可能会解决这个问题。
答案 2 :(得分:1)
$line = fgets($file_handle);
这将返回行,包括最后一行的换行符。来自documentation:
当读取
length-1
个字节或换行符(包含在返回值中)时,读取结束,...
如果要将其传递给select
查询,则应事先切断换行符,例如:
$line = trim ($line);
如果这不起作用,请执行以下操作:
select concat ('[', nome, ']') from selecao where id_selecao = 3
检查数据库表中是否有任何额外的东西。