简体   繁体   English

这个mysqli查询怎么了?

[英]What's wrong with this mysqli query?

I'm trying to figure out what's wrong with my mysqli code below. 我正在尝试找出下面的mysqli代码出了什么问题。 The data is definitely in the table. 该数据肯定在表中。 I have 2 questions: 我有两个问题:

  1. Why isn't this giving me any results? 为什么这没有给我任何结果?
  2. Could I have the echo <table></table> section in a separate php file, if yes, how would I call $name,$partner,$cell etc.? 我可以在单独的php文件中包含echo <table></table>部分,如果是的话,我将如何调用$name,$partner,$cell等?

      <?php $mysqli = new mysqli($host, $uname, $pword, $db); if ($mysqli->connect_errno) { echo "Connection failed: %s\\n", $mysqli->connect_error; exit(); } $query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC"); $result = $mysqli->query($query); while ($row = $result->fetch_array()) { $space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : ''; $name = $row['firstname'].$space.$row['lastname']; $partner = $row['spousefirst']; $cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : ''; $email = (!empty($row['email'])) ? " {$row['email']} (email)" : ''; $ID = $row['ID']; echo'<table> <tbody> <tr> <td><input type="checkbox" name="" id="" value="<? echo $ID; ?>"></td> <td><a href="/backend/leads/view/?ID=<? echo $ID; ?> "><strong><? echo $name; ?></strong></a></td> <td><a href="/backend/leads/view/?ID=<? echo $ID; ?> "><? echo $partner; ?></a></td> <td><? echo $phonecell; ?></td> <td><a href="mailto:<? echo $email; ?>"><? echo $email; ?></a></td> <td><? echo date("M jS, g:i A", strtotime($date)); ?></td> <td><? echo $contacttype; ?></td> <td><? echo $agentassigned; ?></td> <td><? echo $leadstatus; ?></td> <td><a href="/backend/contacts/notes.php?ID=<? echo $ID; ?>">View </a>+ </td> <td><a href="/backend/contacts/todo.php?ID=<? echo $ID; ?>">View </a>+ </td> <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $ID; ?>">D</a></td> </tr> </tbody> </table>'; } ?> 

EDIT: 编辑:

It's showing the <table> but just not giving me values for $name, $email...ect. 它显示的是<table>但没有给我$ name,$ email ... ect的值。

The query looks solid as does the rest of your code. 该查询看起来很稳定,其余代码也一样。 Here are a few troubleshooting steps to try out: 以下是一些故障排除步骤,可以尝试:

  1. echo out $query and run it directly against your database. echo $query并直接对您的数据库运行它。 Do you get results? 你有结果吗? If you don't get any results from a direct query then that means the query is returning 0 rows and your tables will end up empty as well. 如果您没有从直接查询中得到任何结果,则意味着查询返回0行,并且您的表也将最终为空。
  2. Do a var_dump($result) to verify there is information being stored in there. 执行var_dump($result)以验证其中是否存储了信息。 You could also add change $result = $mysqli->query($query); 您还可以添加更改$result = $mysqli->query($query); to $result = $mysqli->query($query) or die $mysqli->error; $result = $mysqli->query($query) or die $mysqli->error;

The results from these 2 steps should give you a good idea on where things are going wrong. 这两个步骤的结果应该使您对哪里出错了有个好主意。 In general you want to stop through each part of the query process and look at every variable involved to ensure that it has the data you expect stored inside of it. 通常,您希望停止查询过程的每个部分,并查看涉及的每个变量,以确保它在其中存储了您期望的数据。

To answer your 2nd question: yes you can. 要回答您的第二个问题:是的,您可以。 If you put the code for the table in a separate file and then include() it, the variables will still be available to the included code. 如果将表的代码放在单独的文件中,然后对其进行include() ,则变量仍可用于所包含的代码。

See the PHP manual on include() 请参阅include()上的PHP手册

The final echo does not look well constructed. 最终的回声看起来构造不好。 I would prefer to use a concatenated string to output the variables, just like this( I put only a few lines as example): 我宁愿使用级联字符串来输出变量,就像这样(我仅以几行为例):

echo'<table>
<tbody>
   <tr>
     <td><input type="checkbox" name="" id="" value="'. $ID .'"></td>
     <td><a href="/backend/leads/view/?ID='. $ID .'"><strong>'. $name.' </strong></a></td> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM