like operator in mysql using php - php

I need to fetch the data from database using like operator.If I use static value i got the answer but using dynamically I can't fetch it.Any body know the answer please help.
$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '2016-01-09%'");
function calendercheck($data)
{
$query = $this->db->query("SELECT * FROM events WHERE start_date LIKE '$data%'");
return($query->result());
}

You can use WHERE docdate BETWEEN '$from' AND '$to' for filter data in mysql.

Related

Only one query instead of two

I have 2 tables, one is called post and one is called followers. Both tables have one row that is called userID. I want to show only posts from people that the person follows. I tried to use one MySQL query for that but it was not working at all.
Right now, I'm using a workaround like this:
$getFollowing = mysqli_query($db, "SELECT * FROM followers WHERE userID = '$myuserID'");
while($row = mysqli_fetch_object($getFollowing))
{
$FollowingArray[] = $row->followsID;
}
if (is_null($FollowingArray)) {
// not following someone
}
else {
$following = implode(',', $FollowingArray);
}
$getPosts = mysqli_query($db, "SELECT * FROM posts WHERE userID IN($following) ORDER BY postDate DESC");
As you might imagine im trying to make only one call to the database. So instead of making a call to receive $following as an array, I want to put it all in one query. Is that possible?
Use an SQL JOIN query to accomplish this.
Assuming $myuserID is an supposed to be an integer, we can escape it simply by casting it to an integer to avoid SQL-injection.
Try reading this wikipedia article and make sure you understand it. SQL-injections can be used to delete databases, for example, and a lot of other nasty stuff.
Something like this:
PHP code:
$escapedmyuserID = (int)$myuserID; // make sure we don't get any nasty SQL-injections
and then, the sql query:
SELECT *
FROM followers
LEFT JOIN posts ON followers.someColumn = posts.someColumn
WHERE followers.userID = '$escapedmyuserID'
ORDER BY posts.postDate DESC

I want to select record multiple key with like

I want to select record from table with key.My code is working but it's not select all record regarding Php and Mysql it select only PHP,Mysql record.
My code is
$skill=PHP,Mysql;
mysql_query("select * from tablename(skill) where fieldname(key) like '%$skill%'");
Try to do this
mysql_query("select * from skill where (key like '%PHP%' OR key like '%Mysql%')");
As you have comma separated values in $skill variable like operator will not work correctly, try using following query
select * from skill where FIND_IN_SET(keyn,'$skill')
Demo sql fiddle at http://sqlfiddle.com/#!2/181c74/5
$skill="PHP,Mysql";
mysql_query("select * from skill where key like '%$skill%'");
I am assuming you want to search for records that contain "PHP", "MySQL", or both. What you're doing is searching for the exact string of "PHP,MySQL".
A quick and dirty solution would be to turn $skill into an array that is split on the commas and then perform the search for each term using the OR keyword.
For example:
$skill = array("PHP", "MySQL");
$query = "select * from tablename(skill) where ";
foreach ($skil in $skill)
$query . "fieldname(key) like '%$skil%' OR ";
//code to remove the OR at the very end
Use in into your Query
select * from `skills` WHERE skillslist in ('Php','Javascript')
Run demo Fiddle

SELECT * BETWEEN AND

I'm trying to select a list of events from a database with MySQL. I'm fairly new to php but usually I can figure stuff out. cant seam to get this to work though..
function get_events_within_dates($da,$dd) {
global $connection;
$query = "SELECT *
FROM events
WHERE date
BETWEEN STR_TO_DATE('$da','Y-m-d') AND STR_TO_DATE('$dd','Y-m-d')
ORDER BY date ASC";
I've used $da and $dd as date of arrival and date of departure..
I can get it to work fine when I replace the variables with exact dates, tried messing around with STR_TO_DATE() and that didn't help either. Any help appreciated.
You are missing % in the format string. Use STR_TO_DATE() as:
STR_TO_DATE('$da','%Y-%m-%d')
Full query as below:
$query = "SELECT *
FROM events
WHERE date BETWEEN STR_TO_DATE('$da','%Y-%m-%d')
AND STR_TO_DATE('$dd','%Y-%m-%d')
ORDER BY date ASC";

PHP / Mysql , compare the values from two while loops

Is there any way to take the contents of my while query below and use it elsewhere in my php script ?. What I am trying to do is collect the answers from a quiz and then compare them to the correct answers, the problem I am having is that the answers come from one table and the questions from another, so I am currently having to run two different while queries, but I cant use $quiz in the 2nd query as it just stays fixed with the last value it spat out. What im basically trying to do it load table A, look inside for the questionID, select that table and pull out the correct answer. Then load table B, looking inside that, select the answer with the same questionID and then use an if statement to compare to to, so that if correctanswer = actualanswer = correct , and loop over this for each question. The main problem that im coming up against though is the first while pulls out all the correct answers just fine, but I cant then use that in the other while to compare too. Im not sure if this is the correct way of doing it, or if there is a better way ?.
LOOK FOR THE CORRECT ANSWER :
$result0 = mysql_query("SELECT * FROM itsnb_chronoforms_data_createquestions
WHERE quizID='$quizID' ORDER BY cf_id ASC");
while($row0 = mysql_fetch_array($result0))
{
$answer = $row0['correctanswer'];
}
LOOK FOR THE ACTUAL ANSWER SUBMITTED :
$result = mysql_query("SELECT * FROM itsnb_chronoforms_data_answerquiz
WHERE quizID='$quizID' AND userID='$userID' ORDER BY cf_id ASC");
while($row = mysql_fetch_array($result))
{
$quiz = $row['quizselectanswer'];
}
Join is propably the best way to do this. But you could also use the php in_array()-Function:
$result0 = mysql_query("SELECT * FROM itsnb_chronoforms_data_createquestions WHERE quizID='$quizID' ORDER BY cf_id ASC");
while($row0 = mysql_fetch_array($result0))
{
$answers[] = $row0['correctanswer'];
}
And Then
$result = mysql_query("SELECT * FROM itsnb_chronoforms_data_answerquiz WHERE quizID='$quizID' AND userID='$userID' ORDER BY cf_id ASC");
while($row = mysql_fetch_array($result))
{
if(in_array($row['quizselectanswer'],$answers)){
...correct...
break;
}
}
SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and
a.quizselectanswer = q.correctanswer
Should return all the correct answers. If you also want the wrong answers, you need to do a outer join on the two tables.

PHP SQL Select From Where

I am having some difficulty running some SQL code.
What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.
This is my SQL in the php:
mysql_query("SELECT * FROM users WHERE joined='$username' GET name")
As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.
How do I do that?
You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.
$result = mysql_query("SELECT name FROM users WHERE joined='$username'");
$row = mysql_fetch_array($result);
echo $row['name'];
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")
Just select the right column in your 'select clause' like above.
Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.
mysql_query("SELECT name FROM users WHERE joined='$username'")
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");
$r = mysql_fetch_array($q);
$name = $r['user_name']; // replace user_name with the column name of your table
mysql_query("SELECT name FROM users WHERE joined='$username' ")
Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories