My Php code is
<?php
include('session.php');
include('db.php');
$session=$_SESSION['agent'];
$home1 = "SELECT fullname FROM users WHERE role='$session'";
$result1=mysql_query($home1) or die(mysql_error());
while ($row1=mysql_fetch_array($result1)) {
echo $gallery = "'".$row1[0]."',"; // output is 'sam','teja','multiplevaluessoon',
$home = "SELECT * FROM chat WHERE chat.from IN ('$gallery')";
}
$result=mysql_query($home) or die(mysql_error());
while ($row=mysql_fetch_array($result)) {
$msg =$row["message"];
echo $randomUserId=$row["from"];
echo $msg . "<br/>";
}
?>
here $gallery contains ',' comma at the end, so i am getting a error in where clause, when i am trying to trim the output of $gallery
'sam','teja','multiplevaluessoon',
last charcter inside loop i am getting
'sam''teja''multiplevaluessoon'
which is trimming all the commas due to while loop, i need last comma to be trimmed so that i can execute it in where clause
please suggest me any ideas
Make use of rtrim().
$yourfinaltext = rtrim($yourcommadelimitedstring, ',');
Change your while like this.
while ($row1=mysql_fetch_array($result1)) {
$gallery .= "'".$row1[0]."',";
$home = "SELECT * FROM chat WHERE chat.from IN ('$gallery')";
}
echo rtrim($gallery,',');
$rest = substr("abcdef", 0, -1); // returns "abcde"
http://php.net/manual/en/function.substr.php
Remove comma as this...
$gallery=substr_replace($gallery ,'',0,1);
Related
Hi i need to do a request to mysql to get some data where filed name = $letter
but the variable $letter may contain '
my problem is this i try to use this :
$letter = mysqli_real_escape_string($conn,strtolower($letter));
$us = $conn->query("SELECT id FROM singers where trim(LOWER(name)) = '".$letter."'");
My problem is in if $letter was I'm legend, after executing this line it become I\'m legend so it can't be find in database because in database it's stored as i'm legend
how i can resolve this and get right result.
echo $letter;
$letter = mysqli_real_escape_string($conn,strtolower($letter));
echo $letter;
RESULT
i'm
i\'m
why you have used
trim and LOWER in your column name
"SELECT id FROM singers where trim(LOWER(name)) = '".$letter."'" //why trim and LOWER
use this
"SELECT id FROM singers where name = '".$letter."'"
Also try this like
$letter = mysqli_real_escape_string($conn,strtolower($letter));
$sql = "SELECT id FROM singers where name = '".$letter."'";
// echo $sql; die; print this and run in phpmyadmin to check
$us = $conn->query($sql);
if ($us !== false) {
echo 'done';
}
else{ die('failed!' . mysqli_error($conn));}
there is difference between field and value in where
the \ added is the problem
I have a SQL query and an array where I put values to exclude. When I perform my query, I would like to test my variable.
If not empty ==> Display values
If empty ==> Display one time 0 result.
My SQL query gives in output these values:FGFR1e12, FGFR3e7, FGFR3e14
When I perform my php script, I manage to display values but it doesn't enter in the loop else when the variable is empty.
Does anyone know what I am doing wrong in my script ?
Here's my script:
<?php
require_once ('config.php');
$VariantNContrib = "SELECT DISTINCT Reference FROM mytable";
$PerformVariantNContrib = mysqli_query($conn, $VariantNContrib) or die(mysqli_error($conn));
// Values to exclude are stored in an array
$arrayNoContrib = array(
'FGFR1e12',
'FGFR3e7',
'FGFR3e14'
);
while ($rowVarNContrib = mysqli_fetch_assoc($PerformVariantNContrib)) {
if (!in_array($rowVarNContrib["Reference"], $arrayNoContrib)) {
$NContribList = '' . implode(',', $rowVarNContrib) . '; ';
if (!empty($NContribList)) {
echo '<br/>Variants:' . $NContribList . '<br/>';
}
else {
echo "0 results";
}
}
}
?>
Your code should be:-
$arrayNoContrib=array('FGFR1e12', 'FGFR3e7', 'FGFR3e14');
while($rowVarNContrib = mysqli_fetch_assoc($PerformVariantNContrib)) {
if(!empty($rowVarNContrib) && !in_array($rowVarNContrib["Reference"],$arrayNoContrib)){ // check $rowVarNContrib array is not empty
$NContribList=implode(',',$rowVarNContrib); // remove empty string
echo "Variants: $NContribList"."<br>"; // remove earlier <br>
}else{
echo "0 results"."<br>"; // add <br>
}
}
Hi I dont know why you have used implode if i am right. You have a array in which you have some values and you are fetching some reference and if all are same then you are displaying 0 result and if not then you are displaying that refence number. Then Please once try this one.
<?php
require_once ('config.php');
$VariantNContrib="SELECT DISTINCT Reference FROM mytable";
$PerformVariantNContrib=mysqli_query($conn,$VariantNContrib) or die(mysqli_error($conn));
//Values to exclude are stored in an array
$arrayNoContrib = array(
'FGFR1e12',
'FGFR3e7',
'FGFR3e14'
);
$i = 0;
while($rowVarNContrib = mysqli_fetch_assoc($PerformVariantNContrib)) {
if (!in_array($rowVarNContrib["Reference"], $arrayNoContrib)) {
$NContribList = trim($rowVarNContrib["Reference"]);
if (!empty($NContribList)) {
echo '<br/>Variants:' . $NContribList . '<br/>';
$i++;
}}
}
echo $i." results";
empty() function does not return true for string which is single space. " ".
$NContribList=''.implode(',',$rowVarNContrib).' ';
In above code you've space at the end of the string and that is the reason empty() is returning FALSE.
Either remove that space from the string or use PHP trim() to remove unnecessary spaces.
I'm trying to list data from my database, and I need to use variables inside of my array, but when echoing it I want it to remove the last comma but it doesn't seem to work.
$forum_usersonline = $kunaiDB->query("SELECT * FROM users WHERE user_loggedin = '1'");
while($forum_usersonline_fetch = $forum_usersonline->fetch_array()) {
$usersonlineuname = $forum_usersonline_fetch["user_name"];
$onlinelist = array($usersonlineuname, ' ');
echo implode($onlinelist, ',');
}
It always returns with user1, user2, so how should I do it?
Your can do this job easily in sql. If your database is mysql try following
SELECT GROUP_CONCAT(user_name) as user_name FROM users WHERE user_loggedin = '1'
or if your database is postgres try following
SELECT string_agg(user_name, ',') as user_name FROM users WHERE user_loggedin = '1'
above query result return comma separated user_name.
You could use chop(); which removes characters from the right end of a string.
An example:
<?php
$str = "Random String Ending With,";
echo $str . "<br>";
echo chop($str,",");
?>
Or you could use rtrim(); like:
<?php
$str = "Random String Ending With,";
echo $str . "<br>";
echo rtrim($str,',');
?>
Or you could also use substr like:
<?php
$str = "Random String Ending With,";
echo substr($str,0,-1)."<br>";
?>
use rtrim(string,',')
For reference goto http://www.w3schools.com/php/func_string_rtrim.asp
The problem is you're doing the implode inside the while loop, it should be done outside after building your final array. This is what I would do:
$forum_usersonline = $kunaiDB->query("SELECT * FROM users WHERE user_loggedin = 1");
while ($row = $forum_usersonline->fetch_array()) {
$onlinelist[] = $row["user_name"];
}
echo implode($onlinelist, ",");
However, this should not be the case as you should be using CONCAT when doing your database query.
EDIT1 : used double quotes and single quotes but I am getting same error.
EDIT2 : same query is returning me result in mysql shell
I am selecting a row from a table.
if(!isset($_GET['title']) || !isset($_GET['user'])){
echo "hi"; //something come here
}
else{
$title = $_GET['title'];
$title = mysqli_real_escape_string($conn,$title);
$user = $_GET['user'];
$user = mysqli_real_escape_string($conn,$user);
echo $title ;
echo $user ;
// tried giving value directly to test but no luck
$query = "SELECT * FROM site WHERE client=\"Chaitanya\" && title=\"werdfghb\" ";
$result5 = mysqli_query($conn,$query) or die(mysqli_error());
$count = mysqli_num_rows($result5);
echo $count ;
while($result9 = mysqli_fetch_array($result5)){
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
include ( 'counter.php');
addinfo($page);
}
In my database there is a row with columns title and client and the values I entered are in that row but when I echo count(no of rows) it is showing zero.
Is there anything wrong with code ?
The error you are getting is due to the line
$page = $kk;
in this code $kk is not declared previously. The defined $kk is in the while loop scope.
declare the variable like this in the outer scope from the while loop
...
$kk = null;
while($result9 = mysqli_fetch_array($result5)) {
$kk = $result9['url'];
echo $kk ;
}
$page = $kk;
...
Error on Fetching Data
You have to crack you SQl into smaller pieces and test the code like this.
run the query SELECT * FROM site without any where and get the count
run the query SELECT * FROM site WHERE client='Chaitanya' and get the count
SELECT * FROM site WHERE title='werdfghb' and check the count
Then run the whole query
And see the results. This way u can find out in where the issue is in your SQL code. I prefer you use the mysql client to execute this queries
As I pointed out in my comment, $kk is undefined in the $page = $kk;, since it is declared in the while loop.
Do something like:
$kk = ''; //can also do $kk=NULL; if you want.
while($result9 = mysqli_fetch_array($result5)) {
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
try this one
$client = "Chaitanya";
$title = "werdfghb";
$query="SELECT * FROM site WHERE client='".$client."' and title='".$title."' ";
you can also use this
$query="SELECT * FROM site WHERE client={$client} and title={$title} ";
My code checks if there is $GET value, if not then assign ALL values of array.
Seems like simple thing,not sure why its not working.
if(isset($_GET["smonth"])) {$smonth= $_GET["smonth"];
}
else {$smonth =12;} working , but not what I want
else {$smonth =array (1,2,3,4,5,6,7,8,9,10,11) ;}
After that I would like to use it in SQL :
and d.month_of_year = '".$smonth."%'
That would be something like
and month_of_year = (all values of array) or 1 value)
My Question:
What would be best solution to check, if active month is available? If not, assign All months to query.Thank You
The built-in PHP functions of in_array and implode should solve your issue:
in_array('1', $_GET["smonth"]); // checks if January is in $_GET["smonth"]
implode("," , $_GET["smonth"]); // Pull all of the values out of $_GET["smonth"] as a A STRING
Try in your statement and d.month_of_year IN (" . implode(',', $smonth) . ")
= operator checks for single value. If you want to check multiple values, use in.
and d.month_of_year in (".$smonth.")
You also have a % there, which works with LIKE queries.
<?php
if(isset($_GET['month'])){
$month = date('m'); //This would give you the index of the current month.
$array = array('01','02','02');
$query = "select * from table where month = ";
if(in_array($month,$array)){
$query = "select * from table where month = '".$month."'";
//Then query here
}
else
{
$query = "select * from table";
$where = "";
foreach($month as $m){
$where .= ' month = "'.$m.'" and ';
}
//There would be a ending and pls just try remove it
$query .= $where;
// then query here
}
}
?>