I have a php script - see below.
And a mysql database with a table with one row in it with 4 fields.
Database = ftest
table = ftable1
urn f1 f2 f3
1 new F1 this is f2 this is f3
if I execute this query
update ftable1 SET f1= ‘new F1b’ WHERE urn = ‘2’
then it should fail because there is no row with urn=2
It does fail in that no line is added and the only existing row where urn = 1 is not changed but the
if ($result==FALSE) statement is never executed
<?php
// Remember use searh [^\x00-\x7F]+ and replace with spaces
// Key things to note
// in MYSQLI when doing a query, one has to include the connection
// in MYSQL if you only had one connection then the connection was implied and not specifically required.
//
//https://prittytimes.com/difference-between-mysql-vs-mysqli-in-php/
//
// mysqli Function mysql Function
// mysqli_real_escape_string($conn,$data) mysql_real_escape_string($data)
// mysqli_errno($con) mysql_errno() or mysql_errno($cxn)
//* mysqli_select_db($con,$dbname) mysql_select_db($dbname)
// mysqli_error($con) mysql_error() or mysql_error($con)
//* mysqli_query($con,$sql) mysql_query($sql) or mysql_query($sql,$con)
// mysqli_fetch_array($result) mysql_fetch_array($result)
// mysqli_num_rows($result) mysql_num_rows($result)
// mysqli_fetch_assoc($result) mysql_fetch_assoc($result)
// mysqli_fetch_row($result) mysql_fetch_row($result)
// mysqli_insert_id($con) mysql_insert_id($con)
//
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
echo "Hello";
$link = db_connect();
$row = get_row_from_table($link, 'ftable1', 'urn', '1');
echo "\n<BR>Before";
print_array_recursive($row);
$var_array = ['f1' => 'new F1b'];
update_table($link, 'ftable1', 'urn', '2',$var_array);
echo "\n<BR>After";
$row = get_row_from_table($link, 'ftable1', 'urn', '2');
print_array_recursive($row);
function update_table($link, $table_name,$ufn,$ud,$var_array)
{
// This is a generalised update table function
// The name of the unique column is contained in the variable $ufn.
// so normally $ufn="urn" and
// $ud is the unique record data and so normally $ud=$urn
// $var_array is an associative array of key value pairs where
// the keys are the field names and the values is the data to be stored.
// 1 GET EXISTING DATA
$row = get_row_from_table($link, $table_name, $ufn, $ud);
// 2 BUILD THE QUERY
$query = "update $table_name SET"; // first bit of the query string
foreach($var_array as $key => $value)
{
$query = $query." ".$key."= '".$value."'";
};
$query = $query." WHERE $ufn = '$ud'";
echo $query;
$result = mysqli_query($link, $query);
// https://www.php.net/manual/en/mysqli.query.php
// Returns false on failure.
// For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_query()
// will return a mysqli_result object.
// For other successful queries, mysqli_query() will return true.
if ($result==FALSE)
{
echo "<br>query did not happen result is false<br>";
echo "<BR>query_update= $query_update";
echo "<BR>result_update= $result_update";
echo "<BR>mysql error no= ".mysqli_errno().": ".mysqli_error()."<BR>";
};
};
function get_row_from_table($link, $table_name, $ufn, $ud)
{
// This function searches $table_name
// The where clause is "WHERE $ufn = '$ud'";
// The row is returned as an associative array of fieldname => data
$query = "SELECT *
FROM $table_name
WHERE $ufn = '$ud'
";
$result = mysqli_query($link, $query);
// Returns false on failure.
// For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_query()
// will return a mysqli_result object.
// For other successful queries, mysqli_query() will return true.
if ($result==FALSE)
{
echo "<br>query did not happen result is false<br>";
echo "<BR>query_update= $query_update";
echo "<BR>result_update= $result_update";
echo "<BR>mysql error no= ".mysqli_errno().": ".mysqli_error()."<BR>";
};
$row = mysqli_fetch_assoc($result);
return $row;
};
function db_connect()
{
//https://www.php.net/manual/en/mysqli.construct.php
/* You should enable error reporting for mysqli before attempting to make a connection */
//mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('localhost', 'root', '', 'ftest');
if (mysqli_connect_errno()) {
throw new RuntimeException('mysqli connection error: ' . mysqli_connect_error());
}
/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli, 'utf8mb4');
if (mysqli_errno($mysqli)) {
throw new RuntimeException('mysqli error: ' . mysqli_error($mysqli));
}
return $mysqli;
};
function print_array_recursive($myarray)
{
if (is_array($myarray))
{
echo "<TABLE border=1>";
foreach($myarray as $key => $value)
{
echo "\n<TR>";
echo "<TD> $key </TD><TD>";
if (is_array($value))
{
echo "<B>Array</B>";
print_array_recursive($value); // call recursively
}
else
{
echo "$value";
};
echo "</TD>";
echo "\n</TR>";
};
echo "</TABLE>";
}
else
{
echo "$myarray is not an array $myarray";
};
};
?>