You should be able to set those two settings in your code, unless the web host has disabled the ability to do so.
You should be learning, developing, and debugging code/queries on a localhost development system. Constantly uploading code to web hosting to see the result of each change is a huge waste of time and if an error occurs during the upload, you can end up running the previous code, not the latest.
Here is typical PDO connection code -
$DB_HOST = ''; // database host name or ip address
$DB_USER = ''; // database username
$DB_PASS = ''; // database password
$DB_NAME = ''; // database name
$DB_ENCODING = 'utf8mb4'; // db character encoding. set to match your database table's character set. note: utf8 is an alias of utf8mb3/utf8mb4
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions. this is the default now in php8+
PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // set default fetch mode to assoc
];
$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);
Converting an old query that has variables being put directly into it into a prepared query is fairly straightforward -
- Remove, and keep for later, any php variables that are inside the sql query statement. note: any wild-card characters in a LIKE comparison are supplied as part of the data value not as part of the sql query statement.
- Remove any quotes or {} that were around the php variable and any concatenation dots/extra quotes that were used to get the php variable into the sql query statement.
- Put a simple ? prepared query place-holder into the sql query statement for each value.
- Call the PDO prepare() method for the sql query statement. This returns a PDOStatement object.
- Call the PDOStatement execute([…]) method with an array of the variables you removed in step #1.
For a query that returns a result set, fetch the data from the query. See the PDOStatement fetch() method when fetching a single row of data, the PDOStatement fetchAll() method when fetching all the rows of data at once, and occasionally the PDOStatement fetchColum() method when fetching a single column from a single row of data. Forget about any num rows function/method/property. Just fetch then test if/how many rows of data there are.
For a query that doesn’t return a result set, you can use the PDO lastInsertId() method and the PDOStatement rowCount() method with an insert/update/delete query to get the last insert id and the number of affected rows.
Php8+ uses exceptions for database statement errors by default. For an insert/update query that could result in duplicate data (the appropriate column(s) need to be defined as unique indexes in the database table), you will need to catch and handle database exceptions from the query, test if the error number is for a duplicate index error, and setup a message for the user letting them know what was wrong with the data that they submitted. For all other query errors, simply rethrow the exception and let php handle it, and for all other type of queries, simply do nothing in your code, and let php catch and handle any database exception, where php will use its error related settings to control what happens with the actual error information via an uncaught exception error (database errors will ‘automatically’ get displayed/logged the same as php errors.)
For the two queries you have shown, they would look like -
$sql="SELECT * FROM qs WHERE ID=?";
$stmt = $pdo-prepare($sql);
$stmt->execute([ $quoteID ]);
$sql="INSERT INTO qs (sourceName) VALUES (?)";
$stmt->prepare($sql);
$stmt->execute([ $sourceName ]);