1

Hello please someone can help me with this code? I think there i can remove * and GET id.

<div class="col-md-4"> <h1>Sidebar</h1> <?php require_once 'connessione.php'; $id=$_GET['id']; $query = "SELECT * FROM campi_name where id='$id'"; $stmt = $DBcon->prepare( $query ); $stmt->execute(); while($row=$stmt->fetch(PDO::FETCH_ASSOC)){ ?> <div class="media"> <div class="media-left"> <a href="#"> <img class="media-object" src="..." alt="..."> </a> </div> </div> <?php } ?> </div> 

How can i show only item (from db) related to the Brand row from table campi_name?

and with code like that how i can show 5 random item from campi_name?

Thank you!

8
  • 1
    5 random items from campi_name table SELECT * FROM campi_name ORDER BY RAND() LIMIT 5 Commented Mar 7, 2017 at 15:32
  • fetch(); is used to fetch a single result, use fetchall() instead : stackoverflow.com/questions/14075954/pdo-fetch-fetchall Commented Mar 7, 2017 at 15:36
  • for the sql logic we would need to know more column names to know how to JOIN your data Commented Mar 7, 2017 at 15:37
  • And u are using PDO prepared wrong way Commented Mar 7, 2017 at 15:37
  • @MasivuyeCokile have you a sample of a best way to do? Commented Mar 7, 2017 at 15:56

1 Answer 1

0

For the first question you can use

$query = "SELECT Brand FROM campi_name WHERE id='" . $id . "'"; 

I'm not sure because I haven't understand what you are asking. Do you want to select only Brand column or what? Do you want to select just the row that content some letters?

For the second question use

$query = "SELECT * FROM campi_name ORDER BY rand() LIMIT 5" 
Sign up to request clarification or add additional context in comments.

5 Comments

Hello thanks for your reply, i want show item with the same brand, in db i have a row with products brand ID,TITLE,BRAND,PRICE,etc,ectc
So $query = "SELECT * FROM campi_name WHERE Brand='" . your brand variable . "'";
Example: If you want to select all rows where Brand="Coca cola" then $brand = "Coca cola"; $query = "SELECT * FROM campi_name WHERE Brand='" . $brand . "'"; It will select all rows of the product of the brand "Coca cola" With this code you need to set $brand like the brand thet you want
sorry maybe I badly explained myself, in my script i have index.php and details.php when click on a item on index.php an user will redirect to details.php page where he can see item details, here how: stackoverflow.com/questions/42616083/… In details.php i placed a sidebar where i want show random products/item and 5 products from the same brand of products/item actually showing in the page, there a sample: comparewear.com/smartwatches/pebble-time check sidebar hope I explained myself, thank you!
I donwvoted you for using this query $query = "SELECT Brand FROM campi_name WHERE id='" . $id . "'"; on a prepared statement

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.