Load MySQL data - php

I have MySQL server installed and I have created a test database named "test" and done a simple login example, with the ID, username, password and email. According to W3Schools, I have modified the code they provide:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, password, email FROM user";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Username</th><th>Password</th><th>Email</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"]. "</td><td>" . $row["password"]. "</td><td>" . $row["email"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
I've searched the directory of the database and I see in C:\Program Files\MySQL\MySQL Server 5.6\data a folder named "test" that contains 3 files, a .opt file, a .frm and a .ibd.
My question is, where should I put the html file in order to load this database data? Or should I change something from my code? Thanks!

Related

My PHP code doesnt seem to want to connect to the database and/or display the data, can someone point out what's wrong?

So basically I am trying to simply just connect to my local mysql database with a football table to display on the site, but it doesn't seem to be able to connect. I feel like it's really obvious but I just can't see it.. I have checked the password is correct also, and it is.
<?php
$servername = "localhost";
$username = "root";
$password = "password"
$dbname = "import_excel_to_mysql";
/* Create connection */
$conn = new mysqli("localhost", "root", "password", "import_excel_to_mysql");
/* Check connection */
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error . mysql_error());
}
$sql = "SELECT position, team_name, played, wins, draws, losses, goals_scored, goals_against, goal_difference, points FROM football table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Pos</th><th>Team Name</th><th>P</th><th>W</th><th>D</th><th>L</th>(etc)</tr>";
/* output data of each row */
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["position"]. "</td><td>" . $row["team_name"]. " " . $row["played"]. " " . $row["wins"]. " " . $row["draws"]. (etc)"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
It should display the table just on its own, but instead it displays all the php code starting at connect_error(. The rest of the site is unaffected.

Displaying results of SQL query as a table?

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.

use a PHP variable as the source in an iframe to play a youtube video

I have spent the better part of three days on this problem. I've tried several solutions that I've found on this site but with little success. What I'm trying to do is use a PHP variable to play a youtube video in an iframe. I'm also trying to run two MySQL queries with the same input. Here is my code as it sits right now. At this moment when it runs I get the table that I'm wanting, though I still need to format it. But the iframe isn't even showing up. A previous solution I tried would pull up the iframe but inside would be an error where I was basically passing the sql query to the iframe.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$trainingid = $_POST["trainingid"];
$sql = "SELECT * FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Training ID</th><th>Title</th><th>Training URL</th><th>Training Quiz URL</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["trainingid"] . "</td><td>" . $row["trainingtitle"] . "</td><td>" . $row["trainingurl"] . "</td><td>" . $row["trainingquizurl"] . "</td></tr>";
}
echo "</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$videosql = "SELECT trainingurl FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$videoresult = $conn->query($videosql);
if ($videoresult->num_rows > 0) {
while ($videourl = $videoresult->fetch_assoc()) {
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?php echo htmlspecialchars($videourl); ?>' frameborder="0" allowfullscreen></iframe>
</div>
It's common practice not to use the variable $videourl outside of the while loop. (Put your iframe within the while loop).
sidenote: fetch_assoc() will create an array. It should be $videourl['trainingurl'];
Here's an example:
<?php
if ($videoresult->num_rows > 0)
{
while($videourl = $videoresult->fetch_assoc()){
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?= $videourl['trainingurl']; ?>' frameborder="0" allowfullscreen></iframe>
</div>
<?php
}
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
edit for clarity and future readers
Note that the <?= $variable ?> opening tag has only been properly supported since php 5.4+. When not using this version, maintain the usage of <?php echo $variable; ?>

SQL query not executing in php command line interface created by me

I have made a SQL command line interface for my own purpose and testing with the following code:
input.php
<!DOCTYPE html>
<html>
<body>
'DESC'command under dev...
<form action="output.php" method="post">
<textarea name="query"></textarea>
<input type="submit">
</form>
</body>
</html>
output.php
<!DOCTYPE html>
<html>
<body>
<div>
<?php
$servername = "server_name"; //I do not wish to show my credentials
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = $_POST["query"];
$result = $conn->query($sql);
if ($result === TRUE) {
if (substr($sql, 0, 6) == "SELECT") {
//Initialize table
echo "<table class=\"table\">";
echo "<tr><th>ID</th><th>Username</th><th>Password</th><th>Email</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"]. "</td><td>" . $row["password"]. "</td><td>" . $row["email"] . "</td></tr>";
}
echo "</table>";
echo "<code>" . $result->num_rows . " rows selected</code>";
} elseif (substr($sql, 0,11) == "INSERT INTO") {
echo "Inserted. Command: $sql";
} elseif (substr($sql, 0, 6) == "DELETE") {
echo "Deleted. Command: $sql";
} elseif (substr($sql, 0, 6) == "UPDATE") {
echo "Updated. Command: $sql";
} else {
echo "Code under dev...\nSorry!";
}
} else {
echo "ERROR: " . $conn->error;
}
echo "<br>";
?>
</div>
</body>
</html>
I have checked the database credentials; they're all fine- no conn error.
I know this because I worked with a table with some data before.
Now, on entering a query, nothing happens except it leaves a message- 'ERROR: '.
Please inform me of any errors.
The query() function only returns TRUE when called with a SQL-statement that doesn't affect rows. In all other cases it returns an object of the type mysqli_result().
That causes the if ($result === TRUE) to jump to else and display the error. In fact there was no error, as $result probably holds a result object.
Try to confirm this by adding echo $result->num_rows; to your else clause.
mysqli::query
See the mysqli::result documentation for more information on how to process your query results.

MySQL php select statement using WAMP server

Im trying to display a database table in my html page but cant seem to get it to work. (code below):
<!DOCTYPE html>
<html>
<body>
<h1> Table </h1>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Library";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `books` ORDER BY `Category` ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["ISBN"]. " Name: ". $row["firstname"]. " - Author: " . $row["BookAuthor"]. " Category: " . $row["Category"]. " - Quantity: " . $row["Quantity"]. " - Price: " . $row["Price"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Im using WAMP server and the "books" table is located in a database called Library. However the table doesnt display and all i get is the php code being shown.
Any idea on whats gone wrong?
Thanks!
Is your WAMP running? (Check the system tray icon. It should be green)
Did you mess with PHP or Apache configs?
Is the file extension .php? It has to be.
Is other PHP displaying properly?

Categories