2017-11-04 29 views
0

因此,我正在股票报价页面上,并且在“查找符号页面”中,用户输入公司的符号,并且将输出存储在数据库中的任何以与用户输入完全相同的字母开头的符号放在桌子上。如何提取数据库中与用户输入相同的字母开头的所有值?

SOLVED-回答以下

+0

您正在分配第一行,紧接着'} else {',那么当您使用'while'循环迭代时覆盖它。你从不使用第一行,所以当你只有一行时,你没有。 (while循环不会将结果集重置为0)您还有其他一些小问题。我在回答中概述了他们。 – ArtisticPhoenix

回答

0

你在小写输入并存储在大写

因此改变你的查询字符串转换成上,然后搜索 最好是在两侧上的where子句。

$query = "SELECT symSymbol, symName FROM symbols " . 
"WHERE upper(symSymbol) LIKE '%".strtoupper($strSymbol)."%'"; 
0

这是怎么一回事?请在你的代码中看到我的评论。

if(!$result) 
{ 
    print "Invalid Query Result<br />\n"; 
} 
else 
{ 
    //============ This line is your main issue ============ 
    $row = @$result->fetch_assoc(); //<-- pulling a row here advances the 
    //data set to row #2 (you never use the data from this row) 
    //====================================================== 
    print "<table border='3' >\n"; 
    print "<tr><th>Symbol</th>"    
    . "<th>Company Name</th>"    
    . "<th>Quotes Page</th" //<<--- unclosed </th> 
    . "<tr><th>History Page</th></tr>\n"; //<<--- misplaced <tr> 
}//<<--- end if($result) block, 
/* 
    anything below this is outside of the scope of the check for the result, 
    this will give you partial HTML and errors in the event of $result = false 
*/ 

/* 
    because, you already advanced the dataset by one row, the loop starts on row 2 
    If you only had 1 row in your result set, then nothing is output from this block 
    While loops do not rewind, like a foreach (besides I don't think you can rewind the DB result cursor)   
*/ 
while($row = @$result->fetch_assoc()) 
{ 
extract($row); //<<--- avoid using this as it pollutes the scope, also where exactly are you using these variables it creates 

print "<tr>"; 
print "<td>{$row["symSymbol"]}</td>"; 
print "<td align='right'>{$symName}</td>"; 
print "<td><a href=\"quotes.php?Name=" . $row["symSymbol"]. "\"> Quotes Page</a></td>"; 
print "<td><a href=\"history.php?Name=" . $row["symSymbol"]. "\"> History Page</a></td>"; 



print "</tr>\n"; 
} 
print "</table>\n"; 

让我们通过清理,截至

if(!$result){ 
    print "Invalid Query Result<br />\n"; 
}else{ 
    print "<table border='3' >\n"; 
     /* 
     don't be afraid to indent or format things in a way that helps 
     keep track of the tags. Not like the "tab" police are going to 
     bust you for using all the tabs up. 
     */ 
    print "<tr>"; 
      print "<th>Symbol</th>"    
      . "<th>Company Name</th>"    
      . "<th>Quotes Page</th>" 
      . "<th>History Page</th>\n"; 
    print "</tr>\n"; 

    while($row = $result->fetch_assoc()){ 
     /* 
      Try to use a consistent style for outputting, if possible. 
     */ 
     print "<tr>"; 
      print "<td>{$row["symSymbol"]}</td>" 
       . "<td align='right'>{$symName}</td>" 
       . "<td><a href=\"quotes.php?Name=" . $row["symSymbol"]. "\"> Quotes Page</a></td>" 
       . "<td><a href=\"history.php?Name=" . $row["symSymbol"]. "\"> History Page</a></td>\n"; 
     print "</tr>\n"; 
    } 

    print "</table>\n"; 
} 

你基本上扔掉你的结果集的第一行开始。当你调用这个..}else{ $row = @$result->fetch_assoc(); ..时,你拉出第一行并将内部指针移到第2行。您不仅不会使用此数据,而且会使用while循环中分配的值覆盖$row。现在while循环不会倒回到第一个结果,因此您拉第2行并覆盖$row或覆盖它与false。这就是为什么当你有一场比赛时,你没有得分。

其他一些事情提:

  • 了解如何更清晰,简洁我的代码,良好的组织易于阅读。编码(IMO)时,可读性是最重要的。

  • 范围,保留$result东西在if块。如果你不这样做,那么你会有一堆错误和无效的HTML。

  • 如果你不打算使用它,请不要指定任何东西。虽然我明白,我只是“测试”或“工作”出来。这很好,但是当你遇到问题时你应该做的第一件事就是去你的代码,组织它并清理它。从重构中移除任何“碎片”,并且添加注释给任何不清楚的东西,只是看它自己的代码。

  • 避免使用extract它会污染您的变量范围。这意味着你可以在没有意识到的情况下覆盖变量。

例如,假设你有这样的代码:

$name = 'John'; 
    ... bunch of other code .. 
    extract($row); 
    ... more code ... 
    echo $name; //outputs Tom 

现在不能够直接看到的$row的价值,你没有办法知道发生了什么事只要看一眼的代码。使用它只是一个不好的做法(IMO),因为您不想使用$row['name']

希望有所帮助。

+0

谢谢,这真的很有帮助!是的,我的代码是一团糟,这帮助我更好地理解它。 – John

+0

当然,这是没有问题的,我一直在编码像6年,我可以做,我睡觉。我可能每天写几千行代码。很容易发现问题。 – ArtisticPhoenix

相关问题