1

Whenever i need something from a database, lets say when a user login, and i need to verify & get the userdata, i do it this way:

  1. Check if user exists with num_rows
  2. Get userdata with fetch_array

So my questions are the following:

  1. I'm just thinking, isn't there an easier way to do all this. Like just use fetch_array and if it doesn't exist, it will return false or whatever it does.
  2. If there is a 1-statement way to do this, what do you need num_rows for?
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
131
  • 1,363
  • 1
  • 12
  • 32
  • 1
    I've added the PHP tag because this doesn't look like a MySQL question to me. – Tim Biegeleisen Apr 03 '17 at 05:57
  • don't you use num_rows and fetch_array in any programming language with mysql? – 131 Apr 03 '17 at 05:58
  • Whenever you define a standard function what it returns are fixed `num_rows` returns int `fetch_array` returns array .. So lets assume if there no data exist then `fetch_array` returns false (boolean). In standard programming this is not good. So fetch_array now has two return types boolean and array. Not good – M A SIDDIQUI Apr 03 '17 at 06:08
  • Why wouldn't that be good? Then i know the row doesn't exist, and i save 1 call to the database. Cause then i can check if fetch_array is true or not, and if it is, it means the row exists, if not, it doesn't exist – 131 Apr 03 '17 at 06:10
  • If you are going to use your own procedures to fetch data, I'd suggest you forget about mysqli adaptor and switch to PDO. Plus it is being layed off the PHP project. Deprecated in 7.0... – Ruby Racer Apr 03 '17 at 06:10
  • Is mysqli deprecated in PHP 7.0??? – 131 Apr 03 '17 at 06:11
  • 1
    @RubyRacer Where have you seen a `mysqli` deprecation notice? Per the manual, http://php.net/manual/en/mysqlinfo.api.choosing.php, that is incorrect information. – chris85 Apr 03 '17 at 06:12
  • 1
    Sorry, mysql, not mysqli.. my bad... Wrong input – Ruby Racer Apr 03 '17 at 06:17
  • 1
    You do not need *num_rows*, your assertions are totally correct. – Jay Blanchard Apr 03 '17 at 12:43

2 Answers2

1

I know it's not perfect answer but i usually do this

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$num_rows = mysql_num_rows(mysql_query("SELECT * FROM table1", $link));
echo "$num_rows Rows\n";
chris85
  • 23,846
  • 7
  • 34
  • 51
Vivek Maru
  • 8,347
  • 1
  • 24
  • 34
  • 2
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 03 '17 at 12:37
0

You posted 2 questions, and for me they are related. The answers however seem to be indirect as an answer goes, so maybe a brief discussion?

I like to use class properties and methods whenever possible (read: I understand them), not only for convenience sake, but also for stability and life-time sake. There is 1) a level of trust that it has been tested and 2) that it will remain at least until somebody thoroughly tests it. Additionally, should the property or method ever change, a Find...Replace usually clears it right up.

Past this, consider what I do:

(object)$db=new mysqli(HOST_DEF, USER_DEF, PASS_DEF, DB_DEF);
if($db->connect_errno || $db->errno) {
    //Do error stuff here
}
(string)$sql = sprintf("SELECT * FROM Table WHERE User='%s'",
                       filter_input(INPUT_POST, '$UserStringPassedInMaybe'));
(object)$result=$db->query($sql);
if(!$result || $result->num_rows()===0) {
    @$result->free();
    $db->close();
    unset($sql);
    //Handle that stuff here
}

You want to trap (quantify) the recordset somehow, and you can kind of compact how you do it. However, if all you're wanting to do is test the presence of a recordset, and you do it enough, then you can save a few lines of code by farming it out to function that returns a boolean, but you're still evaluating that return, when you could just evaluate $result itself as a boolean (as long as it's empty). Kind of dirty since it either evaluates as a boolean false, or an array. In this sense, no, you do not need num_rows.

bt3of4
  • 36
  • 6