$frame_type = '';
$ret = mysqli_query($con, "select * from products where status='1' AND frame_type = '$frame_type' ");
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
Get All Rows If The $frame_type Is Empty I am trying this way but i get zero rows , How to fix that Where $frame_type has value then send to query else not
There are a some of things that is wrong with your question (code). But if you only want answer. Just copy this
$frame_type = '';
$query = "select * from products where status='1' ";
// strlen has value
if(strlen($frame_type)) {
$query .= "AND frame_type = '$frame_type'";
}
$ret = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
PS: It's never a safe idea to pass everything to the query. Use prepared statement if you can.
How would I get this to work, because I am just getting errors right now.
$_GET['providers'] is an array of DB column names, which I am checking if = 1 in the below query.
foreach ($_GET['providers'] as $providers) {
$statement = "AND ".$providers."= '1' ";
}
$sql = "select * from users where user_id ='1' ".$statement." ";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
You should use a whitelist to check if the $providers are known column names. You then should concatenate the $statement, otherwise you overwrite that variable on every iteration.
$statement = '';
$columns = array('known', 'columns', 'go', 'here');
foreach ($_GET['providers'] as $providers) {
if(in_array($providers, $columns)) {
$statement .= " AND $providers = 1 ";
}
}
$sql = "select user_id from users where user_id =1 $statement limit 1";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
You also shouldn't use * unless you really want every column. If you just want to see if a row is returned you also can use limit 1 because you don't care about other rows.
You are overwriting $statement every time the loop is running.
$statement = "";
foreach ($_GET['providers'] as $providers) {
$statement .= "AND ".$providers."= '1' "; // note the ".=" to append
}
$sql = "select * from users where user_id ='1' ".$statement." ";
// to debug: echo "Query :: $sql";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
I have a database have 20 tables I want to search all of these I got stuck that how can I search this and solve this query help is very appreciated. Like:
table1
table2
table3
table4
and so one. This is my script.
<?php
if(isset($_GET["search"]))
{
$condition = '';
//$query = explode(" ", $_GET["search"]);
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "`title` LIKE +'%".mysqli_real_escape_string($connect, $text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM countries2 WHERE " . $condition;
$result = mysqli_query($connect, $sql_query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<tr><td>'.$row["title"].'</td></tr>';
}
}
else
{
echo '<label>Data not Found</label>';
}
}
?>
Try with this
sql_query = "SELECT * FROM `countries`,`countries2` WHERE " . $condition;
i have noticed that you are making a search directory or something like this.
you may use FULLTEXT SEARCH with operators and Stemming
after this your query will look like this.
implement as per your requirement ;).
(SELECT * FROM table1 WHERE(col1,col2,clo3) AGAINST(."$search".) IN NATURAL LANGUAGE MODE)
I'm having trouble with a mysqli_query from inside a foreach loop, I'm getting a string from a table first, then separating that into an array. Then I try looping through the array and calling a query inside the loop.
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
for this user the langusges are German, Italian, Danish, and English, but the $posts array only contains the first post found from the first language (German), can anybody help? I am trying to get all of the posts for each language in the $userLangs array.
It's going through the foreach loop okay as the $lang variable that's echoed changes each time but the query still isn't working properly.
Thanks for the help!
select posts.* from posts
left join users on users.language=posts.language
where users.username='your_desiredusername'
group by users.language;
Just try to run this as a single query by filling the username
no need of multiple queries
You an avoid multiple queries by doing a JOIN, using FIND_IN_SET to match on your comma separated list. You probably need to use REPLACE to get rid of the extra spaces in the comma separated list as well.
Then you can just loop around to display the data, displaying the language on change of language:-
<?php
$sql = "SELECT a.Languages AS user_languages,
b.*
FROM users a
LEFT OUTER JOIN posts b
ON FIND_IN_SET(b.Language, REPLACE(a.Languages, ' ', ''))
WHERE a.Username = '$username'
ORDER BY b.Languages";
$langs_result = mysqli_query($con, $sql);
if($row = mysqli_fetch_assoc($langs_result))
{
print_r(explode(', ', $row['user_languages']));
$prev_langauge = '';
while($row = mysqli_fetch_assoc($langs_result))
{
if ($prev_langauge != $row['Languages'])
{
if ($prev_langauge != '')
{
print_r($posts);
}
$posts = array();
echo $row['Languages']."<br>";
$prev_langauge = $row['Languages'];
}
array_push($posts, mysqli_fetch_assoc($row));
}
if ($prev_langauge != '')
{
print_r($posts);
}
}
UPDATE
See this code:
<?php
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
$row = mysqli_fetch_assoc($langs_result);
$langs = $row['Languages'];
// $langs = 'German, Italian, Danish, French'; added this to run the test
// $userLangs = str_replace(" ","",$langs); this is not needed, see the explode below
$userLangs = explode(", ",$langs);
foreach($userLangs as $lang){
echo $lang;
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql); // this is the result of the select *
while($post = mysqli_fetch_assoc($getLangPosts)){ // iterate over all the results
$postField = $post["yourChoiceField..say..Title"]; // get something from each row
array_push($posts, $title); // push into array
}
}
print_r($posts);
Since the initial select is based on username I don't believe the first loop is needed so your code was on the right track.
A second loop was needed to iterate over the posts though and a field to populate the $posts array.
You need to perform mysqli_fetch_assoc in a loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs); // i don't get why you are doing this though
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
array_push($posts, mysqli_fetch_assoc($getLangPosts));
}
print_r($posts);
}
It would help to know how what the select query actually returns.
mysqli_fetch_assoc only fetches one row on each call you need to use it like this:
while ($row_new = mysqli_fetch_assoc($getLangPosts)){
array_push($posts, $row_new);
}
You need to loop the inner query to get the column data
foreach($userLangs as $lang){
echo "$lang <br>";
$sql = "SELECT * FROM posts WHERE Language = '$lang'";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
OR you should use IN clause instead of loop
$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
while($row = mysqli_fetch_assoc($langs_result)){
$langs = $row['Languages'];
$userLangs = str_replace(" ","",$langs);
$userLangs = explode(",",$langs);
print_r($userLangs);
$posts = array();
$sql = "SELECT * FROM posts WHERE Language IN ('".implode(',',$userLangs)."')";
$getLangPosts = mysqli_query($con, $sql);
while($row1 = mysqli_fetch_assoc($getLangPosts))
array_push($posts, $row1['YOUR_COLUMN_NAME']);
}
The following code works well, but I couldn't find a way to limit the number of results. Any ideas please?
$q = "some keywords for search"; // always escape
$keys = explode( " ",$q );
$query = "SELECT * FROM table WHERE para LIKE '%$q%' ";
foreach($keys as $k)
{
$query .= " OR para LIKE '%$k%'";
}
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc())
{
if ($row != 0) {
$title = $row['title'];
}
}
Any help while be appreciated.
Note: the $q holds the search keywords, and then the code explode it, and search for the keywords in 2 steps:
1- as one sentence using ($q as it is).
2- it searches for each keyword as an array after exploding the $q (here is the part that the "foreach" does).
After that the code loops using "while" to find all results match the search request.
Use LIMIT after completing your query.
Also, if you want to get results sorted by some fields in your table, you could also say " ORDER BY fieldname ASC|DESC"
As follows:
$q = "some keywords for search"; // always escape
$keys = explode( " ",$q );
$query = "SELECT * FROM table WHERE para LIKE '%$q%' ";
foreach($keys as $k)
{
$query .= " OR para LIKE '%$k%'";
}
$query .= " LIMIT 10"; //<<<<<<<<<<<<<<<
$result = $mysqli->query($query);
while( $row = $result->fetch_assoc())
{
if ($row != 0) {
$title = $row['title'];
}
Use LIMIT.
SELECT * FROM table WHERE para LIKE '%$q%' LIMIT 2
You can limit the number of results in your MySQL query, like so:
$query = "SELECT * FROM table WHERE para LIKE '%$q%' LIMIT 5";
this will limit it to 5 results. If you want 10, change it to 10