I've compiled the following PHP and HTML, what I want to do is connect my WAMP database to my webpage, it's a simple task but the output i receive is displayed in the picture below, can somebody show me where i went wrong?
<?php
//Step 1
$db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>
PHP connect to MySQL
</h1>
<?php
//Step 2
$query = "SELECT * FROM patients";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
echo $row['id'] . ' ' . $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
}
?>
</body>
</html>
PHP and HTML error
I have found some errors in that code
$query = "SELECT * FROM patients";
You need to add the semicolon a the end of the query code here, so:
$query = "SELECT * FROM patients;";
Then we have this
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
echo $row['id'] . ' ' . $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
}
This should work
$result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$patientname = $row['patient_name']; //do this with every variable you have
echo "$id $patientname";
}
EDIT: Also, change this
$db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');
with this
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "hospital";
$db = mysqli_connect($dbhost,$dbuser,$dbpass) or die ("dbconn");
mysql_select_db($dbname,$db) or die ("msq");
You dont need to assign
$row = mysqli_fetch_array($result); as you are assigning it inside the while loop
EDITED ANSWER
Build a sperate array from the rows and then loop over that, this way you will have more flexibility to add to or edit the results before displaying them
//Step 2
$query = "SELECT * FROM patients";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$rows = array();
while($row = mysqli_fetch_array($result)){
$rows[] = $row;
}
foreach($rows as $r) {
echo $r['id'] . ' ' . $r['patient_name']. ': ' . $r['check_in_date'] . ' ' . $r['room_number'] . ' ' . $r['bed_number'] . ' ' . $r['notes'] . '<br />';
}
Related
I have re-written this question because of all the down votes and it would seem that either no one understood the question or was unwilling to help a newbie. So I'll as it this way: Can someone tell/show me why this code does not work? (500 error)
OR... at the very least point me in the right direction? I AM WILLING TO LEARN, I just don't know where to begin or who/where to learn from as I am not sure what to even ask other than how do you run multiple "else" statements... that however left me more confused than I already am though!
Any "HELP" would be greatly appreciated!
<?php
$servername = "localhost";
$username = "****";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$bname = $_POST['bname'];
$baddress = $_POST['baddress'];
$bcity = $_POST['bcity'];
$bstate = $_POST['bstate'];
$zipcode = $_POST['zipcode'];
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Login FROM `USERS` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/realsite.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "SELECT Login FROM `DATA` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/demo.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "INSERT INTO `DATA` (Business_Name, Business_Address, Business_City, Business_State, Business_Zip)
SELECT '$bname', '$baddress', '$bcity', '$bstate', '$zipcode' FROM (SELECT 1) t
WHERE NOT EXISTS (SELECT Login, Business_Name,Business_Address FROM `DATA` WHERE Business_Name='$bname' AND Business_Address='$baddress')";
if (mysqli_query($conn, $sql))
{
echo "<a href='https://www.servedwell.com/realsite.php?Login=" . $row["Login"] . "'>LINK</a>";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
Credit goes to #Ancaron for properly formatting code and correcting a syntax error. Final posted code works fine.
<?php
$host = 'localhost';
$user ='root';
$pass = '';
$db = 'sad';
$con =mysqli_connect($host,$user,$pass,$db);
?>
<!DOCTYPE html>
<html>
<head>
<title>ADMIN</title>
<style>
table,th,td
{
border: 1.5px solid black;
}
</style>
<script>
function approve()
{
<?php
$hide = $_POST['omg'];
$qwe = "INSERT INTO `enrolled` SELECT * FROM `new` WHERE `id` = '$hide' ";
?>
}
</script>
</head>
<body>
<form method='POST'>
<table width=500px >
<?php
$sql = "SELECT * FROM New";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<form method='POST'><tr><td>ID: " . $row["id"]. "<br>FIRSTNAME: " . $row["fname"]. "<br>MIDDLENAME: " . $row["mname"]. "<br>LASTNAME : " . $row["lname"]. "<br>COURSE: " . $row["course"]. "<br>ADDRESS: " . $row["address"]. "<br>CONTACT: " . $row["contact"] . "<br><br><input type='text' name='omg' value=$row[id]> <input type='button' name= 'btn' value='Approve' onclick=approve()></td></tr></form>";
}
}
else
{
echo "No Request Sent!";
}
?>
</table>
</form>
</body>
</html>
Im having a problem on copying a certain data of a table and transfer it into another table. this code $qwe = "INSERT INTO enrolled SELECT * FROM new is working and outputs all data contains by table 'New' and if i place WHERE id = '$hide' ; the database 'enrolled' shows nothing.
Something like this should work. First, select your dataset and then build the new insert query and in your loop. It's not the best solution, but hopefully you will understand the concept behinde this. But maybe you need a update query ... when you want to "insert" data at a specfic point. If you want only to copy youre data. This should work.
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'sad';
$connection = mysqli_connect($host, $user, $pass, $database);
$query = "SELECT * FROM New";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$query = 'INSERT INTO `enrolled` (id, fname, mname, lname, course, address, contact)
VALUES
(\'' . $row['id'] . ' \', \'' . $row['fname'] . ' \', \'' . $row['mname'] . ' \', \'' . $row['lname'] . ' \', \'' . $row['course'] . ' \', \'' . $row['address'] . ' \', \'' . $row['contact'] . '\')';
mysqli_query($connection, $query);
}
I am trying to remove a user from the mailing list right before I perform the emailing to my users. However, the code responsible for this within the second while loop is not executing.
Can somebody see why this is?
<?php
$query = "SELECT * FROM remove_request";
$result = mysqli_query($dbc,$query);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$query = "DELETE FROM mail_table WHERE first_name='" . $row["first_name"]
. "' AND last_name='" . $row["last_name"]
. "' AND email_address='" . $row["email_address"] . "'";
$result = mysqli_query($dbc,$query);
echo $row["first_name"] . ' has been removed from the mail list.';
}
}
$query = "SELECT * FROM mail_table";
$result = mysqli_query($dbc,$query) or die('Error querying database');
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
mail($row["email_address"],$subject,$message,$headers);
echo 'email sent to ' . $row["first_name"] . "\n";
}
}
mysqli_close($dbc);
?>
I made a function that loops through all the posts in database and echo them. But there's a slight problem. I am getting whitespace between the name (title of the post) and the content, its like two lines or so. I want reason for this and solution. Thanks.
function read_all_posts(){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM posts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<br>" . $row['content'] . "<br />" . "<i>" . $row['author'] . "</i>";
echo "<br>";
}
}
try,
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<p>" . $row['content'] . "</p>" . "<p><i>" . $row['author'] . "</i></p>";
}
How can use foreach loop to loop through the $Result?
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$Query = mysql_query("SELECT * FROM mytable");
$Result = array( );
while ($Row = mysql_fetch_array ( $Query) ) {
$Result [ ] = $Row;
}
mysql_free_result($Query);
print_r ($Result);
?>
I just have very vague idea:
<?php
foreach ($Result )
{
echo $row[fname] . ' ' . $row[lname] . ' ' $row[email];
}
?>
Could someone help please?
Something like the following
foreach($Result as $row){
$fname=$row['fname'];
// etc...
}
why do you want to do the indirect way with that array? you could instead simpply do this:
$Query = mysql_query("SELECT * FROM mytable");
while ($Row = mysql_fetch_array ( $Query) ) {
echo $Row[fname] . ' ' . $Row[lname] . ' ' $Row[email];
}
mysql_free_result($Query);
$link = mysqli_link('host', 'user', 'pass', 'db');
$query = 'SELECT * FROM mytable';
$result = mysqli_query($link, $query)
while ($row = mysqli_fetch_assoc($result)) {
$out .= $row['fname'] . ' ' . $row['lname'] . ' ' . $row['email'];
}
echo $out;