Jump to content

Quick Question about Mysql and PHP


paulman888888

Recommended Posts

I have my code, and would like to use the WHERE function. My code below.

$sort = isset($_GET['order']) ? mysql_real_escape_string($_GET['order']) : 'id';
$result = mysql_query("SELECT * FROM unit4_music ORDER BY $sort WHERE who=$_GET['who'] ASC"); //the part that needs editing

 

The problem is that the my URL will not always have something with the who variable.

 

Ill explain

mypage.php?who=50 <<-- thats fine because it will show everything where who equals 50

mypage.php <<-- theres know ?who=number, and when its like that i would not like to use the WHERE function.

 

Its hard to explain but i bet theres a simple solution.

 

THankyou

paul

Link to comment
Share on other sites

just use an if statement like you did with the sort

<?php
$sort = isset($_GET['order']) ? mysql_real_escape_string($_GET['order']) : 'id';
$where = isset($_GET['who']) ? "WHERE `who` = '".mysql_real_escape_string($_GET['who'])."' " : "";
$result = mysql_query("SELECT * FROM unit4_music $where ORDER BY $sort "; ; //the part that needs editing
?>

 

Also fixed the statement, WHERE comes before ORDER

 

Ray

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.