FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how to connect database through php





tukun2009manit
i want to connect database through php but i don't know the connection code?
how to do it
sheedatali
tukun2009manit wrote:
i want to connect database through php but i don't know the connection code?
how to do it


Would not hurt searching in Google, there should be plenty of information on that. As for on frihost, look through tutorials and you will find some info.

P.S your post in wrong section.
urtrivedi
Code:

//connect to MySQL
$connect = mysql_connect("localhost", "bp5am", "bp5ampass")
  or die ("Hey loser, check your server connection.");
mysql_select_db("moviesite");

//create "reviews" table
$reviews = "CREATE TABLE reviews (
  review_movie_id int(11) NOT NULL,
  review_date date NOT NULL,
  review_name varchar(255) NOT NULL,
  review_reviewer_name varchar(255) NOT NULL,
  review_comment varchar(255) NOT NULL,
  review_rating int(11) NOT NULL default 0,
  KEY (review_movie_id)
)";

$results = mysql_query($reviews)
  or die (mysql_error());

//populate the "reviews" table
$insert = "INSERT INTO reviews
    (review_movie_id, review_date, review_name,
     review_reviewer_name, review_comment, review_rating)
  VALUES
    ('1', '2003-08-02', 'This movie rocks!',
     'John Doe','I thought this was a great movie even though
     my girlfriend made me see it against my will.' ,'4'),
    ('1','2003-08-01','An okay movie',
     'Billy Bob','This was an okay movie. I liked Eraserhead
     better.','2'),
    ('1','2003-08-10','Woo hoo!',
     'Peppermint Patty','Wish I\'d have seen it sooner!','5'),
    ('2','2003-08-01','My favorite movie',
     'Marvin Marian','I didn\'t wear my flair to the movie but
     I loved it anyway.','5'),
    ('3','2003-08-01','An awesome time',
     'George B.','I liked this movie, even though I thought it
     was an informational video from our travel agent.','3')";

$insert_results = mysql_query($insert)
  or die(mysql_error());
urtrivedi
Now to select and display data you can follow steps given below
Code:

$link = mysql_connect("localhost","bp5am","bp5ampass")
  or die(mysql_error());
mysql_select_db("moviesite")
  or die (mysql_error());
 
$query = "SELECT movie_name, movie_director, movie_leadactor " .
         "FROM movie";
     
$result = mysql_query($query, $link)
  or die(mysql_error());
$num_movies = mysql_num_rows($result);

$movie_header=<<<EOD     
<h2><center>Movie Review Database</center></h2>
<table width="70%" border="1" cellpadding="2"
       cellspacing="2" align="center">
  <tr>
    <th>Movie Title</th>
    <th>Year of Release</th>
    <th>Movie Director</th>
    <th>Movie Lead Actor</th>
    <th>Movie Type</th>
  </tr>
</table>
EOD;
   
$movie_details = '';
while ($row = mysql_fetch_array($result)) {
  $movie_name = $row['movie_name'];
  $movie_director = $row['movie_director'];
  $movie_leadactor = $row['movie_leadactor'];

  $movie_details .=<<<EOD
  <tr>
    <td>$movie_name</td>
    <td>$movie_director</td>
    <td>$movie_leadactor</td>
  </tr>
EOD;
}     

$movie_details .=<<<EOD
  <tr>
   <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Total :$num_movies Movies</td>
  </tr>
EOD;

$movie_footer ="</table>";     
     
$movie =<<<MOVIE
               $movie_header
               $movie_details
               $movie_footer
MOVIE;

  echo "There are $num_movies movies in our database";
  echo $movie;

infinisa
I suggest you look at the excellent PHP tutorial at w3schools.com.

The MySQL database tutorial starts here.

You should certainly read through this whole section of the tutorial, as it will help you understand the code examples already posted.

It's probably worth reading the whole tutorial - I did!
Related topics
simple php problem, please help...
Executing a .sql file through PHP
[RESOLVED] Need Help with pphlogger
Site Showing Error.
A very good PHP MySQL Tutorial
How to Make A PHP Portal.
Can΄t connect to my database with php-nuke
mysql and flash
Problem connecting to MySQL database with PHP [RESOLVED]
Looking to make an online database using php and mysql
upload image through PHP AND AJAX
Trying to connect to table through PHP page
How to connect 2 database from 2 host
What to learn first?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.