Im working on a project at the moment and I have succesfully made a simple php web site that lists data from a sql server. Is it possible to use the data from the server and insert each row into a bootstrap 'Panel with heading' Box
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname, ooureason, reg_date FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. " Reason: " . $row["ooureason"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Related
I have a page that shows some of the information in the database. I'd like to add a link in each row (like making the first name a link) that I can click on that brings me to a page that shows the rest of that row's info (like a profile page). I'm thinking of making a link that passes the id to the profile page so that the profile page can gather the info.
I'm sure this is simple, but I'm just not getting it. How do I make the link show up in each row that sends only that row's id number? Because I'd rather not have to go into each row and make a special link.
Here is the code I have:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "
- Name: " . $row["FirstName"]. "
" . $row["LastName"]. "
" . $row["Phone"]. "
" . $row["Email"]. " <br>";
}
} else {
echo "0 results";
}
$conn->close();
?>```
Based on your example you could do this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["FirstName"] . " " . $row["LastName"] . " " . $row["Phone"] . " " . $row["Email"] . " <br/>";
}
} else {
echo "0 results";
}
$conn->close();
?>
That profile.php page would check if the id is set with isset and query the data based on id simular to this: PHP & MYSQL: Select from where id=$id
Not tested and make sure to sanitize any user-generated variables.
EDIT - Thanks so much to everyone's help. In continuing to learn, I am now working a different statement based on what I learned above but it is giving no result whatsoever. Workbench shows the query statement as good and displays result as "2"
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM(`mattiscool`),booking_date FROM `wp_cbxrbooking_log_manager` WHERE `booking_date` = CURDATE() -1";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result))
{
echo $row['total'];
}
mysqli_close($con);
?>
I am trying to call via php an MySQL Statement and have that output the values. Below is what I have tried. Could someone please help enlighten me as to what I am doing wrong?
<?php
$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\', \n"
. " wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"
. " wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"
. " wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"
. "FROM wp_cbxrbooking_log_manager\n"
. "\n"
. "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY \n"
. "ORDER BY `Time` ASC";
?>
Your code is incomplete, you have to establish a connection to database and then execute the query to get the output. You can find some good examples here and here
You need to execute your query and fetch the result as follows:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\', \n"
. " wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"
. " wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"
. " wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"
. "FROM wp_cbxrbooking_log_manager\n"
. "\n"
. "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY \n"
. "ORDER BY `Time` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I'm a bit new to MySQL and I am trying to create a search system. I am using some simple PHP code from w3schools.
I connect to my database:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "img";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then echo the results.
$sql = "SELECT ID, Name, Description, Path FROM system";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["Name"]. " " . $row["Description"]. " " . $row["Path"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My database contains one entry, but when I run this code, "0 Results" is echoed to the screen. Is there something I am doing wrong here?
I have a mysql database. There are categories in Db for my articles. I want to fetch these categories and display them as a list (side navigation). I was able to establish connection and print the categories but can't put them into the list.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["ID"]. " " . "Name: " . $row["NAME"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I know it's a dumb question, but I'm PHP newbie.
<ul>
<li>Categ 1</li>
<li>Categ 2</li>
<li>Categ 3</li>
</ul>
Thanks in advance.
you nearly got it by yourself... try this one:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<ul>";
while($row = $result->fetch_assoc()) {
echo "<li>" .$row["NAME"]. "</li>";
}
echo "</ul>";
} else {
echo "0 results";
}
$conn->close();
?>
replace
echo "ID: " . $row["ID"]. " " . "Name: " . $row["NAME"]. "<br>";
with
echo "<li>" . $row["NAME"]. "</li>";
I'm trying to read from my database and display the info in my browser but it shows up a blank page,how do i fix this?
This is my html form:
This is my php script:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$sql = "SELECT * FROM mydb";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
Thank you in advance