MySQL query working in PHPMyAdmin but not working in PHP - php

I have written a MySQL query:
$feedItem->tags =
$result = mysqli_query("SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id"'");
while($tag = mysqli_fetch_array($result))
echo $tag;
The query itself shows the results I need in PHPMyAdmin, but for some reason this isn't showing anything when used in PHP. Any ideas where I'm going wrong please?

You are missing concatenation operator (dot)
Change
"SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id"'"
// You are missing dot . here ^
To
"SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id."'"

Use proper string concatenation in your sql query. Find below corrected query.
$result = mysqli_query("SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id."'");

Related

How to find month wise data from mysql in CI

Just a small quick question,
I want to form the query like this
SELECT *
FROM users
WHERE MONTH( `birthday` ) = MONTH( '1999/05/19' )
Now while in my PHP function, I wants to perform same but I am getting this from post variable , after forming the query my output is like this:
SELECT *
FROM `users`
WHERE MONTH(birthday) = 'MONTH(\"1999-05-19\")'
I want to remove \ from code.
My Code for the following is :
$birthday = $this->input->post('birthday');
$where['MONTH(birthday)'] = 'MONTH('.'"'.$birthday.'"'.')';
$result = $this->User_model->getAnyData($where);
Can anyone tell me where I am going wrong?
use like given below may help you
$birthday = $this->input->post('birthday');
$where['MONTH(birthday)'] = "MONTH(".$birthday.")";
$result = $this->User_model->getAnyData($where);
If the POSTed data contains backslashes, use PHP's stripslashes() to remove them: http://php.net/manual/en/function.stripslashes.php.
Depending whether your string was enclosed by single or double quotes will depend on what you do:
<?php
$sql = 'SELECT *
FROM `users`
WHERE MONTH(birthday) = MONTH("1999-05-19")';
Should work. Also:
<?php
$sql = "SELECT *
FROM `users`
WHERE MONTH(birthday) = MONTH('1999-05-19')";
To remove the single quote, you need to pass false paramete in active record.
$this->db->where('MONTH(birthday)', 'MONTH(YOUR_DATA)', FALSE);
So the query would be like:
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('MONTH(birthday)', 'MONTH(YOUR_DATA)', FALSE);
Try with this,Fetching only month.
$birthday = $this->input->post('birthday');
$birthday = DateTime::createFromFormat('d/m/Y', $birthday)->format('m');
$where['MONTH(birthday)'] = $birthday;
$result = $this->User_model->getAnyData($where);
SELECT * FROM users WHERE MONTH( `birthday` ) = '05 ';
DISPLAY MONTH WISE DATA WITH PHP CODEIGNITER
$query = $this->db->query("SELECT COUNT(id) as count,MONTHNAME(created_at) as month_name FROM users WHERE YEAR(created_at) = '" . date('Y') . "'
GROUP BY YEAR(created_at),MONTH(created_at)");
$record = $query->result();
$output = [];
foreach($record as $row) {
$output[] = array(
'month_name' => $row->month_name,
'count' => floatval($row->count)
);
}
$data['output'] = ($output);

Cant insert string into mysql query

I'm trying to make a login page in PHP, and I'm trying to construct the query here:
$q = 'SELECT * FROM users WHERE userid="'+$username+'"';
When I echo it out with
echo $q
I get 0. When I do
$q = 'SELECT * FROM users WHERE userid="'+"test"+'"';
I get 0. When I do
$q = 'SELECT * FROM users WHERE userid="michael"';
I get my expected result of the string being printed out
Use a . for concatenation, also don't forget to clean the data to prevent mysql injection.
$user_id = 'test';
$q = 'SELECT * FROM users WHERE userid="' . $user_id . '"';
Try using a PDO Prepared statement to protect yourself from SQL injection.
$q = 'SELECT * FROM users WHERE userid = ?';
$stmt = $dbh->prepare($q);
if ($stmt->execute(array($username))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
http://php.net/manual/en/pdo.prepared-statements.php
you can use .
$user_id = 'michael';
$q = 'SELECT * FROM users WHERE userid="'.$user_id.'"';
or use double quotes for the expression and use single quotes for the variables
$user_id = 'michael';
$q = "SELECT * FROM users WHERE userid='$user_id'";
im Believe the second option is smallest and easiest to remember

If remove WHERE from code it works other time it does not [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
if(isset($_GET['open'])){
$sub_id = $_GET['open'];
$sub_query = "SELECT * FROM `forum_sub` WHERE s_id = $sub_id";
$sub_run = mysqli_query($connection, $sub_query);
$sub_row = mysqli_fetch_array($sub_run);
$sub_name = $sub_row['title'];
}
/***
* Error starts here:
***/
$topic_query = "SELECT * FROM `forum_topic` WHERE categories = $sub_name";
$topic_run = mysqli_query($connection, $topic_query);
/***
* Note also here:
***/
while($topic_row = mysqli_fetch_array($topic_run)){
$p_id = $topic_row['id'];
$p_title = $topic_row['subject'];
$p_msg =$topic_row['msg'];
$p_date = $topic_row['date'];
$p_username = $topic_row['u_name'];
$p_view = $topic_row['view'];
}
When I Remove WHERE then it is okay otherwise it gives error
Please help me
I tried every way but it just not happening. if I remove WHERE tag then it is happening properly
forum_topic structure
use this
$sub_query = "SELECT * FROM `forum_sub` WHERE s_id =". $sub_id;
Your variables placed inside of the if statement are undefined when outside of the if statement.
Try
if(isset($_GET['open'])){
$sub_id = $_GET['open'];
$sub_query = "SELECT * FROM `forum_sub` WHERE s_id = $sub_id";
$sub_run = mysqli_query($connection, $sub_query);
$sub_row = mysqli_fetch_array($sub_run);
$sub_name = $sub_row['title'];
$topic_query = "SELECT * FROM `forum_topic` WHERE categories = $sub_name";
$topic_run = mysqli_query($connection, $topic_query);
while($topic_row = mysqli_fetch_array($topic_run)){
$p_id = $topic_row['id'];
$p_title = $topic_row['subject'];
$p_msg =$topic_row['msg'];
$p_date = $topic_row['date'];
$p_username = $topic_row['u_name'];
$p_view = $topic_row['view'];
}
}

Exclude the values in "IN" clause

How should I exclude the values in "IN" clause?
$Graduates = "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,73,74,75,76,84,85,86,87,88,89,91,
92,93,94,95,96,98,99,100,101,103,104,105,106,107,108,109,110,111,112,113,114,117,122,123,126,127,131,135,136,137,
138,139,140,142,143,146,156"; //MajorID
$QryFirstNursing = "SELECT *
FROM ".$SemYearSettings->getYearSem($Enroll)."
WHERE ".$SemYearSettings->getYearSem($Enroll).".MajorID IN (".$Graduates.")
AND ".$SemYearSettings->getYearSem($Enroll).".Category = ".$CategoryIDN;
$resultFirstNursing = openQry($QryFirstNursing);
Please help guys. Thanks
#Ardit Meti rights. It look like this:
$Graduates = "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,73,74,75,76,84,85,86,87,88,89,91,92,93,94,95,96,98,99,100,101,103,104,105,106,107,108,109,110,111,112,113,114,117,122,123,126,127,131,135,136,137,138,139,140,142,143,146,156"; //MajorID
$tableName = $SemYearSettings->getYearSem($Enroll);
$QryFirstNursing = "SELECT *
FROM {$tableName}
WHERE {$tableName}.MajorID NOT IN ('{$Graduates}')
AND {$tableName}.Category = {$CategoryIDN}";
$resultFirstNursing = openQry($QryFirstNursing);

Short Code for PHP string with variable

I have a query string that contains a variable like this
$field_name = 'features';
$value = '5';
$query = "SELECT * FROM Table WHERE $field_name\_tid = '$value'";
My goal is to print out the $query like this SELECT * FROM Table WHERE features_tid = '5';
I put \_ there hoping it would work as escape character, but it didn't work. Is there any way to achieve this without use methods like ". $field_name ." and modifying original variable value?
yes:
$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";
You can use:
$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";

Categories