I need some help writing a MySQL query. It needs to look at all records, then sort them by first letter in an alpha-numeric range (Ex. numbers & A-C and then only return in $results the first 15 (in alphanumeric order)
Field to search is 'name'
Simple results showing all (I think 15 limit is right)- how do I modify it?
$results = DB::query("SELECT * FROM Mytable ORDER BY ? DESC LIMIT 15");
For Alpha Range:
$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet
FROM gallery ORDER BY (CASE theme
WHEN '1%' THEN 1
WHEN '2%' THEN 2
WHEN '3%' THEN 3
WHEN 'A%' THEN 4
WHEN 'B%' THEN 5
ELSE -1
END) LIMIT 15");
Getting: ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 15'
What am I doing wrong - trying to list 1's-3's, then A-B.
Update2: In concert w/ above corrected query, I have a simple foreach to assign variables, then have variable echoes in content:
$x = 0
foreach ($results as $row) {
$x++;
if ($x == 1) { // first in query
$t1 = $row['theme'];
$d1 = $row['developer'];
$th1 = $row['thumb'];
}
...
}
Nothing is showing, is there something different I have to do in assigning variables?
Update 3: Do you mean this? Still isn't showing.
$x = 0
while ($row = mysql_fetch_array($results)) {
$x++;
if ($x == 1) { // first in query
$t1 = $row['theme'];
$d1 = $row['developer'];
$th1 = $row['thumb'];
}
...
}
Echo Ex. in body:
<img src="<?php echo($th1); ?>" alt="<?php echo($t1); ?>" />
<span><p class="hname"><?php echo($t1); ?></p>
<p class="hdev"><?php echo($d1); ?></p></span>