Hi what's the best way to do a 'order by' from a query without executing a query for another time? Lets say I have a search query when I have to find names or surnames using a form ,an example :
//Array for storing WHERE part of query
$where=array();
if(!empty($name)) AND !empty($surname)){
$where[]="Name LIKE '%$name%'" .
"AND Surname LIKE '%$surname%'";
}
elseif(!empty($name)){
$where[]="Name LIKE '%$name%'";
}
elseif(!empty($surname)){
$where[]="Surname LIKE '%$surname%'";
}
$where = implode(' AND ', $where);
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name DESC";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
$name = $row[0];
$surname = $row[1];
echo"$name";
echo"$surname";
}
How can I display Name or Surname in ASC mode(ORDER BY Name ASC) after my search"Name LIKE %surname%",without executing again the SQL query? I want for example when I search for a name containing "ich"word to have a list in this way:
MICHAEL
PICHAEL
XICHAEL
=> the ASC mode
XICHAEL
PICHAEL
MICHAEL
=> the DESC mode
I really don't think I'm understanding your question correctly, but I'm going to go out on a limb here and guess you just want to order by two different columns?
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name DESC, Surname ASC";
by the way you had an extra " in your query string
Also, you may want to look into using mysql_fetch_assoc instead of mysql_fetch_array
This is just me nitpicking now ... but instead of using:
echo"$name";
echo"$surname";
You can just write:
echo $name;
echo $surname;
Arr you talking about reversing the order of an array like this?
<?php
//Array for storing WHERE part of query
$where=array();
if(!empty($name) && !empty($surname)){
$where[]="Name LIKE '%$name%'" .
"AND Surname LIKE '%$surname%'";
}
elseif(!empty($name)){
$where[]="Name LIKE '%$name%'";
}
elseif(!empty($surname)){
$where[]="Surname LIKE '%$surname%'";
}
$where = implode(' AND ', $where);
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name";
$res = mysql_query($sql);
$names = array();
$surnames = array();
$wholeNames = array();
$x='0';
while ($row = mysql_fetch_array($res)) {
if(!empty($row['Name'])){
$names[$x] = $row['Name'];
}
if(!empty($row['Surname'])){
$surnames[$x] = $row['Surname'];
}
if(!empty($row['Name'])&&!empty($row['Surname'])){
$wholeNames[$x] = $row['Name'].' '.$row['Surname'];
}
$x++;
}
print "<pre>\n";
print_r ($names);
print_r ($surnames);
print_r ($wholeNames);
print "</pre>\n";
$reversed_names=array_reverse($names);
$reversed_surnames=array_reverse($surnames);
$reversed_wholeNames=array_reverse($wholeNames);
print "<pre>\n";
print_r ($reversed_names);
print_r ($reversed_surnames);
print_r ($reversed_wholeNames);
print "</pre>\n";
?>
Something to watch out for, if you're using LIKE with the wildcards a search for "Art" would match Bart, Arthur, carter, and any other word with "Art" in it.
Related
I have tried with sort() in front of $new but it won't work, where should I put it?
$new = mysql_query("SHOW COLUMNS FROM Professors");
while($row = mysql_fetch_array($new)) {
$output .= "{$row['Field']}";
echo $output;
}
I have also tried something like this:
$new = "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'Professors' ORDER BY column_name";
but, while on my SQL compiler it will sort them correctly, they won't get printed on the page with the exact code above.
Try this, then:
$new = "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'Professors' ORDER BY column_name";
while($row = mysql_fetch_array($new)) {
$output = $row['column_name'] . "<br>";
echo $output;
}
You were ordering it correctly and selecting by column name but you weren't getting the correct field names.
I am trying to work around with dynamic table creation and data fetching. I am trying to get the data using following code :
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_array($result);
echo "<br>".$row['$col_name'];
But, I am unable to get any data back. I checked printing the query and running it in php my admin and its working as I want. But I guess variable in array might not be working I guess.
Please help me regarding the same. Thanks.
The Whole loop looks something like this :
$myQuery = "SELECT * FROM information_schema.columns WHERE table_name = '$tabname'";
$re = mysql_query($myQuery);
while($row = mysql_fetch_array ($re)){
if(!empty ($row)){
$col_name = $row['COLUMN_NAME'];
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
echo "<br>".$myQuery;
$reqq = mysql_query($myQuery);
$roww = mysql_fetch_array($reqq);
echo "<br>".$roww[$col_name];
}
}
You are fetching an array, not an assoc array. Use this:
echo "<br>".$row[0];
Edit: Having looked a little more, this may not be correct. You can set fetch_array to return assoc arrays.
You cannot parse variables through single quotes '
echo $row['col_name']; // manually typed string.
or
echo $row[$col_name];
or
echo $row["$col_name"];
You tried that->
echo "<br>".$row[$col_name];
OR
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_assoc($result);
echo "<br>".$row[$col_name];
Cause like said #Fluffeh it's not a associative array
Have you tried looping through the results array as such:
while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
echo "<br />";
}
I would suggest the following solution. Give the parameter $col_name and AS Col_Name
Then the $row['Col_Name'] can always be set to the parameter no matter what the value.
$myQuery = "SELECT ".$col_name." AS Col_Name FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_array($result);
echo "<br>".$row['Col_Name'];
I think my mind is just drawing a blank, but basically, I want to create an associative array from various sql results
The array needs to look like:
$people = array(
"+1123456789" => "Phil"
);
Here is my SQL Statement
$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";
Thanks!
Edit:
Also, there can be multiple rows that were selected by the sql statement
$sql = " SELECT phonenumber6, firstName FROM members WHERE departmentID = 4 AND phonenumber6 <> '+1';";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result)) {
echo $people[$row['phonenumber6']] = $row['firstName'];
}
while($row=mysql_fetch_assoc($query)) {
$people[$row['phonenumber6']] = $row['firstName'];
}
Addendum
Dunno what you want to echo. Anyway the right syntax is:
while($row=mysql_fetch_assoc($query)) {
$people[$row['phonenumber6']] = $row['firstName'];
echo $row['phonenumber6']. '=> '.$row['firstName']."<br />\n";
}
This will give you an associated array from a mysql result set:
$assoc = mysql_fetch_assoc ($res);
I am using the Select query as
SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'
Here I need all the records whose ordering is less than 1 of the selected record's order($rec['ordering'] = getting from other select query ) when I am trying to echo the query I am not getting complete statement but getting only this -1' AND parent = '0'
here is the whole snippet
$where = ' WHERE (id = ' . implode( ' OR id = ', $cid ) . ')';//Pranav Dave Coded
echo $selquery = "SELECT id, ordering FROM `jos_menu`".$where; //Pranav Dave Coded
$db->setQuery( $selquery );//Pranav Dave Coded
$record = $db->loadAssocList(); //Pranav Dave Coded
if ($model->orderItem($id, -1)) {
echo "<pre>";
print_r($model);
/*exit;*/
//echo $updorderup = mysql_escape_string($model->_db->_sql);//Pranav Dave Coded
foreach($record as $rec)//Pranav Dave Coded
{
echo $aboverow = "SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'";
$db->setQuery( $aboverow );
$above = $db->loadAssoc();
echo "<pre>";
print_r($above);
}//end of foreach
}//end of if
Please suggest me where I am getting wrong.....
It looks like you may need to unwrap the -1 from the quotes:
WHERE ordering='".($rec['ordering'] - 1)."' AND parent = '0'";
Why do you trying to put everything inline?
Why not to make some preparations first?
Why not to compare resulting query with sample one?
Why don't you check every step if it return proper result?
$val = $rec['ordering'] - 1;
//let's see if $cal has proper value:
echo $val."<br>";
$sql = "SELECT id, ordering FROM `jos_menu` WHERE ordering = $val AND parent = 0";
//let's see if query looks good:
echo $sql;
//let's print sampe query to compare:
echo "<br>" ;
echo "SELECT id, ordering FROM `jos_menu` WHERE ordering = 1 AND parent = 0";
As Daniel said, you need to remove the quotes around the -1. Currently its trying to minus a string, which it wouldn't be happy with at all ;)
So I have a list of CSVs in one table. (EG: 1,3,19 )
I want to search out all of the usernames from the other table where the ids match any of those.
I feel like I should be able to do something like:
<?
$query = "SELECT player_ids FROM cast_list WHERE game='".$gameid."' ";
$result = mysql_query($query) or die(mysql_error());
$playerquery = "SELECT username,id FROM players WHERE id IN (".$result.") ORDER BY username;
$player_result = mysql_query($playerquery) or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($player_result) ) {
echo "<li>".$row['username']."</li>";
}
echo "</ul>";
?>
but I can't get it to work. What am I doing wrong?
You can also use a subquery (which will be faster):
$playerquery = "SELECT username,id
FROM players
WHERE id IN (SELECT player_ids FROM cast_list WHERE game='".$gameid."')
ORDER BY username";
Btw if game is an integer field you don't have put quotes (' ') around the value.
The idea is correct, but you need to transfer the $result to an actual string array:
$game_ids = array();
while ($row = mysql_fetch_array($result) ) {
$game_ids[] = .$row[1];
}
Now using implode to convert the array to a comma separated values with a comma:
$playerquery = "SELECT username,id FROM players WHERE id IN (" . implode(",",$result) . ") ORDER BY username;