title
PHP and MySQL with MySQLi: Getting results (Part 4/9)

description
Want more? Explore the library at https://www.codecourse.com/lessons Official site https://www.codecourse.com Twitter https://twitter.com/teamcodecourse

detail
{'title': 'PHP and MySQL with MySQLi: Getting results (Part 4/9)', 'heatmap': [{'end': 570.731, 'start': 534.039, 'weight': 0.886}, {'end': 643.391, 'start': 618.092, 'weight': 0.975}, {'end': 668.705, 'start': 643.951, 'weight': 0.741}], 'summary': 'Learn how to query a database using sql select statements, loop through results, and output data to the page, emphasizing sql select statements and methods to check the number of results. explore error reporting, result printing, and handling database query results, including techniques such as turning off error reporting, debugging silent errors, and using conditional statements, with examples and quantifiable outcomes. discover querying the database and looping through result sets using associative arrays, numeric arrays, and objects in php, including a demonstration of the usage of the free method to clear up memory associated with the result set.', 'chapters': [{'end': 152.731, 'segs': [{'end': 41.305, 'src': 'embed', 'start': 0.329, 'weight': 0, 'content': [{'end': 7.771, 'text': "So now that we've connected to our database, we're going to actually query the database to retrieve records of our choice.", 'start': 0.329, 'duration': 7.442}, {'end': 17.994, 'text': "Now. we're not going to get too far into SQL in this case, but we are going to do a couple of select statements or queries just to retrieve results.", 'start': 8.431, 'duration': 9.563}, {'end': 18.654, 'text': "that we're after.", 'start': 17.994, 'duration': 0.66}, {'end': 23.316, 'text': "What we're then going to do is check the number of results that we've been returned.", 'start': 19.474, 'duration': 3.842}, {'end': 28.258, 'text': 'So we can either output that number or just check that we have actually retrieved some results.', 'start': 23.396, 'duration': 4.862}, {'end': 33.181, 'text': "And then we're going to loop through the results and examine the results in certain ways.", 'start': 28.859, 'duration': 4.322}, {'end': 36.583, 'text': 'so, depending on how you need to use the results and output them to your page.', 'start': 33.181, 'duration': 3.402}, {'end': 41.305, 'text': "And then we'll obviously just output some data to the page as we go,", 'start': 37.203, 'duration': 4.102}], 'summary': 'Query database, retrieve and examine results, output data.', 'duration': 40.976, 'max_score': 0.329, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ329.jpg'}], 'start': 0.329, 'title': 'Querying database for results', 'summary': 'Explains how to query a database using sql select statements, loop through the results, and output the data to the page, while emphasizing the use of sql select statements and methods to check the number of results.', 'chapters': [{'end': 152.731, 'start': 0.329, 'title': 'Querying database for results', 'summary': 'Explains how to query a database to retrieve results using sql select statements, loop through the results, and output the data to the page, emphasizing the use of sql select statements and methods to check the number of results.', 'duration': 152.402, 'highlights': ['The chapter emphasizes the use of SQL select statements to retrieve records from the database, including examples of selecting everything from a table and limiting the results by specifying fields or using limit clauses.', 'The chapter explains the process of checking the number of results returned from the query using methods to output the number or to verify the retrieval of results.', 'The chapter discusses looping through the retrieved results and examining them in various ways, based on the requirements of using the results and outputting them to the page.']}], 'duration': 152.402, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ329.jpg', 'highlights': ['The chapter emphasizes the use of SQL select statements to retrieve records from the database, including examples of selecting everything from a table and limiting the results by specifying fields or using limit clauses.', 'The chapter discusses looping through the retrieved results and examining them in various ways, based on the requirements of using the results and outputting them to the page.', 'The chapter explains the process of checking the number of results returned from the query using methods to output the number or to verify the retrieval of results.']}, {'end': 464.846, 'segs': [{'end': 180.725, 'src': 'embed', 'start': 153.011, 'weight': 1, 'content': [{'end': 156.733, 'text': 'First is to turn off error reporting and just refresh the page.', 'start': 153.011, 'duration': 3.722}, {'end': 159.735, 'text': 'It looks like everything has successfully worked.', 'start': 156.894, 'duration': 2.841}, {'end': 163.417, 'text': "Let's go ahead and just modify this and see what happens.", 'start': 160.436, 'duration': 2.981}, {'end': 171.161, 'text': "Nothing So it looks as though this is sort of silently erroring, and we're not actually getting any error back.", 'start': 164.458, 'duration': 6.703}, {'end': 180.725, 'text': 'Now, the reason being for this is that when we actually retrieve our results, we can check things like the number of results, et cetera, et cetera.', 'start': 171.921, 'duration': 8.804}], 'summary': 'Successfully turned off error reporting, but silently facing issues with no error feedback.', 'duration': 27.714, 'max_score': 153.011, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ153011.jpg'}, {'end': 244.459, 'src': 'embed', 'start': 193.03, 'weight': 0, 'content': [{'end': 201.217, 'text': "So what we're going to do with printr is this will print the sort of structure and data structure, if you like, of what's returned here.", 'start': 193.03, 'duration': 8.187}, {'end': 205, 'text': "It won't be the result set, but you'll see what we get back here.", 'start': 201.517, 'duration': 3.483}, {'end': 208.323, 'text': "So let's just print our result.", 'start': 205.621, 'duration': 2.702}, {'end': 213.352, 'text': 'Ah, of course, from people.', 'start': 211.292, 'duration': 2.06}, {'end': 213.993, 'text': 'There we are.', 'start': 213.653, 'duration': 0.34}, {'end': 215.713, 'text': "So let's refresh.", 'start': 214.733, 'duration': 0.98}, {'end': 220.174, 'text': "So you can see here that we're returned with a MySQLi result object.", 'start': 215.733, 'duration': 4.441}, {'end': 228.696, 'text': "And it's got a few bits of information in here that we can sort of look at and work out that this has completed successfully.", 'start': 220.774, 'duration': 7.922}, {'end': 231.616, 'text': "We've got a field count here, which is five.", 'start': 229.076, 'duration': 2.54}, {'end': 233.497, 'text': 'We already know that there are five fields here.', 'start': 231.656, 'duration': 1.841}, {'end': 238.118, 'text': 'And we also have a numRow, so a number of rows that have been returned, which we know are two.', 'start': 233.917, 'duration': 4.201}, {'end': 244.459, 'text': 'So if we were to say something like limit one or limit one one,', 'start': 238.838, 'duration': 5.621}], 'summary': 'The transcript demonstrates printing structure and data from a mysqli result object, with a field count of 5 and a numrow of 2.', 'duration': 51.429, 'max_score': 193.03, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ193030.jpg'}, {'end': 317.097, 'src': 'embed', 'start': 270.983, 'weight': 5, 'content': [{'end': 272.606, 'text': 'So if we refresh, we get this.', 'start': 270.983, 'duration': 1.623}, {'end': 279.675, 'text': "Now let's go ahead and select stuff from users like I mistakenly did before.", 'start': 273.487, 'duration': 6.188}, {'end': 282.887, 'text': "and we don't get this appear.", 'start': 281.687, 'duration': 1.2}, {'end': 291.629, 'text': 'So we know now that we can check whether this query not was a success or not, but we know that we can get a number of results.', 'start': 282.927, 'duration': 8.702}, {'end': 297.19, 'text': 'And if we wanted to actually check if this query had failed or not, we can also do so.', 'start': 292.249, 'duration': 4.941}, {'end': 302.11, 'text': "And this is extremely important because it allows us to debug a query if it's failed.", 'start': 297.57, 'duration': 4.54}, {'end': 306.031, 'text': 'At the moment, if I was to type in select star from users,', 'start': 302.551, 'duration': 3.48}, {'end': 310.412, 'text': "we don't get an error back because we're not outputting specifically asking to output an error.", 'start': 306.031, 'duration': 4.381}, {'end': 313.434, 'text': 'Now, there are a couple of ways of doing this.', 'start': 311.312, 'duration': 2.122}, {'end': 317.097, 'text': 'We can either put or die on the end.', 'start': 314.234, 'duration': 2.863}], 'summary': 'Demonstrating how to check the success of a query and debug if failed, offering a couple of ways to handle errors.', 'duration': 46.114, 'max_score': 270.983, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ270983.jpg'}, {'end': 421.975, 'src': 'embed', 'start': 390.825, 'weight': 7, 'content': [{'end': 394.169, 'text': "You want to actually do something if this query isn't successful.", 'start': 390.825, 'duration': 3.344}, {'end': 399.397, 'text': 'So what I tend to do is if like this.', 'start': 394.89, 'duration': 4.507}, {'end': 407.644, 'text': "And what this is doing is it's encapsulating this assignment to this result variable, but also checking it at the same time.", 'start': 399.898, 'duration': 7.746}, {'end': 412.548, 'text': 'So what we can then do is we can copy and paste our if statement from here.', 'start': 408.064, 'duration': 4.484}, {'end': 413.669, 'text': 'Just indent that a little.', 'start': 412.668, 'duration': 1.001}, {'end': 421.975, 'text': "So what we're now doing is we're saying if this result was a success and by doing that we're already assigning it to result then we can check the result,", 'start': 414.169, 'duration': 7.806}], 'summary': 'Encapsulate query result assignment and check for success', 'duration': 31.15, 'max_score': 390.825, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ390825.jpg'}], 'start': 153.011, 'title': 'Handling query results', 'summary': 'Discusses error reporting, result printing, and handling database query results, including techniques such as turning off error reporting, debugging silent errors, using the printr technique, and structuring conditional statements, with examples and quantifiable outcomes.', 'chapters': [{'end': 220.174, 'start': 153.011, 'title': 'Error reporting and result printing', 'summary': 'Discusses turning off error reporting, debugging a silent error, and using the printr technique to print the data structure of a result set, which returned a mysqli result object.', 'duration': 67.163, 'highlights': ['The chapter discusses using the printr technique to print the data structure of a result set, which returned a MySQLi result object.', 'The chapter explores debugging a silent error and the reason for not getting any error back when retrieving results.', 'The chapter mentions the need to turn off error reporting and the successful execution of a task.']}, {'end': 464.846, 'start': 220.774, 'title': 'Handling database query results', 'summary': 'Discusses handling of database query results, including checking for successful completion, retrieving row and field counts, debugging query errors, and structuring conditional statements to handle success and failure, with examples and quantifiable outcomes.', 'duration': 244.072, 'highlights': ['The chapter introduces the concept of handling database query results, including checking for successful completion, retrieving row and field counts, and debugging query errors.', 'The speaker demonstrates how to use field count and numRow to verify the number of fields and rows returned from a query, with an example showing a field count of five and a numRow of two.', 'The importance of checking for successful query completion is emphasized, with the demonstration of using if statements to handle positive number results and debug query failures.', "The process of debugging query errors is explained, including using 'or die' to output a database error and making the error message readable and user-friendly for easier debugging.", 'A preferred method for handling query success and failure is presented, utilizing encapsulation and conditional statements to execute actions based on the query result, with an example showcasing successful and failed query outcomes.']}], 'duration': 311.835, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ153011.jpg', 'highlights': ['The chapter discusses using the printr technique to print the data structure of a result set, which returned a MySQLi result object.', 'The chapter explores debugging a silent error and the reason for not getting any error back when retrieving results.', 'The chapter mentions the need to turn off error reporting and the successful execution of a task.', 'The chapter introduces the concept of handling database query results, including checking for successful completion, retrieving row and field counts, and debugging query errors.', 'The speaker demonstrates how to use field count and numRow to verify the number of fields and rows returned from a query, with an example showing a field count of five and a numRow of two.', 'The importance of checking for successful query completion is emphasized, with the demonstration of using if statements to handle positive number results and debug query failures.', "The process of debugging query errors is explained, including using 'or die' to output a database error and making the error message readable and user-friendly for easier debugging.", 'A preferred method for handling query success and failure is presented, utilizing encapsulation and conditional statements to execute actions based on the query result, with an example showcasing successful and failed query outcomes.']}, {'end': 968.232, 'segs': [{'end': 489.044, 'src': 'embed', 'start': 465.627, 'weight': 1, 'content': [{'end': 478.257, 'text': "So, now that we've done this and we've determined that the query was a success and we've retrieved a certain amount of rows so i.e. a positive number of rows we can then start to actually loop through our data.", 'start': 465.627, 'duration': 12.63}, {'end': 489.044, 'text': "So how do we go ahead grabbing our data? There's a few methods that are available to us from this mysqli result object that's returned.", 'start': 478.837, 'duration': 10.207}], 'summary': 'Successful query retrieval of certain number of rows, now ready to loop through data.', 'duration': 23.417, 'max_score': 465.627, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ465627.jpg'}, {'end': 570.731, 'src': 'heatmap', 'start': 517.837, 'weight': 3, 'content': [{'end': 521.938, 'text': "So basically, when we return this, in fact, let's just have a look.", 'start': 517.837, 'duration': 4.101}, {'end': 522.86, 'text': 'It makes a lot more sense.', 'start': 521.999, 'duration': 0.861}, {'end': 530.864, 'text': 'So a numeric array will be 0 indexed and be 0, 1, 2, 3, and have the values bind to them numeric keys.', 'start': 523.94, 'duration': 6.924}, {'end': 538.463, 'text': 'But this will be associative, so it will have names, basically, or strings in place.', 'start': 534.039, 'duration': 4.424}, {'end': 541.265, 'text': "So you can see here that we've got an array.", 'start': 539.364, 'duration': 1.901}, {'end': 544.048, 'text': "We've got ID, first name, last name.", 'start': 542.146, 'duration': 1.902}, {'end': 546.33, 'text': 'So this is the associative array with the data.', 'start': 544.068, 'duration': 2.262}, {'end': 553.356, 'text': "Now let's just go ahead and output this a little more friendly way.", 'start': 546.97, 'duration': 6.386}, {'end': 556.198, 'text': "And that's just wrapping it in pre-formatted tags.", 'start': 553.716, 'duration': 2.482}, {'end': 559.881, 'text': 'And this will basically give us a better view of this.', 'start': 557.199, 'duration': 2.682}, {'end': 570.731, 'text': "So you'll probably notice that although we're returning two results and we can prove that by echoing results.", 'start': 560.962, 'duration': 9.769}], 'summary': 'The transcript explains the difference between numeric and associative arrays, and demonstrates how to output and format an associative array in php.', 'duration': 26.211, 'max_score': 517.837, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ517837.jpg'}, {'end': 649.633, 'src': 'heatmap', 'start': 618.092, 'weight': 0.975, 'content': [{'end': 621.255, 'text': "And because there's a pointer that relates to each records row.", 'start': 618.092, 'duration': 3.163}, {'end': 625.358, 'text': 'Now what we could do is we could say fetch all instead.', 'start': 621.835, 'duration': 3.523}, {'end': 627.899, 'text': "So now let's have a look at what that outputs.", 'start': 625.878, 'duration': 2.021}, {'end': 631.782, 'text': "You can now see that we've got all of the results that have been returned.", 'start': 628.98, 'duration': 2.802}, {'end': 638.087, 'text': 'So we now can see that we can work with this in terms of the two results that have been returned.', 'start': 632.183, 'duration': 5.904}, {'end': 643.391, 'text': "However, you'll also notice that we're now not dealing with an associative array.", 'start': 639.13, 'duration': 4.261}, {'end': 649.633, 'text': 'But what we can do is we can pass through a constant that defines what we are returned with.', 'start': 643.951, 'duration': 5.682}], 'summary': 'Fetching all records returns all results, working with two results, and using a constant to define the output.', 'duration': 31.541, 'max_score': 618.092, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ618092.jpg'}, {'end': 668.705, 'src': 'heatmap', 'start': 643.951, 'weight': 0.741, 'content': [{'end': 649.633, 'text': 'But what we can do is we can pass through a constant that defines what we are returned with.', 'start': 643.951, 'duration': 5.682}, {'end': 655.295, 'text': 'So this is going to be mysqlinum, which is the default.', 'start': 650.293, 'duration': 5.002}, {'end': 659.036, 'text': 'So if we leave that as it is and refresh, it stays the same.', 'start': 655.435, 'duration': 3.601}, {'end': 664.381, 'text': 'What we can also pass is assoc for an associative array.', 'start': 660.097, 'duration': 4.284}, {'end': 666.923, 'text': 'And that will then change this to an associative array.', 'start': 664.801, 'duration': 2.122}, {'end': 668.705, 'text': 'So we can actually now work with this data.', 'start': 666.943, 'duration': 1.762}], 'summary': 'Passing constant mysqlinum returns default, assoc returns associative array.', 'duration': 24.754, 'max_score': 643.951, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ643951.jpg'}, {'end': 923.226, 'src': 'embed', 'start': 896.148, 'weight': 2, 'content': [{'end': 901.792, 'text': "But what we've done here is we've achieved looping through result set and outputting what we need.", 'start': 896.148, 'duration': 5.644}, {'end': 906.134, 'text': "Now there's a method called free in MySQL I,", 'start': 902.512, 'duration': 3.622}, {'end': 912.419, 'text': "which basically frees up memory associated with the result set that you've returned back from your MySQL database.", 'start': 906.134, 'duration': 6.285}, {'end': 916.041, 'text': 'And we want to free this up so we sort of clear up after ourselves.', 'start': 913.019, 'duration': 3.022}, {'end': 923.226, 'text': "The reason being is that now that we've stored the result, we no longer need that result set in memory.", 'start': 916.421, 'duration': 6.805}], 'summary': 'Achieved looping through result set and freeing up memory in mysql.', 'duration': 27.078, 'max_score': 896.148, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ896148.jpg'}, {'end': 968.232, 'src': 'embed', 'start': 941.217, 'weight': 0, 'content': [{'end': 949.322, 'text': "We've looked at counting the result set, checking that the result, or first of all, checking that the query was successful.", 'start': 941.217, 'duration': 8.105}, {'end': 951.984, 'text': "We've managed to output how many results have been returned.", 'start': 949.522, 'duration': 2.462}, {'end': 953.265, 'text': "We've looped through them.", 'start': 952.344, 'duration': 0.921}, {'end': 954.846, 'text': "We've worked with associative arrays.", 'start': 953.285, 'duration': 1.561}, {'end': 956.187, 'text': "We've looked at numeric arrays.", 'start': 954.886, 'duration': 1.301}, {'end': 961.669, 'text': "And we're now working with objects, which is personal preference myself.", 'start': 956.787, 'duration': 4.882}, {'end': 968.232, 'text': "And then we've cleared up after ourselves by using MySQL, the free method on the MySQL result object.", 'start': 962.189, 'duration': 6.043}], 'summary': 'Covered result set counting, looping, associative arrays, numeric arrays, and object manipulation using mysql.', 'duration': 27.015, 'max_score': 941.217, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ941217.jpg'}], 'start': 465.627, 'title': 'Querying and looping through mysql results', 'summary': 'Covers querying the database and looping through result sets using associative arrays, numeric arrays, and objects in php, including a demonstration of the usage of the free method to clear up memory associated with the result set.', 'chapters': [{'end': 690.539, 'start': 465.627, 'title': 'Working with mysqli result object', 'summary': 'Covers looping through data retrieved from a successful mysql query, including methods for fetching results, handling associative arrays, and manipulating result data, with a demonstration of outputting and manipulating the retrieved data.', 'duration': 224.912, 'highlights': ['Looping through retrieved data using fetch methods The chapter explains looping through the data retrieved from a successful MySQL query using fetch methods, demonstrating the use of fetch_assoc and fetch_all methods.', 'Understanding associative arrays and numeric arrays The chapter describes the difference between associative arrays and numeric arrays, with an explanation of how keys are associative to the data in an associative array.', 'Outputting and manipulating retrieved data The chapter demonstrates outputting the retrieved data in a more user-friendly way by wrapping it in pre-formatted tags and discusses the use of constants like MYSQLI_ASSOC for defining the type of data returned.']}, {'end': 968.232, 'start': 691.519, 'title': 'Querying and looping through mysql results', 'summary': 'Covers querying the database, looping through result sets using associative arrays, numeric arrays, and objects in php. it also explains the usage of the free method to clear up memory associated with the result set.', 'duration': 276.713, 'highlights': ['The chapter covers querying the database, looping through result sets using associative arrays, numeric arrays, and objects in PHP. It explains the process of querying the database, looping through result sets using associative arrays, numeric arrays, and objects in PHP, providing a comprehensive overview of working with MySQL results.', 'It also explains the usage of the free method to clear up memory associated with the result set. The chapter elaborates on the utilization of the free method in MySQL to release memory associated with the result set, emphasizing the importance of clearing up memory after querying the database.']}], 'duration': 502.605, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/HQOS3V9nyEQ/pics/HQOS3V9nyEQ465627.jpg', 'highlights': ['Covers querying the database, looping through result sets using associative arrays, numeric arrays, and objects in PHP.', 'The chapter explains looping through the data retrieved from a successful MySQL query using fetch methods, demonstrating the use of fetch_assoc and fetch_all methods.', 'The chapter elaborates on the utilization of the free method in MySQL to release memory associated with the result set, emphasizing the importance of clearing up memory after querying the database.', 'The chapter describes the difference between associative arrays and numeric arrays, with an explanation of how keys are associative to the data in an associative array.']}], 'highlights': ['The chapter emphasizes the use of SQL select statements to retrieve records from the database, including examples of selecting everything from a table and limiting the results by specifying fields or using limit clauses.', 'The chapter discusses using the printr technique to print the data structure of a result set, which returned a MySQLi result object.', 'Covers querying the database, looping through result sets using associative arrays, numeric arrays, and objects in PHP.', 'The chapter explains looping through the data retrieved from a successful MySQL query using fetch methods, demonstrating the use of fetch_assoc and fetch_all methods.', 'The chapter elaborates on the utilization of the free method in MySQL to release memory associated with the result set, emphasizing the importance of clearing up memory after querying the database.']}