gordonisnz Posted June 10, 2023 Share Posted June 10, 2023 (edited) 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 June 10, 2023 by gordonisnz Updated WHILE loop Quote Link to comment https://forums.phpfreaks.com/topic/316960-mysqli-object-orientated-procedural-tutorials/ Share on other sites More sharing options...
gordonisnz Posted June 10, 2023 Author Share Posted June 10, 2023 mysqli_fetch_assoc($result)) << error line Quote Link to comment https://forums.phpfreaks.com/topic/316960-mysqli-object-orientated-procedural-tutorials/#findComment-1609524 Share on other sites More sharing options...
Solution Barand Posted June 10, 2023 Solution Share Posted June 10, 2023 Your query failed, so $result contains false and not a result object. Put this code before your call to nysql_connect... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); ... it won't stop it failing but it will tell you why. Quote Link to comment https://forums.phpfreaks.com/topic/316960-mysqli-object-orientated-procedural-tutorials/#findComment-1609526 Share on other sites More sharing options...
gordonisnz Posted June 10, 2023 Author Share Posted June 10, 2023 Twas the name of my table - now fixed Quote Link to comment https://forums.phpfreaks.com/topic/316960-mysqli-object-orientated-procedural-tutorials/#findComment-1609527 Share on other sites More sharing options...
mac_gyver Posted June 10, 2023 Share Posted June 10, 2023 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. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/316960-mysqli-object-orientated-procedural-tutorials/#findComment-1609528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.