0

My problem is this:

<?php
    // Connect to database server
    mysql_connect("localhost", "root", "abcabc") or die (mysql_error ());

    // Select database
    mysql_select_db('iite') or die(mysql_error());

    // Get data from the database depending on the value of the id in the URL
    $strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    while($row = mysql_fetch_array($rs)) {

        // Write the data of the person
        echo'<h1>'. $row['title'].'</h1>';
        echo'<h1>'. $row['price'].'</h1>';
    }

    // Close the database connection
    mysql_close();
?>

I want to show related posts, for this I need to insert this:

$sql = "SELECT * FROM table1 where title like '%keyword%' limit 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["price"]. " " . $row["title"]. "<br>";
    }
} else {
    echo "0 results";
}

so how can I insert the related post part near

$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];

and how to echo?

Thanks

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dipen Shah Jun 15 '16 at 14:15

3 Answers3

0

You can use OR or AND for combining different where conditions(according to requirement) in query :

Ex;

$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5";
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0
$strSQL = mysql_query(SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5) or die(mysql_error());

$row_strSQL = mysql_fetch_assoc($strSQL );
$totalRows_strSQL = mysql_num_rows($strSQL );


if ($result->totalRows_strSQL > 0) {
    // output data of each row
    while($row_strSQL= $result->fetch_assoc()) {
        echo "id: " . $row_strSQL["id"]. " - Name: " . $row_strSQL["price"]. " " . $row_strSQL["title"]. "<br>";
    }
} else {
    echo "0 results";
}
Rico_93
  • 108
  • 1
  • 6
0

You don't need two seperate queries for that at all. Just use the OR operator in ur where clause like Where id=" . $_GET['id']." or title like '%keyword%'

Ratul Doley
  • 499
  • 1
  • 6
  • 12