Update one table from another, uses an implicit join
UPDATE table1 AS A, table2 AS B
SET A.fieldx = B.fieldz
WHERE A.field7 = B.field3 and A.field2 = 'not me' -- etc...
Make the first character of the value of a field upper-case
UPDATE Valor.Table SET `size` = CONCAT(UCASE(LEFT(`size`, 1)), SUBSTRING(`size`, 2));
PHP, update a table. No return values.
$updatequery = "[DELETE FROM | INSERT INTO | UPDATE] Valor.Table [fieldx fieldz VALUES (?, ?) | SET fieldx = ?] WHERE field1 = ?;";
$stmt_update = $conn->prepare($updatequery);
$stmt_update->bind_param("s[sss]", $arg1[, $var2, $var3, $var4]);
--------------------
$stmt_update->execute();
$stmt_update->close();
function modifyDB1($query, $arg1){
// $query = "[DELETE FROM | INSERT INTO | UPDATE] Valor.Table [fieldx fieldz VALUES (?, ?) | SET fieldx = ?] WHERE field1 = ?;";
// - OR -
// $query = "CALL Valor.SP(?);"
global $conn;
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $arg1);
$stmt->execute();
}
function getValueFromDB1($query, $arg1){
// $query = "SELECT field1 FROM Valor.Table WHERE field2 = ? LIMIT 1;" || "CALL Valor.SP(?);"
global $conn;
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $arg1);
$stmt->execute();
return $stmt->get_result()->fetch_all()[0][0];
}
TENTATIVE:
function getResultSet1($query, $arg1){
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $arg1);
$stmt->execute();
$currresult = $stmt->get_result(); // i.e. the current (first) result set we are on in $stmt
return $currresult->fetch_all(); // fetch all rows
// These are now not necessary, because done by object destruction when function exits
// $currresult->free();
// $stmt->next_result(); // Will get an error on the next call if we don't advance to the final result.
// $stmt->close();
}
$fetchedset = getResultSet($query, $arg1);
foreach($fetchedset as $row){
$val1 = $row[0];
$val2 = $row[1];
}
PHP, prepared query calling a stored procedure. Simplist case: One row with one field is returned.
Stored procedures always return at least two sets:
$sp_getvalue = $conn->prepare("CALL Valor.GetValue(?);");
$sp_getvalue->bind_param("s", $arg1);
--------------------
$sp_getvalue->execute();
$currresult = $sp_getvalue->get_result(); // i.e. the current (first) result set we are on in $sp_getvalue
$fetchedset = $currresult->fetch_all();
$value = $fetchedset[0][0]; // "ASSOCIATIVE ARRAY" MY ASS
$currresult->free();
$sp_getvalue->next_result(); // Will get an error on the next call if we don't advance to the final result.
--------------------
$sp_getvalue->close();
PHP, prepared query calling a stored procedure which returns multiple rows.
Stored procedures always return at least two sets:
$sp_getresults = $conn->prepare("CALL Valor.SP(?);");
$sp_getresults->bind_param("s", $arg1);
--------------------
$sp_getresults->execute();
$currresult = $sp_getresults->get_result(); // i.e. the current (first) result set we are on in $sp_getresults
$fetchedset = $currresult->fetch_all(); // fetch all rows
// can now free some resources
$currresult->free();
$sp_getresults->next_result(); // Will get an error on the next call if we don't advance to the final result.
foreach($fetchedset as $row){
$val1 = $row[0];
}
--------------------
$sp_getresults->close();
TENTATIVE:
PHP, a FUNCTION for prepared query calling a stored procedure which returns multiple rows.
Stored procedures always return at least two sets:
function runstoredprocedure($arg1){
$sp_getresults = $conn->prepare("CALL Valor.SP(?);");
$sp_getresults->bind_param("s", $arg1);
$sp_getresults->execute();
$currresult = $sp_getresults->get_result(); // i.e. the current (first) result set we are on in $sp_getresults
return $currresult->fetch_all(); // fetch all rows
// These are now not necessary, because done by object destruction when function exits
// $currresult->free();
// $sp_getresults->next_result(); // Will get an error on the next call if we don't advance to the final result.
// $sp_getresults->close();
}
$fetchedset = runstoredprocedure($arg1);
foreach($fetchedset as $row){
$val1 = $row[0];
}
PHP, get a count from a table. Uses bind_result, which I don't use anywhere else. Should I start using this?
$query = "SELECT COUNT(*) FROM Valor.Table WHERE field1 = ? AND field2 = ?;";
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $arg1, $var2);
--------------------
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->close();