Search:

Categories



Count the number of rows

Count the number of rows in a MySQL database

//database can be found at http://www.getphp.net/sample-mysql-database/

<?php
//connect to server with username and password
$connection = mysql_connect (“localhost”,”username”, “password”) or die (“Cannot make the connection”);
//connect to database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
$rows = mysql_num_rows($result);
//output total
echo $rows;

?>

Share

Display MySQL database rows alphabetically

//database can be found at http://www.getphp.net/sample-mysql-database/

<?php
//connect to server with username and password, this is the default settings
$connection = mysql_connect (“localhost”,”username”, “password”) or die (“Cannot make the connection”);
//connect to database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test ORDER BY name ASC”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
if(mysql_num_rows($result))
{
//output as long as there are still available fields
while($row = mysql_fetch_row($result))
{
echo (“<a href=\”$row[2]\”>$row[3]</a>”);
echo (“: $row[4]<br>”);
}
}
//if no fields exist
else
{
echo “no values in the database”;
}

?>

Share

Display a random entry from a MySQL database

This example displays one random link from the example database mentioned below

//database can be found at http://www.getphp.net/sample-mysql-database/

<?php
//connect to server with username and password
$connection = mysql_connect (“localhost”,”username”, “password”) or die (“Cannot make the connection”);
//connect to database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test ORDER BY RAND() LIMIT 1”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
if(mysql_num_rows($result))
{
//output as long as there are still available fields
while($row = mysql_fetch_row($result))
{
echo (“<a href=\”$row[2]\”>$row[3]</a>”);
echo (“: $row[4]<br>”);
}
}
//if no fields exist
else
{
echo “no values in the database”;
}

?>

Share

Display x amount of links from a MySQL database

This example will display only 5 entries from the sample MySQl database
<?php
//database can be found at http://www.getphp.net/sample-mysql-database/
//connect to server with username and password, this is the default settings
//when MySQL is installed on Windows XP(Not recommended)
$connection = mysql_connect (“localhost”,”root”, “”) or die (“Cannot make the connection”);
//connect to database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test LIMIT 5”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
if(mysql_num_rows($result))
{
//output as long as there are still available fields
while($row = mysql_fetch_row($result))
{
echo (“<a href=\”$row[2]\”>$row[3]</a>”);
echo (“: $row[4]<br>”);
}
}
//if no fields exist
else
{
echo “no values in the database”;
}

?>

Share

Show all rows in a MySQL table

This shows all the available rows in a MySQL table

//sample database can be found at http://www.getphp.net/sample-mysql-database/ <?php
//connect to server with username and password, this is the default settings
//when MySQL is installed on a Windows XP rig (Not recommended)
$connection = mysql_connect (“localhost”,”root”, “”) or die (“Cannot make the connection”);
//connect to the test database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
if(mysql_num_rows($result))
{
//output as long as there are still available fields
while($row = mysql_fetch_row($result))
{
echo (“<a href=\”$row[2]\”>$row[3]</a>”);
echo (“: $row[4]<br>”);
}
}
//if no fields exist
else
{
echo “no values in the database”;
}
?>

Share

list all databases

list all your MySql databases using PHP

<?php
$server = “localhost”;
$username = “root”;
$password = “”;
$con = mysql_connect(“$server”,”$username”,”$password”)or die (“Couldn’t connect to MySQL Server.”);

$listdb = mysql_list_dbs($con);
while ($row = mysql_fetch_object($listdb))
{
echo $row->Database . “<br>”;
}
?>

Share