如何从MySQL中选择特定信息

时间:2013-12-08 18:52:44

标签: php mysql

我正在尝试使用特定的表格,行和列中的数字,但似乎无法正确使用。

表格为tblproducts。该列为qty。我需要选择id 13的位置。

这是我正在使用的代码,但它没有返回任何内容:

<?php
$con=mysqli_connect("localhost","blank","blank","blank");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT qty FROM tblproducts");

while($row = mysqli_fetch_array($result))
  {
  echo $row['13'];
  }

mysqli_close($con);
?> 

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

在查询中完成所有操作

SELECT qty FROM tblproducts where id =13

不需要循环

$result = mysqli_query($con,"SELECT qty FROM tblproducts where id =13");

$row = mysqli_fetch_assoc($result);
echo $row['qty'];

答案 1 :(得分:0)

你的sql语法有点偏,看看我的更正。你对row['13']的回声看起来也很可疑。您可以使用print_r()打印数组以查找需要使用的密钥。可能是$row['qty']

 <?php
    $con=mysqli_connect("localhost","blank","blank","blank");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con,"SELECT qty FROM tblproducts where id = 13");

    while($row = mysqli_fetch_array($result))
      {
        print_r($row); // You can use print_r to have a look at your full array. The index '13' might not be what you where looking for.
      }

    mysqli_close($con);
    ?>