Fetching All Data From mySQL And Display It In A Data-grid

josephbupe

Registered User.
Local time
Today, 19:28
Joined
Jan 31, 2008
Messages
247
Hi,

I am beginning to learn PHP and mySQL starting on a simple movies database.

Data was loading okay from mySQL database in one of my PHP pages (select.php) with a SELECT statement until i included a few codes to query data through a form. What is happening now is that when I open this page, data does not appear immediately until I run a query through a form then it will appear below the same form in a data-grid layout.

However, I need help to fetch and display all the data in mySQL database when the page is opened first, and if possible re-query the data-grid when the search form is cleared.

Here is my code:

Code:
<html>
<head>
<title>SELECT data in mySQL database</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
  <table width="599" border="1">
    <tr>
      <th>Keyword
      <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
      <input type="submit" value="Search"></th>
    </tr>
  </table>
</form>
<?

if($_GET["txtKeyword"] != "")
    {
    $objConnect = mysql_connect("localhost","joseph","encrypted") or die(mysql_error());
    $objDB = mysql_select_db("movies");
    // Search By Title or Year or Country
    $strSQL  = "SELECT * FROM movies WHERE (mtitle LIKE '%".$_GET["txtKeyword"]."%'  or mcountry LIKE '%".$_GET["txtKeyword"]."%')";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    $Num_Rows = mysql_num_rows($objQuery);

...etc

I will appreciate.

Joseph
 
Till some one comes along, have you tried something like below ( the logic ):

PHP:
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
  <table width="599" border="1">
    <tr>
      <th>Keyword
      <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
      <input type="submit" value="Search"></th>
    </tr>
  </table>
</form>
<?

$objConnect = mysql_connect("localhost","joseph","encrypted") or die(mysql_error());
$objDB = mysql_select_db("movies");

if($_GET["txtKeyword"] != "")
{
    // Search By Title or Year or Country
    $strSQL  = "SELECT * FROM movies WHERE (mtitle LIKE '%".$_GET["txtKeyword"]."%'  or mcountry LIKE '%".$_GET["txtKeyword"]."%')";
} 
else	
{
    // Search By Title or Year or Country
    $strSQL  = "SELECT * FROM movies ";
}

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

...etc

Thanks
 
Hi Ricyan,

I tried your code and it works perfectly well.

Thanx alot.

Joseph
 
Last edited:
Glad you found it helpful.
Do not know if this forum is oriented towards php & mysql ( I may be wrong ).
Nearly a year back, when I was dabbling with php & mysql,
I found the guys / gals at http://forums.devshed.com/ very helpful, specially for php & mysql.
Though, I have not used this ( http://forums.phpfreaks.com/ ) myself, I used to hear good reports about it.

Thanks :)
 
Thanx for the information. I will check them.

Best regards.

Joseph
 

Users who are viewing this thread

Back
Top Bottom