0
<?php
//connection to the database 
try {     
  $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                    frostedc_user, 'pass'); 
 echo "connected"; 
}  
catch(PDOException $e) {  
 echo $e; //2 
} 

// select everything from the news table
$query = "SELECT * FROM movie";// Table name NOT database name 

foreach ($pdo->query($query) as $row) {
    echo "<table border='1'>";
    echo "<tr>";
        echo "<td width='150'>".$row['movietitle']."</td>";
        echo "<td width='150'>".$row['genre']."</td>";
        echo "<td width='150'>".$row['LastViewed']."</td>";
        echo "<td width='150'>".$row['Location']."</td>";
    echo "</tr>";

}
echo "</tr>";
echo "</table>";
echo "<br>";
echo "
    <form>
        <p>Please Enter a Movie Title</p>
        <input type='text' name='new_movie' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Genre</p>
        <input type='text' name='movie_genre' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Last View Date</p>
        <input type='text' name='last_view' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Location</p>
        <input type='text' name='movie_loca' />
        <input type='submit' value='Submit' />
    </form>";




$pdo = null;

?>

this is the new updated code. I am trying to use the inputs to enter data into my database. I have researched how to do this, but so far I haven't got anything to work. Any thoughts? Also would it be easier for me to use include and make the inputs in html? if so could i use them to enter the data into the db?

  • mysql() is deprecated...use mysqli() – chirag ode Sep 21 '13 at 04:11
  • 3
    your mixing PDO with mysql –  Sep 21 '13 at 04:11
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Sep 21 '13 at 12:22

4 Answers4

1

You are doing 2 wrong things here

First : Mixing PDO with MySQL
Second : $query = "SELECT * FROM myDB"; You cant select from a Database.. You need to do a SELECT from your TABLE ! (Are you sure myDB is your table ?)

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

As you have tagged your question PDO I have removed all unneeded code from your example.

<?php
//connection to the database 
try {     
    $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                    user, 'password'); //1
    echo "connected"; 
}  
catch(PDOException $e) {  
    echo $e; //2 
} 
// select everything from the news table
$query = "SELECT * FROM myTable";// Table name NOT database name 
echo "<table>";
echo "<tr>";
foreach ($pdo->query($query) as $row) {//3
    echo "<td>".$row['movietitle']."</td>";
    echo "<td>".$row['genre']."</td>";
    echo "<td>".$row['LastViewed']."</td>";
    echo "<td>".$row['Location']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
$pdo = null;//5
?>

The number comments

1 Setting character set Manual

2 Echoing error message only for development.In production you should either do something with it or remove try/catch.

3 As there are no parameters use query()

4 For utf8 Prior to PHP 5.3.6

5 Changed mysql_close();(mysql_) to $pdo = null; (PDO)

david strachan
  • 7,174
  • 2
  • 23
  • 33
  • Thank you! I am trying to learn PHP and PDO and MYSQL to access my database, but as you can tell I'm not doing so hot... – Nathaniel Miller Sep 21 '13 at 16:52
  • I get an error Warning: mysql_close(): no MySQL-Link resource supplied in /home/frostedc/public_html/test/index.php on line 24 – Nathaniel Miller Sep 21 '13 at 16:52
  • @NathanielMiller Sorry my wrong. Didn't provide proper code for closing database connection. See edit – david strachan Sep 21 '13 at 17:22
  • thats ok, thank you so much for you help! any advice where I can learn how to do the above code better? I'm trying to be able to add and change the information from the table using user input. So far I'm confused from all of the sights I have looked at – Nathaniel Miller Sep 21 '13 at 17:30
  • @NathanielMiller Here is a good [**tutorial**](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). If you find my answer useful you can accept it. You can always ask a new question just make sure you provide code,any errors,etc. – david strachan Sep 21 '13 at 17:45
  • to update the database information, i have an html input. how do I use that to enter it into the database? code: – Nathaniel Miller Sep 21 '13 at 20:16
  • echo "

    Please Enter a Movie Title

    "; echo "

    Please Enter the Genre

    "; echo "

    Please Enter the Last View Date

    "; echo "

    Please Enter the Location

    ";
    – Nathaniel Miller Sep 21 '13 at 20:16
  • Should be new question.setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $stmt = $dbh->prepare("INSERT INTO `movies` (`id`, `name`) values (NULL, ? )"); $stmt->bindParam(1, $new_movie); $stmt->execute(); $dbh = null; ?>

    Please Enter a Movie Title

    – david strachan Sep 21 '13 at 22:25
-1
<?php // connect to the database
  $host = 'localhost';
  $username = 'user';
  $pass = 'password';
  $conn=mysql_connect($host,$username,$pass)or die(mysql_error());
  mysql_select_db("myDB");
  // select everything from the news table
  $query = "SELECT * FROM news";
  $result = mysql_query($query);

  while($row = mysql_fetch_array($result)){

    echo "<table>
            <tr>
              <td>".$row['movietitle']."</td>
              <td>".$row['genre']."</td>
              <td>".$row['LastViewed']."</td>
              <td>".$row['Location']."</td>
            </tr>
          </table>";
  }

  // disconnect from the database
  mysql_close();
?>
Milan and Friends
  • 5,560
  • 1
  • 20
  • 28
-1

try this code

<?php // connect to the database
$host = 'localhost';
$username = 'user';
$pass = 'password';
mysql_connect($host,$username,$pass);
mysql_select_db("myDB");
// select everything from the news table
$query = "SELECT * FROM `tablename`";
$result = mysql_query($query);
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['movietitle']."</td>";
echo "<td>".$row['genre']."</td>";
echo "<td>".$row['LastViewed']."</td>";
echo "<td>".$row['Location']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
Janak Prajapati
  • 896
  • 1
  • 9
  • 36