I have a data-table in mySQL and I need help accessing the information to display on an HTML Page.
Here are some details.
Host: 127.0.0.1:8889
username: root
password: root
database-name: gibsonek
table-name: events
Here is my code:
<html>
<head>
<meta charset="UTF-8">
<title>Gibson Ek Schedule</title>
<!--JQuery Add-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!--My JS Add-->
<script src="script.js"></script>
<!--Normalize CSS Add-->
<link rel="stylesheet" href="css/normalize.css">
<!--Google Font - Open Sans - Add -->
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600'>
<!--Bootstrap Add-->
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
<!--My CSS Add-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="navbar">
<span>Gibson Ek Schedule</span>
</div>
<div class="header">
<div class="color-overlay">
<div class="day-number"></div>
<div class="date-right">
<div class="day-name"></div>
<div class="month"></div>
</div>
</div>
</div>
<div class="timeline">
<ul id = "l">
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<p>SQL DATA WILL GO IN HERE</p>";
}
mysql_close(); //Make sure to close out the database connection
?>
</ul>
</div>
</div>
</body>
</html>
Please use PDO (it's more flexible and safe)
$servername = "127.0.0.1:8889";
$username = "root";
$password = "root";
$dbname = "gibsonek";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `events` WHERE id='1'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
var_dump($result); // or as you like
There is a good article.
There is a best guide.
<div class="timeline">
<ul id = "l">
<?php
$host = "127.0.0.1:8889";
$username = "root";
$password = "root";
$db_name = "gibsonek";
$mysqli = new mysqli($host, $username, $password, $db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '.$mysqli->connect_error);
}
$query = "SELECT * FROM `events` WHERE id='1'"; // Here if 1 means id
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
?>
<!-- Here You can add any HTML or css -->
<li><?php echo $row["column"]; ?> </li> // write your column name what you want to show
<?php
}
?>
</ul>
</div>
I think that would be clear for you, leave a comment if any query.
Thanks
I think you missed something
here is correct code:
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE id(or any column as u want)='1'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
print_r($row);
}
mysql_close(); //Make sure to close out the database connection
?>
Related
I am trying to fill bootstrap cards with data from a database.
I am very close.
I have the mainphp file called project.php
A file with a function called component to fill them, called component.php.
And a connectDB.php file, that connect the DB and extracts the information from a table.
I am aware this is alot of code so I highlighted 2 areas the problem is definitely in...Apologies if too much, I can remove the function if necessary as it works..
here is an extract from project.php
<?php
require_once('connect/connectDB.php');
require_once('component.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>The Wild Music Shop</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!--Font awesome-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<!-- i have no idea why it won't link -->
<!-- <link type="text/css" rel="stylesheet" href="myCSS/somecss.css"> -->
<link rel="stylesheet" href="myCSS/somecss.css">
</head>
<body>
<!-- the cards -->
<div class = "container" id="thecards">
<div class = "row text-center py-5">
//Definite problem here
<?php
$result = getData();
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>
</div>
</html>
This component definitely works as I tested it
<?php
function component($pInstName, $pPrice, $pProductImg, $pdescription){
$element = "
<div class=\"col-md-3 col-sm-6 my-3 my-md-0\">
<form action=\"index.php\" method=\"post\">
<div class=\"card shadow\">
<div>
<img src=\ $pProductImg\" alt=\"Image1\" class=\"img-fluid card-img-top\">
</div>
<div class=\"card-body\">
<h5 class=\"card-title\">Our $pInstName</h5>
<h6>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"far fa-star\"></i>
</h6>
<p class=\"card-text\">
$pdescription
</p>
<h5>
<span class=\"price\">$$pPrice</span>
</h5>
<button type=\"submit\" class=\"btn btn-warning my-3\" name=\"add\">Add to Cart </button>
</div>
</div>
</form>
</div>
";
echo $element;
}
?>
and finally here is the connectDB.php
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
if (!$connection)
{
die('Could not connect: ' . mysqli_error());
}
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
$row=mysqli_fetch_assoc($result);
echo "Pls work".$row['InstName'];
mysqli_close($connection);
//get product from Database
//Definite problem here
function getData(){
$sql = "select * from $this->tablename";
$result = mysqli_query($this->con,$sql);
if(mysqli_num_rows($result) > 0){
return $result;
}
}
?>
I am awar the problem is somewhere in the connection step. I am very new to php in general and have been at this very long, I would appreciate help!
You have a function getData() that has "$this->table". Dont copy paste the codes without understanding what it does.
Clean up your connectDB.php:
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
In project.php:
<div class = "row text-center py-5">
//Definite problem here
<?php
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>
I'm trying to use $_GET to call a dynamic page, but it throws "Error". Do I need to call page 1 from page 2 or why can't they talk to each other?
Thanks in advance for your help, supposingly there's an easy solution.
/Jimmy
This is page 1:
<!DOCTYPE html>
<html>
<head>
<title>Players</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php"); ?>
<?php
$servername = "localhost";
$username = "jim";
$password = "pass";
$dbname = "jim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL search
$sql = "SELECT PlayerID, Person FROM People where PlayerID is not null ORDER BY Person";
$result = $conn->query($sql);
// Condition
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Player</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>"."<a href='spelarfakta.php?=id{$row['PlayerID']}'>{$row['PlayerID']}</a>"."</td>"."<td>".$row["Person"]."</td>";
}
echo "</table>";
} else {
echo "No hits";
}
$conn->close();
?>
</body>
</html>
This is page 2:
<!DOCTYPE html>
<html>
<head>
<title>Player Stats</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php");
if (isset($_GET['id'])) {
echo '<p>'. $_GET['id'].'</p>';}
else {echo 'Error';
}
?>
</body>
</html>
Correct solution as a comment:
=id should be id=
I want to make a page where the element will redirect the user to a random page from the database.
I made the HTML code, but I can not deal with PHP and MySQL - I was able to connect to the database, but any attempt to connect the MySQL code with the tag ended in a loss.
Can I count on help writing a PHP script and linking code with ?
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat+Subrayada" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<title>Random it</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header></header>
<div class="random">
Random
</div>
<?php
require_once "connect.php";
$conn = #new mysqli($host, $db_user, $db_password, $db_name);
$sql = "SELECT link FROM randomit ORDER BY RAND()";
?>
<footer></footer>
</body>
</html>
Let's try this:
Put this PHP part at the top of the page;
<?php
// set a default link in case something goes wrong
$DEFAULT_LINK = "default.html";
// connect to DB
require_once "connect.php";
$conn = new mysqli($host, $db_user, $db_password, $db_name);
// set query -> get 1 result only with LIMIT 1
$sql = "SELECT link FROM randomit ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result !== false)
$row = mysqli_fetch_assoc($result);
else
$row = false;
// if we have a valid result => use it
// else => use the default
if ($row && isset($row["link"]))
$RANDOM_LINK = $row["link"];
else
$RANDOM_LINK = $DEFAULT_LINK;
?>
And now the HTML part:
<!DOCTYPE html>
<html>
<!-- head part... -->
<body>
<header></header>
<div class="random">
Random
</div>
<footer></footer>
<!-- etc... -->
I'm making a website for school about MLB with a database. But now I want information in the database as an option in a dropdownlist. This is my HTML Code:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png"
sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png"
sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet"
type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
echo "<select name='naamteam'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['teamnaam'] ."'>" . $row['teamnaam'] ."
</option>";
}
echo "</select>";
?>
</div>
</div>
</body>
</html>
The options would be the teamnames, but it seems like my dropdownlist is still empty: picture of the dropdownlist on the website
This is what my database looks like:
picture of my database
I hope someone could help me and I would really appreciate it.
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png" sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
$dropdownlist = '';
while($row = mysqli_fetch_array($result)) {
$teamnaam = $row['teamnaam'];
$dropdownlist .="<option value='" . $teamnaam . "'>" . $teamnaam . "</option>";
}
if(isset($dropdownlist)){
echo "<select name='naamteam'>";
echo $dropdownlist;
echo "</select>";
}
?>
</div>
</div>
</body>
</html>
EDIT: Thank you for correcting the formatting... did not notice that mistake
I have completed this basic quote generator as a first project, but have noticed that it frequently displays the same quote upon refresh. Granted, there are only five at the moment, but this problem might still be apparent as I fill the database. I am currently learning JQuery AJAX for a more advanced one - but am only starting. To my knowledge, this is not a duplicate question. Displays database query upon reload. Thank you!
Here is the code:
connect.php:
<?php
$connection = new mysqli('localhost', 'root', '', 'random_quotes');
if ($connection->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ')'
. $mysqli->connect_error);
}
?>
functions.php:
<?php
require ('connect.php');
$query = "SELECT id, quote, author FROM quotes ORDER BY RAND() LIMIT 1";
$getQuote = $connection->query($query);
?>
HTML:
<!doctype html>
<html lang="en">
<?php
require ('includes/connect.php');
require ('includes/functions.php');
?>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Candal' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container-fluid text-center">
<h1>Random Quote Generator</h1>
<p>Some of my favourite all around quotes!</p>
<br/>
<button class="btn btn-default" onclick="newQuote()" type="submit">New Quote
</button>
<div class="quote_wrap text-center">
<span class="quote">
<?php
while($row = $getQuote->fetch_assoc()){
$stringID = $row['id'];
$stringQuote = $row['quote'];
$stringAuthor = $row['author'];
echo $stringQuote;
}
?>
</span>
</br>
<div class="author text-center"><?php echo $stringAuthor?></div>
<div class="quoteid" id="<?php echo $stringID?>"></div>
</div>
</div>
<script src="https: /ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
function newQuote() {
location.reload();}
</script>
</body>
</html>
Generate a random number and then access quote by that id. May work better..
$query = "SELECT COUNT(*) as noQuotes FROM quotes ";
$result = $connection->query($query);
$row = $result->fetch_assoc();
$noQuotes = $row["noQuotes"];
srand(time());
//echo(rand(1,$noQuotes));
$randNo = rand(1,$noQuotes);
$query = "SELECT id, quote, author FROM quotes WHERE id = $randNo ";
$getQuote = $connection->query($query);