I am trying to pass a variable that looks like this "+ interval 4" or "- interval 5" to this PDO statement.
$query = "INSERT INTO
" . $this->table_name . " (employee_id, work_date)
SELECT DISTINCT emp_num,
CURDATE() :test DAY FROM " . $this->table_name3 . "";
My bindParam
$stmt->bindParam(":test", $this->interval);
I not sure if I should be using bindValue having a hard time figuring this out.
This works fine but i know it is not right
$query = "INSERT INTO
" . $this->table_name . " (employee_id, work_date)
SELECT DISTINCT emp_num,
CURDATE() " . $this->interval . " DAY FROM " . $this->table_name3 . "";
and here is the whole function if that helps
function sqlbuild(){
$query = "INSERT INTO
" . $this->table_name . " (employee_id, work_date)
SELECT DISTINCT emp_num,
CURDATE() " . $this->interval . " DAY FROM " . $this->table_name3 . "";
$stmt = $this->conn->prepare( $query );
echo json_encode($stmt);
$stmt->bindParam(":test", $this->interval);
//$stmt->bindParam(1, $this->interval);
echo json_encode($this-> interval);
if($stmt->execute()){
return true;
}else{
return false;
}
}
Related
As far as I can tell, I select 6 columns from the table, and I bind 6 variables so why does it tell me that it does not match?
This is for a rating script where people can vote cars.
This script works with the Insert/Select/Union but I noticed that everything was getting really slow when people voted the images from the folder structure so I would like to move the selected images to another folder where they can be accessed faster, however I seem to get a problem when trying to bind the result. This is usually not a problem for me.
Could someone explain to me why this is happening in this particular case ?
Update:
I forgot to include the category variables SORRY they are in the real script just defined up the script... So why all the downvotes? I told you the SQL works- I just tried to improve it.
$cat1 = 1;
$cat2 = 2:
$cat3 = 3;
$stmt = $dbCon->prepare(" INSERT INTO cars_daily ( cars_daily_identifier, cars_daily_source, cars_daily_views, cars_daily_votes, cars_daily_rating, cars_daily_category) "
. " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_bcar "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
. " UNION "
. " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_car "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
. " UNION "
. " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_car "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) ");
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
if ($stmt->execute()) {
echo "<br><br>";
echo "Success SELECT/INSERT";
echo "<br><br>";
} else {
echo "<br><br>";
print_r($stmt->error);
echo "<br><br>";
echo "Fail SELECT/INSERT";
}
$stmt->bind_result($cars_car_id, $cars_cars_source, $cars_car_views, $cars_car_votes, $cars_car_rating, $cars_car_category);
while ($stmt->fetch()) {
copy("models/$cars_cars_source", "daily/$cars_cars_source");
}
The cause
An INSERT query won't yield records (like a SELECT would) even if the values to be inserted come from a SELECT statement.
A solution
One option is to run the INSERT query and then run a separate SELECT to fetch the values that were inserted.
$selectSQL = " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_bcar "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
. " UNION "
. " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_car "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
. " UNION "
. " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
. " FROM cars_car "
. " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) ";
//run the INSERT statement
$stmt = $dbCon->prepare(" INSERT INTO cars_daily ( cars_daily_identifier, cars_daily_source, cars_daily_views, cars_daily_votes, cars_daily_rating, cars_daily_category) " . $selectSQL);
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
if ($stmt->execute()) {
echo "<br><br>";
echo "Success SELECT/INSERT";
echo "<br><br>";
} else {
echo "<br><br>";
print_r($stmt->error);
echo "<br><br>";
echo "Fail SELECT/INSERT";
}
$stmt->close(); //close the prepared statement
//run the SELECT statement
$stmt = $dbCon->prepare($selectSQL);
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
$stmt->execute()
$stmt->bind_result($cars_car_id, $cars_cars_source, $cars_car_views, $cars_car_votes, $cars_car_rating, $cars_car_category);
.
I have the following script that retrieve numbers from 2 tables, make a sum, and the value is updated into a 3th table.
$query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) +
(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014') AS total";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result)){$valor_final = $row['total']; }
$query_update="UPDATE fusion_analysis SET jan='$valor_final' WHERE year_for_analysis='2014' AND item_for_analysis='general' AND category='general'";
$result = mysqli_query($mysqli,$query_update);
I need to run the same script for each month of the year. Everything is exaclty the same except the variable 'month' that changes from 1 to 12 and the SET in UPDATE where the value is uploaded for each month ('jan','feb', 'mar'...etc)
I'm currently just copying and pasting the same script changing this few parameters but I believe there is a smarter way to do this in less lines of code I have
See date function of PHP:
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='".date('n')."'"
." AND group_of_costos='general' AND year_analysis='".date("Y")."' ) +"
."(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='".date('n')."'"
. " AND group_of_costos='general' AND year_analysis='".date("Y")."') AS total";
$query_update="UPDATE fusion_analysis"
. " SET `". strtolower(date('M'))."`='$valor_final'"
. " WHERE year_for_analysis='".date("Y")."'"
. " AND item_for_analysis='general'"
. " AND category='general'";
NOTE:
Y - A full numeric representation of a year, 4 digits like 2014
n - Numeric representation of a month, without leading zeros 1 - 12
M - A short textual representation of a month, three letters Jan through Dec
For month as short textual I've used the strtolower function to make it lowercase.
UPDATE
Based on OP comment:
for ($i = 1; $i <= 12; $i++) {
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "' ) +"
. "(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "') AS total";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
$valor_final = $row['total'];
$monthName = strtolower(date('M', strtotime(date("Y") . "-" . str_pad($month,2, "0", STR_PAD_LEFT) . "-" . date("01") )));
$query_update = "UPDATE fusion_analysis"
. " SET `" . $monthName . "`=' " . $valor_final . "'"
. " WHERE year_for_analysis='" . date("Y") . "'"
. " AND item_for_analysis='general'"
. " AND category='general'";
mysqli_query($mysqli, $query_update);
}
i have something like this to insert data from a form to my MySQL table. is my use of select statements in the insert valid? please enlighten me.
if(isset($_POST['date']) && isset($_POST['docName']) && isset($_POST['docSpec']) && isset($_POST['time']) && isset($_POST['symptoms']) )
{
$nameOfUser = $_COOKIE['userlogin'];
$docName = $_POST['docName'];
$date = $_POST['date'];
$symptoms = $_POST['symptoms'];
$time = date('H:i:s',strtotime($_POST['time']));
$id = mt_rand(1000,9999); //generate random appointment id
$insertQuery = "insert into appointment values
($id,(select doctorid from doctors where doctorName like '$docName' ),
$date,$symptoms,
(select patientid from patient where patientFName like '$nameOfUser'), $time)";
if(mysqli_query($conn,$insertQuery)===true)
{
echo "<script>alert('success');</script>";
}
else
{
die('Invalid query: ' . mysql_error());
$message .= 'Whole query: ' . $query;
die($message);
}
}
it says invalid query. the columns in the insert statement is already in right order. can anyone help me?
You have to specify the columns that you are inserting into -
insert into appointment (col1, col2, col3, ...) values
($id,(select doctorid from doctors where doctorName like '$docName' ), $date,$symptoms,(select patientid from patient where patientFName like '$nameOfUser'),$time)";
It looks like you have 6 columns.
EDIT: This syntax may help to clear things up -
$insertQuery = "INSERT INTO `appointment` (`col1`, `col2`, `col3`,`col4`,`col5`,`col6`) ";
$insertQuery .= "VALUES (";
$insertQuery .= "'" . $id . "'";
$insertQuery .= ", '" . "(SELECT `doctorid` FROM `doctors` WHERE `doctorName` LIKE '%" . $docName . "%')" . "'";
$insertQuery .= ", '" . $date . "'";
$insertQuery .= ", '" . $symptoms . "'";
$insertQuery .= ", '" . "(SELECT `patientid` FROM `patient` WHERE `patientName` LIKE '%" . $nameOfUser . "%')" . "'";
$insertQuery .= ", '" . $time . "'";
$insertQuery .= ")";
You're also using LIKE without giving it the chance to find other elements because you're not using wildcards.
I have a working script that selects image fields in all tables and empty their values if the physical file doesnt exist.
$query1 = "SELECT table_name,column_name
FROM information_schema.columns
WHERE table_schema='schemaname' AND column_name like '%image%' or column_name='video'";
$result1 = mysql_query($query1) or die(mysql_error() . " -- " . $query1);
while($row1 = mysql_fetch_row($result1)){
if (!strpos($row1[0],'backup') > 0){
$sql = "Select COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME = '".$row1[0]."' AND EXTRA = 'auto_increment'";
$resultcol = mysql_query($sql);
$rowcol = mysql_fetch_row($resultcol);
$query2 = "SELECT " . $row1[1] . ", " .$rowcol[0] . "
FROM " . $row1[0] . "
WHERE " . $row1[1] . " != '' AND " . $row1[1] . " IS NOT NULL
";
echo $query2 . "<br>";
$result2 = mysql_query($query2) or die(mysql_error() . " -- " . $query2);
while ($rowdb = mysql_fetch_row($result2)){
if (!strpos($rowdb[0],'facebook') > 0 && !file_exists($img_root.'/'.$rowdb[0])){
$sql = "UPDATE ".$row1[0]." SET ". $row1[1] . " = '' WHERE " . $rowcol[0]. "= ".$rowdb[1];
echo $sql . "<br><br>";
$delete_count++;
//mysql_query("UPDATE ".$row1[0]." SET ". $row1[1] . " = '' WHERE id = ".$row1["id"]);
}
}
}
}
The script is working fine, but it takes time though, I was wondering if there is a smarter way (more optimized) to get the same function ? Thanks
You have several options.
The first, and IMHO the best option - is to use an ORM -
I recommend Idiorm, Doctrine, or Propel.
Then, you would use something like (in idiorm) fetch_all and loop through that, instead of through the mysql_fetch_row()
Second, you should switch to mysqli -- the functions you are using are deprecated in PHP5.5
Third -- you could just use either mysql_fetch_array or mysql_fetch_all (I'm not sure, but I would be on the latter)
The key thing here is:
Do not loop mysql functions.
Performance wise the problem is that you are looping through a result set, and performing queries for each row.
However with your output it is difficult to eliminate this. Otherwise you might be able to do the whole script in a single SQL statement.
Minimal clean up to just remove one of the selects:-
<?php
$query1 = "SELECT a.table_name, a.column_name, b.COLUMN_NAME AS auto_inc_col
FROM information_schema.columns a
INNER JOIN information_schema.columns b
ON a.table_name = b.table_name AND b.EXTRA = 'auto_increment'
WHERE table_schema='schemaname' AND column_name like '%image%' or column_name='video'";
$result1 = mysql_query($query1) or die(mysql_error() . " -- " . $query1);
while($row1 = mysql_fetch_assoc($result1))
{
if (!strpos($row1['table_name'],'backup') > 0)
{
$query2 = "SELECT " . $row1['column_name'] . ", " .$row1['auto_inc_col'] . "
FROM " . $row1['table_name'] . "
WHERE " . $row1['column_name'] . " != '' AND " . $row1['column_name'] . " IS NOT NULL
";
echo $query2 . "<br>";
$result2 = mysql_query($query2) or die(mysql_error() . " -- " . $query2);
while ($rowdb = mysql_fetch_row($result2))
{
if (!strpos($rowdb[0],'facebook') > 0 && !file_exists($img_root.'/'.$rowdb[0]))
{
$sql = "UPDATE ".$row1['table_name']." SET ". $row1['column_name'] . " = '' WHERE " . $row1['auto_inc_col']. "= ".$rowdb[1];
echo $sql . "<br><br>";
$delete_count++;
//mysql_query("UPDATE ".$row1['table_name']." SET ". $row1['column_name'] . " = '' WHERE id = ".$row1["id"]);
}
}
}
}
?>
When I tested this query out in mysql it was fine but when I went to run it in php I keep getting this error.
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 'SELECT *, (#rownum := #rownum + 1) AS rank FROM ( SELECT *, (totalWins+(total' at line 1
This is the php code I have.
<?php
$sql = " SET #rownum = 0; ";
$sql .= " SELECT *, (#rownum := #rownum + 1) AS rank FROM ( ";
$sql .= " SELECT *, (totalWins+(totalPushs*.5)) AS totalPoints, totalWins+totalLost+totalPushs AS totalBets FROM ( ";
$sql .= " SELECT *, SUM(win) AS totalWins, SUM(lost) AS totalLost, SUM(push) AS totalPushs FROM ( ";
$sql .= " SELECT *, (finalResult = 'Winner') AS win, (finalResult = 'Loser') AS lost, (finalResult = 'Push') AS push FROM ( ";
$sql .= " SELECT " . $db_prefix . "users.userID, userName, ";
$sql .= " IF (pickID=visitorID, visitorResult, homeResult) AS finalResult ";
$sql .= " FROM " . $db_prefix . "users ";
$sql .= " JOIN " . $db_prefix . "picks ";
$sql .= " ON " . $db_prefix . "users.userID = " . $db_prefix . "picks.userID ";
$sql .= " JOIN " . $db_prefix . "schedule ";
$sql .= " ON " . $db_prefix . "picks.gameID = " . $db_prefix . "schedule.gameID ";
$sql .= " ) x ";
$sql .= " ) x ";
$sql .= " GROUP BY userID ";
$sql .= " ) x ";
$sql .= " ) x ";
$sql .= " ORDER BY totalPoints DESC, totalWins DESC, totalPushs DESC, totalLost ";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $row[rank] . '|' . $row[userName]. '|' . $row[totalWins] . '|' . $row[totalLost] . '|' . $row[totalPushs] . '|' . $row[totalPoints];
echo '<br>';
}
?>
I can get the php code to work without the first line of code
$sql = " SET #rownum = 0; ";
but it won't echo out the rank column.
Is there something I have to do differently to line one of the code when it's in php?
mysql_query does not support running more than one query at a time. You must first run
mysql_query("SET #rownum = 0;");, then you can run the rest of your query in a second mysql_query call.
Please try tablename.* instead of *