Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从MySQL接收各种结果';像';?_Php_Mysql - Fatal编程技术网

Php 如何从MySQL接收各种结果';像';?

Php 如何从MySQL接收各种结果';像';?,php,mysql,Php,Mysql,所以,我写了这段代码,它是有效的。但是,它只返回一个结果。所以我的数据库中有两个‘Eric Clapton’的名字,每个名字都有不同的id。但是查询只返回这两个选项中的一个。有什么想法吗 <?php $now = htmlentities(rawurldecode($_GET['word'])); // in this case i passed in 'Eric Clapton' $cat = htmlentities($_GET['cat']); // category, in th

所以,我写了这段代码,它是有效的。但是,它只返回一个结果。所以我的数据库中有两个‘Eric Clapton’的名字,每个名字都有不同的id。但是查询只返回这两个选项中的一个。有什么想法吗

<?php

$now = htmlentities(rawurldecode($_GET['word'])); // in this case i passed in 'Eric Clapton'
$cat = htmlentities($_GET['cat']); // category, in this case lets say i passed in 'music'

$con = mysql_connect("localhost","USERNAME","PASS");
if (!$con)
  {
  echo "<pre>An error occured, please try again later. Sorry...</pre>"; 
  }
else{ mysql_select_db("DATABASE", $con);

    $result = mysql_query("SELECT id, name FROM $cat WHERE name LIKE '%$now%'");
    $row = mysql_fetch_array($result);

    echo "<pre>";
    print_r($row);
    echo "</pre>";

mysql_close($con);  
}
?>

您缺少循环,您在其中循环查看结果:

while($row=mysql\u fetch\u array($result))
{
回声“;
打印(行);
回声“;
}

您当前所做的是从结果中获取第一行

你真的应该清理你的输入和/或移动PDO和准备好的语句。你觉得“mysql\u real\u escape\u string”怎么样?在执行查询之前,在$cat和$word上都执行。很好,但是如果您转到PDO,我真的更喜欢(您也应该如此!)。好多了,你简直无法想象
mysql.*
是一组旧的、几乎不推荐使用的函数。它可以正常工作。但是当我输入整个名字,比如Led齐柏林飞艇,它什么也不返回。
<?php

$now = '%'.htmlentities(rawurldecode($_GET['word'])).'%';
$cat = htmlentities($_GET['cat']); 

$dsn = 'mysql:dbname=DATABASE;host=localhost';
$user = "USER";
$password = "PASS";

# connect to the database
try {
    $DBH = new PDO($dsn, $user, $password);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    # the data we want to insert
    $data = array($now);

    $STH = $DBH->prepare("SELECT id, name FROM $cat WHERE name LIKE ?");
    $STH->execute($data);
    $result = $STH->fetchAll();

}
catch(PDOException $e) {
    echo "Uh-Oh, something wen't wrong. Please try again later.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

echo "<pre>";
print_r($result);
echo "</pre>";

$DBH = null;

?>
while($row = mysql_fetch_array($result))
{
    echo "<pre>";
    print_r($row);
    echo "</pre>";
}