PHP/MySQL Database/mysql num rows
Содержание
Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows()
<source lang="html4strict">
<html> <head> <title>Using mysql_num_rows()</title> </head> <body> <?php $user = "root"; $pass = ""; $db = "mydatabase"; $link = mysql_connect( "localhost", $user, $pass ); if ( ! $link ) {
die( "Couldn"t connect to MySQL: ".mysql_error() );
} mysql_select_db( $db, $link ) or die ( "Couldn"t open $db: ".mysql_error() ); $result = mysql_query( "SELECT * FROM domains where sex="F"" ); $num_rows = mysql_num_rows( $result );
print "$num_rows women have added data to the table
\n";mysql_close( $link ); ?> </body> </html>
</source>
function mysql_num_rows() determines the number of rows returned from a SELECT query statement.
<source lang="html4strict">
Its syntax is: int mysql_num_rows (int result) <? @mysql_connect("localhost", "root","") or die("Could not connect to MySQL server!"); @mysql_select_db("mydatabase") or die("Could not select database!"); $query = "SELECT title FROM mytable WHERE title LIKE \"p%\""; $result = mysql_query($query); print "Total rows selected: ".mysql_num_rows($result); mysql_close(); ?>
</source>
int mysql_num_rows ( resource result )
<source lang="html4strict">
Building Queries on the Fly <?php function opendatabase ($host,$user,$pass) {
try { if ($db = mysql_connect ($host,$user,$pass)){ return $db; } else { throw new exception ("Sorry, could not connect to mysql."); } } catch (exception $e) { echo $e->getmessage (); }
} function selectdb ($whichdb, $db){
try { if (!mysql_select_db ($whichdb,$db)){ throw new exception ("Sorry, database could not be opened."); } } catch (exception $e) { echo $e->getmessage(); }
} function closedatabase ($db){
mysql_close ($db);
} $db = opendatabase ("localhost","root",""); selectdb ("mydatabase",$db); $_POST["user"] = "root"; $_POST["pass"] = "";
function validatelogin ($user,$pass){
mysql_real_escape_string ($user); mysql_real_escape_string ($pass); $thequery = "SELECT * FROM userlogin WHERE username="$user" AND password="$pass""; if ($aquery = mysql_query ($thequery)){ if (mysql_num_rows ($aquery) > 0){ return true; } else { return false; } } else { echo mysql_error(); }
}
if (validatelogin ($_POST["user"],$_POST["pass"])){
echo "You have successfully logged in.";
} else {
echo "Sorry, you have an incorrect username and/or password.";
} //Then close the database. closedatabase ($db);
?>
</source>
Listing All Rows and Fields in a Table
<source lang="html4strict">
<html> <head> <title>Selecting Data</title> </head> <body> <?php $user = "root"; $pass = ""; $db = "mydatabase"; $link = mysql_connect( "localhost", $user, $pass ); if ( ! $link ) {
die( "Couldn"t connect to MySQL: ".mysql_error() );
} mysql_select_db( $db, $link ) or die ( "Couldn"t open $db: ".mysql_error() ); $result = mysql_query( "SELECT * FROM domains where sex="F"" ); $num_rows = mysql_num_rows( $result );
print "$num_rows women have added data to the table
\n"; print "".stripslashes($field)." | ";
mysql_close( $link ); ?> </body> </html>
</source>
mysql_num_rows.php
<source lang="html4strict">
<? mysql_connect("localhost","root",""); mysql_select_db("mydatabase"); $query = "SELECT title FROM mytable WHERE myvalue > 15.99"; $result = mysql_query($query); echo "There are ".mysql_num_rows($result)." product(s) priced above \$15.99."; ?>
</source>