Jump to content

MYSQLI Object orientated & procedural tutorials


gordonisnz
Go to solution Solved by Barand,

Recommended Posts

Hello. 

Are there any GOOD tutorials about MYSQLI  Object orientated coding, and procedural.

IE - ONE PAGE that has procedural instructions and DOESNT EVEN BREATHE a sign of object-orientated, and the swapped version.

I prefer Procedural.

I've found tutorials that have both codes in one page - very confusing.. You use 1 line of procedural, and another line of object orientated & of course its causes errors. I want seperate pages/information.

I've been coding for years and now want a new thing added to my site. So I COPY the exact WORKING code i have on a separate script to this new script, and now that same code doesn't work.

my current code is here:-

$link = mysqli_connect('SITE.com', 'blah', 'Blahpasswrd', 'moreblah');
if ($link->connect_errno) {
   $GLOBALS["jobs"].="Failed to connect to MySQL: " . $link=>connect_error;
   save_logs("XXXXXXXXXV06_JOBSCHECK",$GLOBALS["jobs"]);
    exit();
}
    $sql = "SELECT * FROM `jobs` WHERE `ID` LIKE '$job%';";
$GLOBALS["jobs"].="\n$sql\n\n";$result = $link->query($sql);

$result = $link->query($sql);
  while($row = mysqli_fetch_assoc($result)) {
      $text.=print_r($row,true)."\n";
}

the error message im getting is:-

[10-Jun-2023 23:30:43 Pacific/Auckland]  PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in  PATH.php on line 72
[10-Jun-2023 23:35:03 Pacific/Auckland] PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in PATH.php on line 72

all i want is a list of each line/row of the database that matches. 

Edited by gordonisnz
Updated WHILE loop
Link to comment
Share on other sites

don't bother with the mysqli extension. it is overly complicated, inconsistent, and in the case of procedural vs OOP statements, has different error responses for the same root problem. instead, use the much simpler, consistent, and more modern PDO extension.

in php8+, both the mysqli and PDO extensions use exceptions for errors by default. the line of code that @Barand posted enables exceptions for errors for the msyqli extension. when you use exceptions for database statement errors, php will 'automatically' display or log the raw database errors, via an uncaught exception error. therefore, you can remove any existing error handling logic, since it won't ever get executed upon an error, simplifying the code.

you should also name the connection variable as to the type of connection it contains, $mysqli/$pdo. this will let anyone looking at the code know what extension it is using, and also let you search for which code has been converted and which hasn't.

you also need to use a prepared query when supplying external, unknown, dynamic values to a query when it gets executed, so that any sql special characters in a value cannot break the sql query syntax, which is how sql injection is accomplished. if you switch to the much simpler PDO extension, after you prepared the query, you can simply supply an array of the input values to the ->execute([...]) call.

  • Thanks 1
  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.