MYSQL LIKE but not equal - php
I have got database with this structure
Id Country Interests
12 Azerbaijan Acting,Reading
14 Azerbaijan Reading,Writing
16 Azerbaijan Acting,sleeping
And this PHP/MYSQL
$Interests = "Acting,Reading";
$Country = "Azerbaijan";
$query = "SELECT * FROM user_opt WHERE Country='$country' AND Interests='$Interests' ";
$i = substr_count($Interests,',');
for($j = 0; $j <= $i; $j++) {
$a = explode(',', $Interests);
$query2 = "SELECT * FROM user_opt WHERE Country='$country' AND Interests LIKE '%{$a[$j]}%'";
}
So with this code it returns me this ids
12
12
16
12
14
But i want it to return
12
16
14
You probably want to use OR to find all of your interests. To do that, build up your OR statement first, then insert it into the query. NOTE: This is not a secure/preferred way of doing this! Make sure you're using PDO or mysqli and bind your parameters!
$interestArr = [];
foreach($Interests as $Interest) {
$interestArr[] = "Interests LIKE '%{$Interest}%'";
}
$query2 = "SELECT * FROM user_opt WHERE Country='$country' AND (".implode(' OR ',$interestArr).")";
you can avoid the loop using a regexp
SELECT distinct *
FROM user_opt
WHERE Country='$country' AND Interests REGEXP 'Acting|Reading';
I did it using
!=
Like this
$query2=$con->query("SELECT * FROM user_opt WHERE Country='$country' AND Interests!='$Interests' AND Interests LIKE '%{$a[$j]}%'") or die($con->error);
you don't need to loop the result, just using OR comparator
$Interests = "Acting,Reading";
$Country = "Azerbaijan";
$query = "SELECT * FROM user_opt WHERE Country='$country' AND ("
$a = explode(",", $Interests);
$interestQuery = "";
for($j = 0; $j < count($a); $j++) {
if($j+1 == count($a))
$interestQuery = $interestQuery . "Interests LIKE '%". $a[$j] ."%') "
else
$interestQuery = $interestQuery . "Interests LIKE '%". $a[$j] ."%' OR"
}
$query = $query.$interestQuery;
$echo $query;
Related
Display the total of the SUM (price * quantity) from SELECT query [duplicate]
I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working. while ($row = mysql_fetch_assoc($result)){ $sum += $row['Value']; } echo $sum;
You can completely handle it in the MySQL query: SELECT SUM(column_name) FROM table_name; Using PDO (mysql_query is deprecated) $stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes'); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $sum = $row['value_sum']; Or using mysqli: $result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes'); $row = mysqli_fetch_assoc($result); $sum = $row['value_sum'];
$query = "SELECT * FROM tableName"; $query_run = mysql_query($query); $qty= 0; while ($num = mysql_fetch_assoc ($query_run)) { $qty += $num['ColumnName']; } echo $qty;
Try this: $sql = mysql_query("SELECT SUM(Value) as total FROM Codes"); $row = mysql_fetch_array($sql); $sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database: Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off' For this condition, the following would be the sql query and code: $sql_qry = "SELECT SUM(duration_sec) AS count FROM tbl_npt WHERE date='09-10-2018' AND status='off'"; $duration = $connection->query($sql_qry); $record = $duration->fetch_array(); $total = $record['count']; echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table. Method #1 $qry = "SELECT column_value AS count FROM table_name "; $res = $db->query($qry); $total = 0; while ($rec = $db->fetchAssoc($res)) { $total += $rec['count']; } echo "Total: " . $total . "\n"; Method #2 $qry = "SELECT SUM(column_value) AS count FROM table_name "; $res = $db->query($qry); $total = 0; $rec = $db->fetchAssoc($res); $total = $rec['count']; echo "Total: " . $total . "\n"; Method #3 -SQLi $qry = "SELECT SUM(column_value) AS count FROM table_name "; $res = $conn->query($sql); $total = 0; $rec = row = $res->fetch_assoc(); $total = $rec['count']; echo "Total: " . $total . "\n"; Method #4: Depreciated (don't use) $res = mysql_query('SELECT SUM(column_value) AS count FROM table_name'); $row = mysql_fetch_assoc($res); $sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']). Also, make sure you set $sum = 0 before the loop. Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'"); $result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL "SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ sum = $row['SUM(price)']; } echo sum;
remove duplicate camma from mysql query
I am trying to fetch data with mulyiple value with Mysql IN operator but problem I am getting to much (,,,). Here is my query : $get_id = "select rel_blog from pages"; $resu = mysql_query($get_id); $p_id=""; $rel_b=""; $i = 1; while ($page_id = mysql_fetch_array($resu)){ $p_id.= $page_id['page_id']; $rel_b.= $page_id['rel_blog']; } $p_id."<br>"; echo $rel_b."<br>"; echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7"; Ouery output is Like this this is not proper form so please help me . SELECT * FROM pages WHERE rel_blog IN (,114,93,126108default,,,,default,,,,default,,default,,,default,,default,,,,,,,default,,default,,,default,,default,,,,,,,,,,,,,,,,,,,,,,,,,,123,145,144,148,81,107default,,,,,default,,,,,,145,138,118default,,,,,,default,,,,,,,default,,,,,,,default,,,,,,,default,,,118default,,,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,default,,,,,,default,,,,,,,,,default,,,,,,,,default,,,,,default,,,,,,,123,122,125,113default,,,default,,,default,,124,145140default,,,,,,,,,,,,,default,,,,,,,,default,,,default,,,default,,,,default,,,,default,,default,,,,default,,,default,,,default,,default,,,,,,,default,,,,,,default,,,,default,,,,,,default,,,default,,,,137default,,,,,,,default,,,default,,,default,,,,,,default,,,,,,,,,,default,,,,,,,,82,83default,,,,,,default,,,,default,,,,default,,,default,,,default,,,,default,,,,default,,,,default,,,,default,,,default,,,,default,,,,default,,,default,,,default,,,,,,default,,,,,,default,,,,,default,,,default,,,,,default,,,,,default,,,,default,,,default,,default,,default,,default,,,,,,,default,,,,default,,default,,,,default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,,,,default,,,,default,,,,,default,,,,,,default,,,,,,,default,,,,,,default,,,,,,,default,,default,,default,,,,,default,,,default,,,default,,,default,,,,default,114,default,,,,,,117,116default,,,,,default,,,,117,116default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,default,,,default,,default,,,,,,default,,,,,,default,,,,,108default,,,,118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,142,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138,89,126,113,default,default,118,146,82,81,148,107,142,115,109,126,default,123,144,145,80,146,82,81,117,142,128,138,135,93,140,123,144,145,132,82,80,117,83,142,130,140,145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,123,144,118,80,82,81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,134,139,113,141,124,93140,150,150) order by add_date desc limit 7
Actually I can't figure out how you are getting even a single comma... This is how you can try it out. $get_id = "select rel_blog,page_id from pages"; $resu = mysql_query($get_id); $p_id=""; $rel_b=""; $i = 1; while ($page_id = mysql_fetch_array($resu)){ $p_id.= $page_id['page_id']; if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") { $rel_b.= $page_id['rel_blog'] .","; } } $p_id."<br>"; $rel_b = substr($rel_b,0,-1); // get rid of the last comma echo $rel_b."<br>"; echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
You can do this before your query. $rel_b = preg_replace('/,+/', ',', trim($rel_b, ',')); This will remove repeated commas as well as commas from beginning and end of the string.
Try using implode(): $get_id = "select rel_blog from pages"; $resu = mysql_query($get_id); $p_id = array(); $rel_b = array(); $i = 1; while ($page_id = mysql_fetch_array($resu)){ $p_id[] = $page_id['page_id']; $rel_b[] = $page_id['rel_blog']; } $p_id = implode(',', $p_id); $rel_b = implode(',', rel_b); echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
PHP SQL Query for autocomplete
I have a MySqL DB with a table of Properties NAMES(can be more then one word) and I want to run a query to get results for the user text inputs to use for an autocomplete field. The query I currently use is: $query = "SELECT * FROM table WHERE LOWER(name) LIKE '%$value%' LIMIT 7"; But it is not good enough. I tried splitting the input $value but for some reason is not working: $values = explode(" ", $value); $str = ""; for($i = 0; $i < count($values); ++$i) { if( $i == 0) $str .= "LOWER(name) LIKE '%&$values[$i]%' "; else $str .= "AND LOWER(name) LIKE '%$values[$i]%' "; } $query = "SELECT * FROM table WHERE ". $str . " LIMIT 7"; Do you have any suggestions? TX in advance.
Do OR instead of AND in your query: $str .= "OR LOWER(name) LIKE '%$values[$i]%' ";
For loop getting out of control?
I have put together a for loop for a small search function I am building. The code is as follows $keyword = explode("+", $keywords); for($i=0; $i <= count($keyword); $i++){ //check user table $q = "SELECT id, username FROM ".TBL_USERS." WHERE username LIKE '%$keyword[$i]%'"; $result = $database->query($q); while($row=mysql_fetch_assoc($result)){ extract($row); echo "<a href='/profile.php?id=$id'>$username</a>"; } } When this runs, it is somehow retrieving all of the users from the database. Is there any obvious reason for this? If $keywords = "gregg"; , would this still work as there wouldn't be a + sign (only one word). Either way, this still doesn't work when it has multiple words involved! I can confirm the query works perfectly if the term 'gregg' is passed into it. Thanks for reading, hope you can help.
Your for loop looks like: for ($i=0; $i <= count($keyword); $i++) This is going past the end of $keyword by one, so the last query the loop executes is "LIKE '%%'" which would return every row. To fix this, change it to: for ($i=0; $i < count($keyword); $i++) Also, you cannot do "$keyword[$id]". It must be "{$keyword[$i]}"
your index has count($keyword) - 1, not count($keyword); $keyword = explode("+", $keywords); for($i=0; $i <= count($keyword) - 1; $i++){ //check user table $q = "SELECT id, username FROM ".TBL_USERS." WHERE username LIKE '%".$keyword[$i]."%'"; $result = $database->query($q); while($row=mysql_fetch_assoc($result)){ extract($row); echo "<a href='/profile.php?id=$id'>$username</a>"; } } and changed quotes
try this $q = "SELECT id, username FROM ".TBL_USERS." WHERE username LIKE '%{$keyword[$i]}%'"; or better this $q = "SELECT id, username FROM ".TBL_USERS." WHERE username LIKE '%" . $keyword[$i] . "%'";
Consider use OR in the sql: $keyword = explode("+", $keywords); $keywords = array_map("mysql_real_escape_string", $keywords); $q = "SELECT id, username FROM user WHERE username LIKE '%" . implode("%' OR username LIKE '%", $keywords) . "%';"; $result = $database->query($q); while($row = mysql_fetch_assoc($result)) { extract($row); echo "<a href='/profile.php?id=$id'>$username</a>"; }
Get sum of MySQL column in PHP
I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working. while ($row = mysql_fetch_assoc($result)){ $sum += $row['Value']; } echo $sum;
You can completely handle it in the MySQL query: SELECT SUM(column_name) FROM table_name; Using PDO (mysql_query is deprecated) $stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes'); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $sum = $row['value_sum']; Or using mysqli: $result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes'); $row = mysqli_fetch_assoc($result); $sum = $row['value_sum'];
$query = "SELECT * FROM tableName"; $query_run = mysql_query($query); $qty= 0; while ($num = mysql_fetch_assoc ($query_run)) { $qty += $num['ColumnName']; } echo $qty;
Try this: $sql = mysql_query("SELECT SUM(Value) as total FROM Codes"); $row = mysql_fetch_array($sql); $sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database: Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off' For this condition, the following would be the sql query and code: $sql_qry = "SELECT SUM(duration_sec) AS count FROM tbl_npt WHERE date='09-10-2018' AND status='off'"; $duration = $connection->query($sql_qry); $record = $duration->fetch_array(); $total = $record['count']; echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table. Method #1 $qry = "SELECT column_value AS count FROM table_name "; $res = $db->query($qry); $total = 0; while ($rec = $db->fetchAssoc($res)) { $total += $rec['count']; } echo "Total: " . $total . "\n"; Method #2 $qry = "SELECT SUM(column_value) AS count FROM table_name "; $res = $db->query($qry); $total = 0; $rec = $db->fetchAssoc($res); $total = $rec['count']; echo "Total: " . $total . "\n"; Method #3 -SQLi $qry = "SELECT SUM(column_value) AS count FROM table_name "; $res = $conn->query($sql); $total = 0; $rec = row = $res->fetch_assoc(); $total = $rec['count']; echo "Total: " . $total . "\n"; Method #4: Depreciated (don't use) $res = mysql_query('SELECT SUM(column_value) AS count FROM table_name'); $row = mysql_fetch_assoc($res); $sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']). Also, make sure you set $sum = 0 before the loop. Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'"); $result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL "SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ sum = $row['SUM(price)']; } echo sum;