-1

I am currently in a class at school and we have to link our MySQL database with our website using php. I already made and populated my tables in MySQL. My professor sent us this chunk of code to display the table info on our websites. However when I run it nothing happens and I haven't learned enough about php to know why it is not working. I used my correct host name, password, ect. But it won't work and when he does the tutorial online in the video it works for him.

This is the code I am using.

<html>
<head>
<title>Query All Movies from Database</title>
<body>

<?

@ $db = mysql_pconnect("localhost","username","password");

if (!$db)
{
    echo "ERROR: Could not connect to database.  Please try again later.";
    exit;
}

mysql_select_db("database name");

$query = "select * from movie";

$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo "<p>Number of movies found: ".$num_results."</p>";

for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p>";
echo htmlspecialchars( stripslashes($row["movieid"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["title"]));
echo "<br>";
//echo htmlspecialchars( stripslashes($row["directorid"]));
//echo "<br>";
echo htmlspecialchars( stripslashes($row["year"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["genre"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["runtime"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["plotdescription"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["comments"]));
echo "<br>";
echo "</p>";

}  

?>

</body>
</html>

This is the output I am getting. Directly to the screen.

Number of movies found: ".$num_results."
"; for ($i=0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo "
"; echo htmlspecialchars( stripslashes($row["movieid"])); echo "
"; echo htmlspecialchars( stripslashes($row["title"])); echo "
"; //echo htmlspecialchars( stripslashes($row["directorid"])); //echo "
"; echo htmlspecialchars( stripslashes($row["year"])); echo "
"; echo htmlspecialchars( stripslashes($row["genre"])); echo "
"; echo htmlspecialchars( stripslashes($row["runtime"])); echo "
"; echo htmlspecialchars( stripslashes($row["plotdescription"])); echo "
"; echo htmlspecialchars( stripslashes($row["comments"])); echo "
"; echo "

"; } ?>

I did alot of research and read the links and here is the working code! Thanks for helping teach me!! This site is so great! You guys are awesome!

<?php
$servername = "localhost";
$username = "ursername";
$password = "password";
$dbname = "database name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT movieid, title, directorid, year, genre, runtime, plotdescription, comments FROM movie";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Movie ID: " . $row["movieid"]. "<br>Title: " . $row["title"]. "<br>Director ID: " . $row["directorid"]. "<br>Year: " . $row["year"]. "<br>Genre: " . $row["genre"]. "<br>Run Time: " . $row["runtime"]. "<br>Plot Description: " . $row["plotdescription"]. "<br>Comments: " . $row["comments"]." <br><br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Thrifty Coder
  • 41
  • 1
  • 8
  • 1
    Why not ask your professor? He should explain better than us. – Felippe Duarte Nov 18 '16 at 19:28
  • 5
    For starters, use full full PHP tags and remove the @, it suppresses errors that may be helpful to you.. On another note.. tell your teacher they should be teaching mysqlI << I at the end or PDO method – Duane Lortie Nov 18 '16 at 19:28
  • I ran a test code to make sure I could connect to my database and it worked so all my user info is correct. However when i run this code it wont even tell me if it is connected or not. I just spits part of the code on the screen. – Thrifty Coder Nov 18 '16 at 19:28
  • 4
    *facepalm* for a professor giving out `mysql_` based code – jmoerdyk Nov 18 '16 at 19:29
  • 6
    Your teacher sucks. `mysql_` calls have been depreciated. – TheValyreanGroup Nov 18 '16 at 19:29
  • I asked my teacher and he doesn't know. The person that usually taught this course just left. – Thrifty Coder Nov 18 '16 at 19:29
  • 5
    Well he needs to leave too. He's teaching 8 year old methods. And _he doesn't know_. Seriously......? – TheValyreanGroup Nov 18 '16 at 19:30
  • Are you working on a Mac or other unix machine? Do you have the unix permissions on the php file set correctly so the file is readable and can execute? If so please look at the unix command chmod. Since your php file is doing nothing I suspect you may have a file permissions issue. – mba12 Nov 18 '16 at 19:30
  • I suspect that`` is not a PHP tag :/ – Duane Lortie Nov 18 '16 at 19:31
  • I am just using a windows pc. I added php to the first tag and took away the @. Now the page just shows nothing.... – Thrifty Coder Nov 18 '16 at 19:33
  • I checked the database and all the variables are spelled correctly in the code and everything matches. – Thrifty Coder Nov 18 '16 at 19:34
  • 1
    Repost your new code if you've changed something. If you're seeing PHP commands as plain text you have a tag wrong somewhere. – TheValyreanGroup Nov 18 '16 at 19:36
  • 2
    Take [this link](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and share it with your professor. The `mysql_` functions were removed from PHP in 7.0. Also, unless you have some specific reason, **never** use `pconnect`. You'll eat up all your MySQL connections. – Machavity Nov 18 '16 at 19:47
  • *not working* is not a valid question and unless you show what **you** have tried and why it did not work, you will not understand any explinations or why what your teacher gave you is outdated crap. –  Nov 18 '16 at 19:59
  • Thanks I checked out the link. But could you show me how to get this to display on my webpage. Im getting so aggravated. I just want to display my data. My college sucks. Im sorry guys. – Thrifty Coder Nov 18 '16 at 20:09
  • I figured it out guys! – Thrifty Coder Nov 19 '16 at 01:27

2 Answers2

1

Don't use that code - that snippet is not secure these days. You should be using one of the newer connection methods like PDO or MySQLI. I prefer the latter, try using the docs on PHP's site to set up your stuff.

http://php.net/manual/en/function.mysqli-connect.php

The only reason I could imagine that your teacher is using this method is either they don't know any better or they are using a very old version of PHP.

Older MySQL functions are procedural and get wonky when writing in OOP (object oriented programming) because they are manually escaped. The newer mysqli_ functions work with both procedural and OOP and support prepared statements. Prepared statements are safer because they parameterize the values so you run into less issues with SQL injection and other vulnerabilities. You also get some speed enhancements because the prepared statements only have to parsed on the preparation and not the execution. So if you use a lot of the same parameters you get some extra speed!

PDO also supports prepared statements, but its a little more complex for newcomers because it introduces an abstraction layer (basically you build the query in PHP instead of raw SQL statements). This was a turn off for me when I first started so I would try getting good at the MySQLi stuff before you look too deep into PDO.

Matthew R.
  • 4,332
  • 1
  • 24
  • 39
  • I have no idea what MySQLI is? – Thrifty Coder Nov 18 '16 at 19:40
  • MySQLi is built into PHP. It's a newer way to connect to the database. The function you are using is deprecated (it is outdated but left in the code base so that older sites dont break). Here is a good guide to MySQLi that walks you through everything! http://php.net/manual/en/mysqli.quickstart.php – Matthew R. Nov 18 '16 at 19:44
1

There are so many fundamental issues here, but it seems like the issue with your output being wrong is because you're concenating the echo string, and doing wrong. With PHP you can put variables inside of double quotes and it will still parse correctly. And as I said, the code you have posted cannot be outputting that.

So change your first echo line to this and see what happens.

echo "<p>Number of movies found: $num_results</p>";

I always say, it's far better to teach yourself something than learn from a so called professor. He has given you depreciated code, that is now removed in PHP 7. He has told you i don't know what's wrong with your code and completely steered you in the wrong direction for secure, modern web development. This professor has no business teaching anyone PHP.

TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30