这里我想将
$part1
和 $part2
值分配给 Mysqli 查询。但问题是 $part2
值没有进入查询。我无法找到我的代码有什么问题。
$string = 'Enter a domain name' . "\r\n";
socket_write($client, $string, strlen($string)) or die("Could not write output\n");
$str = '';
while ($input = socket_read($client, 1024)) {
$str .= $input;
if (strpos($str, "\n") !== false) {
break 1;
}}
$part = explode(".", $str);
$part1 = $part[0];
$part2= $part[1];
$sql = "SELECT DomainCategory.Name "
. "FROM DomainName_Client, DomainNameType, DomainCategory, OrderDomain_Client "
. "WHERE DomainName_Client.Name='$part1' "
. "AND DomainNameType.Name='$part2' "
. "AND DomainName_Client.TypeID=DomainNameType.ID "
. "AND DomainCategory.ID=DomainName_Client.DomainCategoryID "
. "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID";
$result = $mysqli->query($sql);
$row1 = $result->num_rows;
if ($row1 > 0) {
$row = $result->fetch_array(MYSQLI_NUM);
printf("%s\n", $row[0]);
} else {
echo 'This is not a Registered Domain';
}
正如 Combinu 所建议的,PDO 是更好的选择。但是,为了符合您的想法,我将连接变量的内容。
$sql = "SELECT DomainCategory.Name "
. "FROM DomainName_Client, DomainNameType, DomainCategory, OrderDomain_Client "
. "WHERE DomainName_Client.Name= ". $part1
. "AND DomainNameType.Name= " . $part2
. "AND DomainName_Client.TypeID=DomainNameType.ID "
. "AND DomainCategory.ID=DomainName_Client.DomainCategoryID "
. "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID";
与PDO连接
尝试一下 { $dbh = new PDO("pgsql:主机=$host;端口=5432;dbname=$db;用户=$user;密码=$pass"); 回显“已连接”; } 捕获(异常 $e) { 回显“无法连接:”。 $e->getMessage() ."
"; }
准备你的陈述:
$sth = $dbh->prepare("选择 DomainCategory.Name " 。 “来自 DomainName_Client、DomainNameType、DomainCategory、OrderDomain_Client” 。 “哪里 DomainName_Client.Name='?' ” 。 “并且 DomainNameType.Name='?' ” 。 “AND DomainName_Client.TypeID=DomainNameType.ID” 。 “AND DomainCategory.ID=DomainName_Client.DomainCategoryID” 。 "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID");
并执行:
$sth->execute(array($part1, $part2)); $red = $sth->fetchAll();
使用 {$part2} 并检查 $part2 在赋值后是否有价值
$sql = "SELECT DomainCategory.Name "
. "FROM DomainName_Client, DomainNameType, DomainCategory, OrderDomain_Client "
. "WHERE DomainName_Client.Name='{$part1}' "
. "AND DomainNameType.Name='{$part2}' "
. "AND DomainName_Client.TypeID=DomainNameType.ID "
. "AND DomainCategory.ID=DomainName_Client.DomainCategoryID "
. "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID";
使用连接查询而不是简单的带有where条件的选择查询
`"SELECT DomainCategory.Name FROM DomainName_Client join DomainNameType on DomainNameType.ID = DomainName_Client.TypeID join DomainCategory on DomainCategory.ID=DomainName_Client.DomainCategoryID join OrderDomain_Client on OrderDomain_Client.DomainNameID=DomainName_Client.ID WHERE DomainName_Client.Name = '".$part1."' AND DomainNameType.Name = '".$part2."'";`