PHP/MySQL Database/mysql num rows
Содержание
Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows()
<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 "<p>$num_rows women have added data to the table</p>\n";
mysql_close( $link );
?>
</body>
</html>
function mysql_num_rows() determines the number of rows returned from a SELECT query statement.
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();
?>
int mysql_num_rows ( resource result )
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);
?>
Listing All Rows and Fields in a Table
<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 "<p>$num_rows women have added data to the table</p>\n";
print "<table>";
while ( $a_row = mysql_fetch_row( $result ) ) {
print "<tr>";
foreach ( $a_row as $field ) {
print "<td>".stripslashes($field)."</td>";
}
print "</tr>";
}
print "</table>";
mysql_close( $link );
?>
</body>
</html>
mysql_num_rows.php
<?
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.";
?>