简体   繁体   English

如何使用SQL选择查询获取特定数据?

[英]How can I get a specific data using SQL select query?

I am trying to make a blog site.For this purpose I need to use a specific data from a specific field from my database table.To do that I wrote these code. 我正在尝试创建一个博客站点,为此,我需要使用数据库表中特定字段中的特定数据。为此,我编写了这些代码。

    <?php
    $host = "localhost";
    $user = "root";
    $pass = "12345";
    $db = "bnsb";
    $conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
    mysql_select_db($db, $conn) or die("Database couldn't select!");

    $img = "select image from news where uid=1";
    echo $img;
    ?>

My database connection is OK.It should print like this user_img1.jpg . 我的数据库连接正常,应该像这样user_img1.jpg进行打印。 But it prints the whole sql query like select image from news where uid=1 . 但是它会打印整个sql查询,例如从uid = 1的新闻中选择图像 I run this code on phpmyadmin. 我在phpmyadmin上运行此代码。 It works! 有用! But it does not work in my php script.How can I do now? 但这在我的php脚本中不起作用。我现在该怎么办?

You can not give the query as it is and expect result like in phpadmin. 您不能按原样提供查询,并且无法获得类似phpadmin中的结果。 For this first of all you have to connect to your DB like this 首先,您必须像这样连接到数据库

$con = mysqli_connect("localhost","my_user","my_password","my_db");

execute required query like this 像这样执行所需的查询

$query22 = "select image from news where uid = 1";

$result22 = mysqli_query($con, $query22) or die (mysqli_error());

Get the result and display like this 得到结果并像这样显示

while($rows = mysqli_fetch_array($result22, MYSQLI_BOTH)) 
{
  echo "<br>Values in db: " . $rows['columnname'];
}

Also i advice you to take a look at these tutorials 我也建议您看一下这些教程

http://codular.com/php-mysqli http://codular.com/php-mysqli

http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/ http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/

Please read some PHP 101 kind of tutorials on how to use PHP. 请阅读一些有关如何使用PHP的PHP 101教程。

To get data from DB (in almost any language) 从数据库获取数据(几乎使用任何语言)

  1. You need to connect to a DB. 您需要连接到数据库。 The connection gets you some sort of resource 连接为您提供了某种资源
  2. You formulate your query (which you seem to have done) 您制定查询(您似乎已完成)
  3. You execute the query against the DB that you connected to (step #1) 您对连接到的数据库执行查询(步骤1)
  4. You get a result (set) 您得到一个结果(组)
  5. You iterate over the result set to get the individual result(s); 您遍历结果集以获取单个结果; in your case the result set would be just one result (or row). 在您的情况下,结果集将只是一个结果(或一行)。

The examples to do this in PHP are very basic; 在PHP中执行此操作的示例非常基础。 please do your own lookup on net. 请您自己在网上查找。 This one seems good enough to get you started - http://www.w3schools.com/php/php_mysql_intro.asp 这似乎足以让您入门-http: //www.w3schools.com/php/php_mysql_intro.asp

Try this, 尝试这个,

<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");

$img = "select image from news where uid=1";
$result=mysql_query($img);
while($row=mysql_fetch_array($result)){
   echo '<img src="your_path_to_image/'.$row['image'].'" /> - '.$row['image'];
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM