Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with mySQL query. Need * instead of ' * '
    primarykey
    data
    text
    <p>I have the following code that doesn't work. It doesn't work because it will do a query</p> <p><strong>WHERE column = ' * '</strong>, instead of <strong>WHERE column = *</strong></p> <p>I tried to think of a way to get it so it will do WHERE variable = 'var' if a variable is posted in the form and WHERE column = * if not posted, but I can't think of a way, and everything I tried is hacky or not working.</p> <pre><code> if(isset($_POST['variable'])){ $variable=$_POST['variable']; } else{$variable='*';} $sql="SELECT * FROM table WHERE column = '$variable'"; </code></pre> <p><strong>EDIT, Here is the actual code:</strong></p> <pre><code>&lt;form method='post' action='policy.php?go'&gt; &lt;input type='radio' name='gen' value='M'&gt;Male &lt;input type='radio' name='gen' value='F'&gt;Female &lt;select name='state'&gt; &lt;option value='AK'&gt;AK&lt;/option&gt; &lt;option value='WY'&gt;WY&lt;/option&gt; &lt;/select&gt; &lt;input type='radio' name='logic' value='&gt;'&gt;Older Than &lt;input type='radio' name='logic' value='&lt;'&gt;Younger Than &lt;select name='age'&gt; &lt;option value='5'&gt;5&lt;/option&gt; &lt;option value='11'&gt;11&lt;/option&gt; &lt;option value='17'&gt;17&lt;/option&gt; &lt;option value='65'&gt;65&lt;/option&gt; &lt;/select&gt; &lt;input type='submit' name='submit' value='Search'&gt; &lt;/form&gt; &lt;?php if(isset($_GET['go']) &amp;&amp; isset($_POST['submit'])){ if(isset($_POST['state'])){ $state="'".mysql_real_escape_string( $_POST['state'] )."'"; } else{ $state='*';} if(isset($_POST['age'])){ $age=$_POST['age']; //append to query string } if(isset($_POST['logic'])){ $log=$_POST['logic']; //append to query string } else{$log='';} if(isset($_POST['gen'])){ $gen="'".mysql_real_escape_string( $_POST['gen'] )."'"; } else {$gen='*';} echo "&lt;table id='hor-minimalist-b' summary='Employee Pay Sheet' class='tablesorter'&gt; &lt;caption&gt;Age: ".$log." ".$age." Gender: ".$gen." &lt;/caption&gt; &lt;thead&gt; &lt;tr&gt; &lt;th scope='col'&gt;State&lt;/th&gt; &lt;th scope='col'&gt;Number&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;"; // (WHY IS THIS NOT WORKING?) $sql = "SELECT SUM(num) AS sum, state,gen,age FROM `policy-ssi` WHERE age $log $age AND gen = $gen GROUP BY state ORDER BY sum DESC"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload