php stmt相似产品展示

时间:2019-03-06 21:30:41

标签: php mysqli

我正在创建PHP stmt相似产品显示脚本,此脚本正常工作,但未显示相似产品,我想显示相似产品标题

这是我的代码

<?php 
$id=$row['id'];
if($stmt = $con->prepare("SELECT title
FROM products order by rand() limit 3
")){
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_array(MYSQLI_ASSOC)){
    //results
}}
?>  

view.php

    <?php
    if(isset($_GET['id'])) {
    include("config.php");
    $id = $_GET['id'];
    $sql = "select * from products where id = '$id'";
         $result = $con->query($sql); 
           if($result->num_rows > 0){           
                    while($row = $result->fetch_assoc()){
                    //results
              }}}
           ?>

1 个答案:

答案 0 :(得分:0)

确保数据库凭据正确。

尝试下面的代码,让我知道

1)使用rand()方法查询(这就是您想要的)

<?php
         $dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = 'your password goes here';
         $dbname = 'your database goes here';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';
         $sql = 'SELECT title FROM products order by rand() limit 3';
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "Title: " . $row["title"]. "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>

2。)查询使用where子句,在这种情况下,id(1)用于显示ID与数据库中ID匹配的产品标题

<?php


         $dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = 'your password goes here';
         $dbname = 'your database goes here';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';
         $sql = "SELECT id,title FROM products where id='1'";
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "Title: " . $row["title"]. "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>

更新部分

首先在下面创建表格并将其插入

create table products(id int(11) primary key auto_increment,title varchar(30),description varchar(30),image varchar(30),cat_id int(11));

您将插入至少一条记录

insert into products (id,title,description,image,cat_id) values(1,'product title','product details','product.png',100);

要进行测试,请查询产品表以获取 id为1

的详细信息
<?php

         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = '';
         $dbname = 'anglejs';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';
         $sql = "SELECT id,title,description,image,cat_id FROM products where id='1'";
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "Id: " . $row["id"]. "<br>";
               echo "Title: " . $row["title"]. "<br>";
                 echo "description: " . $row["description"]. "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>

现在,因为ID来自 url

出于测试目的:

您可以像以前一样将代码另存为 view.php 打开浏览器并输入类似

http://localhost/-----yourfolder目录进入此处----- / view.php?id = 1 您可以从附加到view.php文件的网址中看到 1的ID 。由于我们插入了ID为1的记录,因此以下代码将显示 仅记录 id = 1 等的行。...

<?php

         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = '';
         $dbname = 'anglejs';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';


$id = $_GET['id'];
//check if id is empty
if($id==''){
echo "Id is empty";
exit;
}

         $sql = "SELECT id,title,description,image,cat_id FROM products where id='$id'";
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "Id: " . $row["id"]. "<br>";
               echo "Title: " . $row["title"]. "<br>";
                 echo "description: " . $row["description"]. "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>