变量在PHP中传递[重复]

js5cn81o  于 2023-10-15  发布在  PHP
关注(0)|答案(4)|浏览(92)

此问题已在此处有答案

How to include a PHP variable inside a MySQL statement(5个答案)
2天前关闭。
这里我想把$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';
}
plupiseo

plupiseo1#

正如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";
cwxwcias

cwxwcias2#

联系PDO

try
     {
      $dbh = new PDO("pgsql:host=$host;port=5432;dbname=$db;user=$user;password=$pass");
      echo "Connected

";
    }
    catch (Exception $e)
    {
      echo "Unable to connect: " . $e->getMessage() ."

";
    }

准备你的陈述:

$sth = $dbh->prepare("SELECT DomainCategory.Name "
        . "FROM DomainName_Client, DomainNameType, DomainCategory,                OrderDomain_Client "
        . "WHERE DomainName_Client.Name='?' "
        . "AND 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();
guicsvcw

guicsvcw3#

使用{$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";
lyfkaqu1

lyfkaqu14#

使用连接查询,而不是简单地选择带有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."'";`

相关问题