用$ _GET选择MYSQL

时间:2013-03-20 23:55:50

标签: php mysqli

我正在尝试根据$ _GET值运行一个选择查询。我有一个错误

以下是代码:

<?
$ini = $_GET["ini_id"];
$con=mysqli_connect("localhost","root","","globalgoals");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else

{
    $country = array();
    //country
    $sql="";
 //!mysqli_query($con,$sql))
    $result = mysqli_query($con,"SELECT * FROM country");/*table name*/
    while($row = mysqli_fetch_array($result))
    {
        array_push($country,$row['country']);/*column name*/
    }
    //

    $goal=array();
    //goal
    $sql="";
 //!mysqli_query($con,$sql))
    $result = mysqli_query($con,"SELECT * FROM metrics where ini_id=$ini");/*table name*/
    while($row = mysqli_fetch_array($result))
    {
        array_push($goal,$row['metrics']);/*column name*/
    }
}


?>



<?  print_r($country);?><br>

<? echo input: $ini ?>

这是错误

警告:mysqli_fetch_array()要求参数1为mysqli_result,布尔值在第28行的C:\ xampp \ htdocs \ xampp \ Testing \ New folder \ table.php中给出

数组([0] =&gt; UK [1] =&gt; DE [2] =&gt; IT [3] =&gt; ROE)

输入:B2C

4 个答案:

答案 0 :(得分:1)

您的错误在于您的查询:

SELECT * FROM metrics where ini_id=$ini 

你应该在''这里包含$ in:

SELECT * FROM metrics where ini_id='$ini'

你也应该考虑准备好的陈述......

答案 1 :(得分:0)

mysqli_query($con,"SELECT * FROM country")mysqli_query($con,"SELECT * FROM metrics where ini_id=$ini")返回false(不知道哪个,看不到行号)。您可能有SQL错误。

另外,这个:

mysqli_query($con,"SELECT * FROM metrics where ini_id=$ini")

不安全。 $ini来自用户输入,这意味着他们可以填充他们想要的东西(“SQL注入”)。请改用prepare

答案 2 :(得分:0)

首先检查你的$ _GET [“ini_id”]是否已设定。

再次检查您的$ country数组是否包含项目(至少为1)。

第三

    <?
        $ini = $_GET["ini_id"];
    $con=mysqli_connect("localhost","root","","globalgoals");
    // Check connection
    if (mysqli_connect_errno() && isset($_GET["ini_id"]))// checking...
      {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      else
      {
        $country = array();

        $result = mysqli_query($con,"SELECT * FROM country");/*table name*/
        while($row = mysqli_fetch_assoc($result)) //assoc instead of array!!!!
        {
            array_push($country,$row['country']);/*column name*/
        }

        $goal=array();

        $result = mysqli_query($con,"SELECT * FROM metrics where ini_id=$ini");/*table name*/
        while($row = mysqli_fetch_assoc($result)) //assoc instead of array!!!!
        {
            array_push($goal,$row['metrics']);/*column name*/
        }
    }

    ?>

    <?  print_r($country);?>
    <br>
    <? echo input: $ini ?>

答案 3 :(得分:-1)

mysqli_fetch_array 数组时,不确定使用数组的重点是什么。您收到该错误,因为查询无效。

$result = mysqli_query($con,"SELECT * FROM metrics where ini_id = '$ini'");

您还需要清理$_GET[ini_id];使用绑定参数。