From the course: PHP: Accessing Databases with PDO and MySQLi
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Submitting a SELECT query and getting the number of results - PHP Tutorial
From the course: PHP: Accessing Databases with PDO and MySQLi
Submitting a SELECT query and getting the number of results
MySQL Improved uses a single method called query to submit SQL statements to the database. When used with a select query, it returns a MySQLi result object. As well as containing the result set, the result object has a property indicating how many rows it contains. This is MySQLi_num_rows.php, which you can find in the chapter five 05_03 folder of the exercise files. The try block at the top of the page includes the connection script and then runs a very simple SQL query, a select query. The body of the page contains a table where the results of the query will eventually be displayed. So let's submit the query. To do that, we simply need to call the query method and assign the result to an object. So we'll call the result object result equals db our connection, and then we just use the query method and pass it our SQL, our select query. To find out how many rows the result set contains, we can access the num_rows property of the result object. So let's insert a new line just before…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Connecting to a database with MySQLi5m 57s
-
Setting the character set1m 57s
-
Submitting a SELECT query and getting the number of results4m 4s
-
Fetching the result7m 35s
-
Rewinding the result for reuse3m 20s
-
Handling non-SELECT queries5m 27s
-
Getting error messages5m 47s
-
Sanitizing user input with real_escape_string()4m 7s
-
-
-
-