$user_forum_sql = (!empty($forum_id)) ? " WHERE session_page = " .intval($forum_id) : '';
$sql = "SELECT * FROM " . $session_table_name . $user_forum_sql;`
Don't use SELECT * - query only needed columns
Use prepared statements
Post the whole error message (mysql_error)
Print the whole sql statement and run in manually against the server
mysql queries in php should not end with a semicolon
And get the php syntax right:
$user_forum_sql = (!empty($forum_id)) ? " WHERE session_page = " . intval($forum_id) : '';
$sql = "SELECT col1, col2, col3 FROM " . $session_table_name . $user_forum_sql;
Related
I try to update my table using join in Codeigniter but I get error
Unknown column 'filter.ID_AREA' in 'where clause
$this->db->join('filter', 'filter.ID_AREA = area.ID_AREA', 'left');
$this->db->set('ID_VERIFIKASI', '3');
$this->db->where('pelayanan.ID_AREA', $ID_AREA);
$this->db->where('filter.ID_AREA', $ID_AREA);
$this->db->where('filter.ID_RAYON', $ID_RAYON);
$this->db->where('pelayanan.ID_RAYON = filter.F1_RAYON');
$this->db->where('SUBSTR(TGLRUBAH, 3, 6) = filter.F1_BULANTAHUN');
$this->db->where('ID_VERIFIKASI', '2');
$this->db->where('ID_KENDALA is not null');
$this->db->update('pelayanan');
if ($this->db->affected_rows() > 0) {
return true;
}
else {
return false;
}
How can I update table using join in Codeigniter?
I would recommend that you avoid UPDATE JOIN using the Query Builder. Use an SQL string and query it instead.
From what I see in your snippet you may try to convert it to something like this :
public function updateMyDB() {
$sql =
"UPDATE pelayanan P " .
"LEFT JOIN filter F ON F.ID_AREA = area.ID_AREA " .
"SET ID_VERIFIKASI = 3 "
"WHERE P.ID_AREA = " . $ID_AREA .
"AND F.ID_AREA = " . $ID_AREA .
"AND F.ID_RAYON = " . $ID_RAYON .
"AND P.ID_RAYON = F.F1_RAYON " .
"AND SUBSTR(TGLRUBAH, 3, 6) = F.F1_BULANTAHUN " .
"AND ID_VERIFIKASI = 2 " .
"AND ID_KENDALA IS NOT NULL";
$this->db->query($sql);
return ($this->db->affected_rows() > 0);
}
This is a not a perfect query, there is no reference to table "area" and to ID_VERIFIKASI and ID_KENDALA. I didn't test the query but I'm pretty sure it will fail in the JOIN, but you got the idea. Up to you to create the correct sql for your needs :)
Hope it helps!
You can try this approach:
function edit_save($data, $post_id, $user_id)
{
$this->db->set($data)
$this->db->where('table1.user_id', $user_id);
$this->db->where('table1.post_id', $post_id);
$this->db->where('table1.data_id_fk = table2.data_id');
$this->db->update('table1, table2');
}
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
?>
Try to replace all the query thing by this:
$query = "
SELECT *
FROM subjects
WHERE id = $subject_id
LIMIT 1";
I'd be looking at what your passing into $subject_id.
Please please please don't use SELECT *. Even if you want all of the columns, write them out. If your tables change and get more columns added then your pulling along more data.
I have this MySQL statement writen in PHP, but it seems to contain a syntax-error.
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) : '';
$sql = "SELECT * FROM " . $session_table_name . '"'.$user_forum_sql.'"';
This is the error I'm having. I'm not sure what is causing it.
SQL Error : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '""' at line 1
SELECT * FROM ""
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) .'' ;
$sql = "SELECT * FROM " . $session_table_name.$user_forum_sql;
echo $sql;
Your $session_table_name is empty, so you might have not started your session or have not set the value.
I have a select statement and an update statement. What I would like to do in the update statement is set the value of 'recipes_saved' to the result of the select statement. I have tried to use this:
$query = "UPDATE `users` SET `recipes_saved` = ('SELECT `recipe_1_name` FROM `carbohydrates`') WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
$data= mysqli_query($dbc,$query) or die('Query failed: ' . mysqli_error());
but the query fails.
Any help would be much appreciated.
I think you have an extra ' in your 'SELECT and also in your FROM carbohydrates' and use LIMIT again like:
Try to copy the query below:
$query = "UPDATE `users` SET `recipes_saved` = (SELECT `recipe_1_name` FROM `carbohydrates` LIMIT 1) WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
You could of course remove the back tick if you want to make it less cluttering, like:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates LIMIT 1) WHERE user_id = '" . $_SESSION['user_id'] . "'";
As far as I know, you do not need so many quotes in your query. Try:
$query = "UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='" . $_SESSION['user_id'] . "'";
It would also be useful to log in to your database directly (either command line or a GUI client) and try running the query:
UPDATE users SET recipes_saved = (SELECT recipe_1_name FROM carbohydrates) WHERE user_id='username'
and see if that works.
I'm trying to a single value in my DB...When I run it through the console, it works correctly (as I'm replacing the variables with numbers and text).. However, My query is not returning a value for book ID when I insert the PHP variable for it.. It's because the book_id is unpopulated...
$query = "UPDATE books "
. "SET readstatus='".$readstatus."' "
. "WHERE book_id=".$book_id;
echo $query
The echoed query states:
UPDATE books SET readstatus='half' WHERE book_id=0
The book ID is stored in the URI as bookstatusupdate.php?book_id=
Just cannot figure this one out!
It would help to know the error. Firstly, echo out the query:
$query = "UPDATE books "
. "SET readstatus='".$readstatus."' "
. "WHERE book_id=".$book_id;
echo $query;
I would guess that $book_id is unpopulated, so the query fails. What you should really be doing to make it secure is casting integers with (int) and wrapping strings in mysqli_real_escape_string().
$query = "UPDATE books "
."SET readstatus='". mysqli_real_escape_string( $readstatus )."' "
."WHERE book_id=". (int) $book_id;
If you're trying to get data from the URL, do it like so:
$book_id = (int) $_GET['book_id'];
$query = "UPDATE books "
."SET readstatus='". mysqli_real_escape_string( $readstatus )."' "
."WHERE book_id=". (int) $book_id;
echo $query;