我对PHP和MySQL感到头疼

时间:2016-10-17 10:13:28

标签: php mysql

我有这个问题:

function search_by_name($mysql, $name, $lastname)
{
    $query = 'SELECT idKlienci FROM Klienci WHERE Imie = "' . $name . '" 
              AND Nazwisko = "' . $lastnem . '"';
    $result = $mysql->query($query);
    $row = mysqli_fetch_array($result); // I want to get the ID' of table `Klienci1`
    // and here i don't know how many dimentions have this array
    echo $row[0][0]; // prints nothing
}

4 个答案:

答案 0 :(得分:1)

$lastnem!= $lastname

因此,请更改查询以使用正确的变量名称

$query = 'SELECT idKlienci 
            FROM Klienci 
            WHERE Imie = "' . $name . '" 
              AND Nazwisko = "' . $lastname . '"';

为了使这种代码更易于阅读,您还可以利用双引号字符串中的变量自动扩展的事实。这使得这更容易阅读,因此调试。

$query = "SELECT idKlienci 
            FROM Klienci 
            WHERE Imie = '$name' 
              AND Nazwisko = '$lastname'";

$result = $mysql->query($query);

// use mysqli_fetch_assoc() then you get only one assoc array
// so you can use named parameters to the array.
// the names will match the column names in the table

//$row = mysqli_fetch_array($result);
// also mysqli_fetch_assoc() only returns one row at a time
$row = mysqli_fetch_assoc($result);

// a row is always one dimensional so do 
echo $row['id']; 

因此,如果查询的结果集中有多行,则必须在循环中获得结果

$result = $mysql->query($query);

while ( $row = mysqli_fetch_assoc($result)) { 
    echo $row['id'] . '<br>'; 
}

现在你应该看到两行

答案 1 :(得分:0)

MYSQL不对字符串使用双引号,它使用简单的引号:

$query = "SELECT idKlienci FROM Klienci WHERE Imie = '$name' AND Nazwisko = '$lastname'";

当您使用双引号时,您也可以将变量直接添加到PHP字符串中,而无需将它们连接起来。

答案 2 :(得分:0)

您的参数名称$lastname&amp;使用的变量$lastnem名称不相同。

答案 3 :(得分:0)

试试这个:

 $row = mysqli_fetch_array($result); 
    echo $row[0];