Why am I getting white spaces from this function? - php

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>";
}

Related

Problem getting results with php and mysqli

I've a problem by running this php script:
<?php
$link = mysqli_connect("localhost", "root", "*******", "adsb");
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$query = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id='2414';";
$query .= "SELECT aircrafts.id, plane.reg, plane.hex, plane.typ, plane.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `plane` ON aircrafts.hex=plane.hex WHERE aircrafts.id='2414'";
$result = mysqli_multi_query($query);
/* execute multi query */
if ($result->num_rows > 0) {
echo "<h1>INFO ABOUT FLIGHT RECORD " . $id . "</h1>";
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
/* close connection */
mysqli_close($link);
When I run the 2 query's in phpmyadmin I get 1 result.
(When I run 1 query on my site I get a result too, see code at the bottom of this post)
But when I run it on my site it's shows "0 results".
So... There must be a fould in the $result section.
Who can help? :-)
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "adsb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id=2414";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
This is probably your specific problem.
$result = mysqli_multi_query($query);
Here is the PHP Doc
You need to establish the connection.
$result = mysqli_multi_query($link, $query);
Like the others mentioned in the comments, I would refactor this code.

Why is password_verify returning false when it's true

<?php
$server_host = "mysql-omarsdajani.alwaysdata.net";
$server_username = "usertestsest";
$server_password = "testtest";
$server_dbName = "omarsdajani_hustlehut";
$player_email = $_POST ["emailPost"];
$player_password = $_POST ["passwordPost"];
$conn = mysqli_connect ($server_host, $server_username, $server_password, $server_dbName);
if (!$conn) {
echo ("Could not connect to the server.");
}
$findPlayer = mysqli_query ($conn, "SELECT * FROM Users WHERE Email = '$player_email'");
if (mysqli_num_rows ($findPlayer) > 0) {
while ($row = mysqli_fetch_assoc ($findPlayer)) {
if (password_verify ($player_password, $row['Password'])) {
echo ("Signed in");
echo ("|ID:" . $row['ID'] . "|Email:" . $row['Email'] . "|Username:" . $row['Username'] . "|Name:" . $row['Name'] . "|Date Registered:"
. $row['Date_Registered'] . "|Goal:" . $row['Goal'] . "|System:" . $row ['System'] . "|Starting BFP:" . $row ['Starting_BFP'] .
"|Starting Weight:" . $row ['Starting_Weight'] . "|Cal Goal:" . $row ['Cal_Goal'] . "|Carb Goal:" . $row ['Carb_Goal'] .
"|Protein Goal:" . $row ['Protein_Goal'] . "|Fat Goal:" . $row ['Fat_Goal'] . "|Current Carbs:" . $row ['Current_Carb'] .
"|Current Protein:" . $row ['Current_Protein'] . "|Current Fat:" . $row ['Current_Fat'] . "|Current Cals:" . $row ['Current_Cals']
. "|Current BFP:" . $row ['Current_BFP'] . "|Current Weight:" . $row ['Current_Weight']);
} else {
echo ("Incorrect password");
}
}
} else {
echo ("Incorrect email");
}
Above is my PHP Login script, the password won't verify. I echoed $player_password and the password input from the POST is correct. The issue is the passwords won't verify. And in my DB the column length for the Password column is 600 characters. I honestly do not know what is wrong here.

PHP not displaying database on website

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 />';
}

Printing MySQL data to textbox

I apologize before I speak English very little I want to write all of the data in textboxes, but I could not do it
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vipser";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("SELECT * FROM `a_info` WHERE `pax`='" . $_GET['pax'] . "' and `nereden`='" . $_GET['durum'] . "' and `nereye`='" . $_GET['gdurum'] . "'");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while ($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['pax'] . "</td>";
echo "<td>" . $row['a_cinsi'] . "</td>";
echo "<td>" . $row['nereden'] . "</td>";
echo "<td>" . $row['nereye'] . "</td>";
echo "<td>" . $row['saat'] . "</td>";
echo "<td>" . $row['km'] . "</td>";
echo "<td>" . $row['fiyat'] . "</td>";
echo '<td><img src="' . $row["a_resmi"] . '" width="75" height="75"/></td>';
echo "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close(); ?>
Store the data of the loop in a variable instead of echoing and then assign the variable into textbox's value. Done this here:
<?php
$str = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$str .= . $row['pax'] . " " . $row['a_cinsi'] . " " . $row['nereden'] . " " . $row['nereye'] . " " . $row['saat'] . " " . $row['km'] . " " . $row['fiyat'] . " ";
}
}
?>
<input type="text" value="<?= $str ?>" />
You can put this input inside while if you want different textboxes for each row.

PHP json_encode() in while loop

I am trying to use json_encode() in a while loop while getting database results. Here is my code:
<?
$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);
$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");
while($row = sqlite_fetch_array($results)) {
$data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));
sqlite_close($database);
?>
The output of this is
{"response":"lastUserID lastUser lastXPos lastYPos"}
I want it to be...
{"response":["1 Alex 10 12", "2 Fred 27 59", "3 Tom 47 19"]}
etc.
So I want the json_encode() function to put ALL users into the array rather than the last one. How would I do this? Thanks
Try:
<?
$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);
$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");
$data = array();
while($row = sqlite_fetch_array($results)) {
$data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));
sqlite_close($database);
?>
Change this
while($row = sqlite_fetch_array($results)) {
$data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
to
$data = array();
while($row = sqlite_fetch_array($results)) {
$data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
Push each user to an array:
$data = array();
while($row = sqlite_fetch_array($results)) {
$data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

Categories