Can anyone help please?
This is my code.I want to add an alt option for images but everything I try is throwing errors.
I have tried studying the php handbook and have tried copying code from other questions but so far no luck.
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2";
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src='$row['ming']'>';
}
} else {
echo "0 results";
}
$conn->close();
?>
You have a string concatenation problem :
echo '<img src="' . $row['ming'] . '">';
Will output : <img src="myimage.jpg">.
To add a alt attribute, you could do :
echo '<img src="' . $row['ming'] . '" alt="' . $row['name'] . '">';
Will output something like
<img src="myimage.jpg" alt="image name">
change your code like this:
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2"
;
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src="'.$row['ming'].'" alt="'.$altText.'">'; //change here
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem was that you did not have an alt attribute in your echo statement and the second was your concatenation was a little off with misplaced quotes.
while($row = $result->fetch_assoc()) {
echo '<img src=' . $row['ming'] . 'alt="Your alt message">';
}
Related
When I click on artikle It needs to diretct me to page.php where I display whole article.
Problem is im not sure how to with $_GET superglobal var properly take information. I have to get ID with $_GET.
I already have included database in my index.php where I displayed several articles.
Im sending id like this:
echo '<a href="clanak.php? class="form-field-textual" id='.$row['id'].'">';
//article.php
<?php
$servername = "localhost:3306";
$user = "root";
$pass = "";
$dbo = "projekt";
// Create connection
$conn = new mysqli($servername, $user, $pass, $dbo);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM projekt";
if ($conn->query($sql)===TRUE) {
$specID=$conn->insert_id;
echo "id: " . $row["id"]. " - Name: " .
$row["kategorija"]. " " . $row["naslov"]. "<br>";
} else {
echo "0 results";
}
$conn->close();
?>
$row_id = $row['id'];
echo 'some text';
<?php
$servername = "localhost:3306";
$user = "root";
$pass = "";
$dbo = "projekt";
// Create connection
$conn = new mysqli($servername, $user, $pass, $dbo);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = isset($_GET['id']) ? (int)$_GET['id'] : '';
$sql = "SELECT * FROM projekt WHERE id = $id";
if ($conn->query($sql)===TRUE) {
$specID=$conn->insert_id;
echo "id: " . $row["id"]. " - Name: " .
$row["kategorija"]. " " . $row["naslov"]. "<br>";
} else {
echo "0 results";
}
$conn->close();
I am currently looking to display an image on my website using PHP. The image data is in MySQL, and I know how to display data from MySQL, but I can't figure out how to display images using insertname.png type data.
My code is currently this: (In pet_url, it basically states dogpuppyrare.png, which is a png inside my public_html inside my file manager on my hosting website.
<?php
$servername = "localhost";
$username = "dbuser";
$password = "dbpass";
$dbname = "db name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT pet_url FROM user_pets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["pet_url"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The fact the field is labelled ['pet_url'], I'm making the assumption that the data in this key is a URL, so just display the src using the HTML img tag.
if ($result->num_rows > 0) {
//output data of each row
while ($row = $result->fetch_assoc()) {
echo '<img src="' . $row["pet_url"] . '" alt="my pet image"/>"<br>"';
}
}
You can read more about image tags at w3schools:
Convert this line:
echo $row["pet_url"]. "<br>";
To this;
echo '<img src="'. $row["pet_url"] . '" alt="my pet image" /><br>';
I have this php code to connect database it works fine.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cv";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM services";
$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["service_name"]. " " . $row["service_desc"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My question is: how to create function getServices() and give argument to show this results in another php file using foreach or while, like this:
<?php foreach ($results as $key=>$result) : ?>
<?php echo $result['service_name']; ?>
<?php endforeach; ?>
Okay. So you have to do the following things:
db.php :
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cv";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function yourFunctionName() {
$sql = "SELECT * FROM services";
$result = $conn->query($sql);
return $result;
}
$conn->close();
otherfile.php :
<?php
include 'db.php';
$data = yourFunctionName();
if ($data->num_rows > 0) {
// output data of each row
while($row = $data->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["service_name"]. " " . $row["service_desc"]. "<br>";
}
} else {
echo "0 results";
}
?>
Hope this works, haven't tested yet.
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; ?>
<?php
$servername = "localhost";
$username = "root";
$password = "william";
$dbname = "camping";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT fnavn,enavn,epost,tlf FROM knr ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["fnavn"]. " - Name: " . $row["enavn"]. " " . $row["epost"]. "ire: " . $row["tlf"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Tried it, and it doesnt work, anyone who knows why? It's something wrong with the second if statement I think
You would need to join the tables:
SELECT * FROM produkt
LEFT JOIN produkttype ON (produkt.ptype = producttype.ptype)
WHERE utleid="No"