I have a problem in add value in SELECT query.
$sql=("SELECT `image` FROM `testtable`");
The output: 123.jpg
But I want output: 127.0.0.1/home/galery/123.jpg
So I tried:
$path='127.0.0.1/home/galery/';
.........
$sql=("SELECT $path+`image` FROM `testtable`");
But it's not working.
There are two ways to accomplish this.
Method 1:
Use string concatenation to join the path to the result from the SQL:
$path = '127.0.0.1/home/galery/';
$sql = "SELECT `image` FROM `testtable`";
// Run the query...
$result = $path . $sql;
In php, string concatenation is performed with the . operator. Also see here.
Method 2:
The second method is via the CONCAT SQL function:
$sql = "SELECT CONCAT('" . $path . "', `image`) FROM `testtable`";
Or:
$sql = "SELECT CONCAT('{$path}', `image`) FROM `testtable`";
See this question for the difference between these options.
$sql=("SELECT CONCAT('$path',`image`) FROM `testtable`");
Use concatenation like below....
$sql=("SELECT".$path."+image FROM test")
Here, text in double quotes are string
Related
I'm trying to create a dynamic code that would ready from any table with the certain name but the difference between each table name is a number that is generated by a variable: for example :
//that's how I get my variable the value for example is = 3
$pid = $GLOBALS["localid"];
//the table name for example is tablename_3
$strTable = "tablename_" .$pid;
//here's how the query should look like
$query = "SELECT * FROM . $strTable . where .....;
I'm making a mistake somewhere but can't figure it out and would appreciate a little help please
Remove the dots and also make sure you have single quotes aroung where
$query = "SELECT * FROM $strTable where '.....';
Besides the comments about do or don't build your queries like this...
You're not closing the quotes properly.
$query = "SELECT * FROM . $strTable . where .....; //Double quote not closed.
should be:
$query = 'SELECT * FROM' . $strTable . 'where .....'; //Single quoted strings concatenated with variable.
or
$query = "SELECT * FROM $strTable where ....."; //Variable inside double quoted string.
I have a basic query as follows
$query = "SELECT * FROM files WHERE (subject_code = '".$subject_code."' )";
I have to modify this query to create a query where we have multiple subject codes fetched from an array.
$query1="SELECT subject_code FROM subject WHERE (sbranch = '".$branch."' )";
$result = mysqli_query($bmysqli, $query1);
Now result holds all the required subject_codes say result[0]=SC1,result[1]=SC2 etc....
Now I want to the original query to be modified something as follows
$query = "SELECT * FROM files WHERE (subject_code = SC1 OR subject_code = SC2 .....)";
Note that i don't know what are the values of SC1 SC2 etc.It depends on the branch and is inside $result.
How can i accomplish this?
You can use a subquery like this:
$query = 'SELECT * FROM files
WHERE subject_code IN (
SELECT subject_code FROM subject WHERE sbranch = "' . $branch . '"
)';
You can use implode to convert array to string comma delimited
like below:
$branch_array = array('subject1', 'subject2', 'subject3');
$branch = implode("','", $subject_array );
$query1="SELECT subject_code FROM subject WHERE sbranch in ('".$branch."')";
result:
'subject1', 'subject2', 'subject3'
Hope that resolve your issue
You can use Mysql In operator
Like this
SELECT * FROM files WHERE subject_code in ('SC1','SC2')";
You can achieve ('SC1','SC2') by using implode php function,
like this:
$subCodes = implode("','", $subject_array );
SELECT * FROM files WHERE subject_code in ('".$subCodes ."')"
$query1="SELECT subject_code FROM subject WHERE (sbranch = '".$branch."' )";
$result = mysqli_query($bmysqli, $query1);
$codes=(array)$result; //this will convert it in normal array. old school typecasting ;)
$codes=array_values($codes);
$query = "SELECT * FROM files WHERE subject_code in (".implode(",",$codes).")";
//considering result is an object of subject codes.
CODE UPDATED ABOVE as it is an object.
You don't need to append to the first query. Just build a new query using MySQL in operator and php implode function.
SELECT * FROM files WHERE subject_code in ('SC1','SC2')
You can amend your php array to comma delimited string of SubjectCodes.
then:
$query1="SELECT * FROM files WHERE subject_code in (".$NewString.") ";
$NewString will look something like this:
"'SC1', 'SC2', 'SC3'"
$result = array(0=>'SC1',1=>'SC2'); //this is your array
$branch = implode("','", $result );
$query .= "
SELECT * FROM files WHERE subject_code in ('".$branch."') "; //this is query
I have the following php code:
mysql_query("SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'");
I also have a string:
$mynumbers = "AND b.question_code IN (1);";
How can I combine this string withing the mysql_query()?
Thanks,
mysql_query("SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."' " . $mynumbers);
But keep in mind that AND GROUP BY all_surveys.question_code IN (1); is incorrect sql and makes no sense.
You can also do like this if you want more simplicity;
$sql="SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
$sql.=$mynumbers;
echo $sql;
Also as zerkms said your sql seems to be incorrect
First is, you can not combine above two statements, the alternatively you can do like this-
//Here i assume that you want to concatenate 2nd condition on particular situation so you need to add if condition or else you can directly contcate it with "." (dot) operator.
$query = "SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
if(//your condition) $query .= "AND GROUP BY b.question_code IN (1);";
mysql_query($query);
Dont use AND before Group By
Try the following code,
$query = "SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
$query .= " GROUP BY b.question_code IN (1)"
mysql_query($query)
I'm running a very simple query that I think should work. The only thing that I haven't done before is put a php variable in the WHERE clause of the query. The variable $X is a numerical value, say 100. When I run this query, I just get a value of 0 returned. Am I doing something obviously stupid?
SELECT generator_64k.n
FROM generator_64k
WHERE generator_64k.n<= '$X'
I've looked around the web and also tried this:
SELECT generator_64k.n
FROM generator_64k
WHERE generator_64k.n<= '" . $X . "'
But this also just returns 0.
Any ideas? Thanks in advance.
$query = "SELECT generator_64k.n FROM generator_64k WHERE generator_64k.n<= {$X};";
Try this one, or post your PHP code.
<?php
$X = 100;
$query = "SELECT n FROM generator_64k WHERE n <= $X";
$result = mysql_query($query);
if (!$result) {
echo ('Query error: ' . mysql_error());
}
E.g of php and using variables
$query = "select * from table1 where col1 <=" .$myVariable;
$result= mysql_query($query);
The mysql_query() function returns false on error (false == 0), otherwise, it returns a resource. mysql_query does not return the value from the result set. You must use mysql_fetch_assoc or something similar to fetch the rows from the result set.
Also, ensure that you wrap the query in double quotes so PHP can expand the variable $X.
Use mysql_error to fetch the error from the last call to mysql_query.
make it like this
$sql="select `username` from `users` where id='$newid';";
mysql_query($sql);
here $newid is the int value.
The symbol used before and after username, to get this you have to press the key just below esc .
You can't have ' around your numeric value. MySQL will treat it as string.
You should do this instead
" WHERE number <= " . (int)$val . " .. "
// or (but not recommended due to security problem)
" WHERE number <= $val "
I have an array of ID:s, and the ID:s are in this format:
Bmw_330ci_89492822
So it's a string!
Now, I have this code to find whatever is in that array, in MySQL:
($solr_id_arr is the array I mentioned above, it contains string ID:s)
ex: $solr_id_arr[0] outputs Bmw_330ci_89492822
$solr_id_arr_imploded = implode(", ", $solr_id_arr);
$query = "SELECT * FROM my_table WHERE ad_id IN ('$solr_id_arr_imploded')";
$qry_result = mysql_query($query) or die(mysql_error());
Problem is this wont work because (I think) that there should be quotes around each of the imploded elements in order for MySQL to find the match. The field in MySQL I am matching is of type Varchar.
Here is the $query echoed:
SELECT * FROM my_table WHERE ad_id IN ('Bmw_m3_cool_565440282, Bmw_m5_839493889')
Do you have any other solutions for this, all I need is to find matches in MySQL which are inside this array!
Thanks
Don't surround the entire thing in quotes. It is looking for where ad_id is 'Bmw_m3_cool_565440282, test'
Use
SELECT * FROM my_table WHERE ad_id IN ('Bmw_m3_cool_565440282', 'test')
A quick fix would be to change:
//this
$solr_id_arr_imploded = implode(", ", $solr_id_arr);
//to this
$solr_id_arr_imploded = implode("', '", $solr_id_arr);
This one seems complicated but it's more safer and fastest one
function escaped($str)
{
return mysql_escape_string($str);
}
$arrayOfIds = array_map("escaped", $solr_id_arr);
$solr_id_arr_imploded = implode(", ", $arrayOfIds);
$query = "SELECT * FROM my_table WHERE ad_id IN ('$solr_id_arr_imploded')";
$qry_result = mysql_query($query) or die(mysql_error());
Simple switch to ', ' in implode():
implode("', '", $solr_id_arr);
This, together with the hardcoded quotes in the SQL string will format them as separate items.
Previous answers will work fine.
Just make sure the strings themselves do not contain quotes. If they do, escape each string before you do the implode().
If it were my code I'd write it like this:
$solr_id_arr_imploded = "'" . implode("', '", $solr_id_arr) . "'";
$query = "SELECT * FROM my_table WHERE ad_id IN ($solr_id_arr_imploded)";
$qry_result = mysql_query($query) or die(mysql_error());
...just because it keeps all the quoting work in one place. You might also want to make sure that the array isn't empty before entering this block of code. Otherwise the SELECT will match all empty ad_id's, which probably isn't what you wanted. We're also assuming that the elements of the array don't include any quote characters (or user-provided strings that haven't been sanity-checked).