This example displays one random link from the example database mentioned below
//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 ("$row[3]");
echo (": $row[4]
");
}
}
//if no fields exist
else
{
echo "no values in the database";
}
?>
Showing posts with label mysql_fetch_row. Show all posts
Showing posts with label mysql_fetch_row. Show all posts
Monday, September 28, 2009
Thursday, September 24, 2009
Display X entries from MySQL table
This example will display only 3 entries from the 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 3";
//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 ("$row[3]");
echo (": $row[4]
");
}
}
//if no fields exist
else
{
echo "no values in the 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 3";
//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 ("$row[3]");
echo (": $row[4]
");
}
}
//if no fields exist
else
{
echo "no values in the database";
}
?>
Monday, September 21, 2009
Display all entries in a MySQL table
This shows all the available rows in a MySQL table
//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 ("$row[3]");
echo (": $row[4]
");
}
}
//if no fields exist
else
{
echo "no values in the database";
}
?>
//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 ("$row[3]");
echo (": $row[4]
");
}
}
//if no fields exist
else
{
echo "no values in the database";
}
?>
Labels:
mysql,
mysql_fetch_row,
mysql_query,
mysql_select_db,
php
Subscribe to:
Posts (Atom)