So I want to echo a specific value from my database, but it doesn't wok. I'd like to echo that value into a text form something like this:
<h6 class="brand-before" align="center"><small>Konyhakész fa</small></h6><br>
<input type="text" name="konyha" size="18" align="center" value="<?php echo $konyha; ?>" />
And this is how I connect to MySQL:
<?php
mysql_connect("localhost", "koristuzep", "***") or die("Kapcsolódás az adatbázishoz sikertelen.");
mysql_select_db("koristuzep")or die("Kapcsolódás az adatbázishoz sikertelen.");
$konyha = mysql_query("SELECT * FROM $arak WHERE ID=3")
?>
you should fetch the results first from $konyha as given in this example
<?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 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"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
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();
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">';
}
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.
<?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"
I am trying to use PHP in two different parts of a page. But when I try to load the page I receive the error: undefined variable $conn. Why am I getting this error?
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\"
class=\"amount-type\"
placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
You use $conn at the top of your script
$result = mysqli_query($conn, $sql);
Yet you define it further down
$conn = mysqli_connect($servername, $username, $password, $dbname);
You'll need to move that section up to the first before you can run any queries.
<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
You're defining and connecting to the database after you try to use it. Move the connection part above the query. If this is the only place that you will be using the connection the page then this is fine. But if other pages or sections of the page will use the connection you should look at placing this in a common file that is included at the top of every page that uses it.
<tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
I'd be pretty sure that you need to define $conn before you call it in the top part of the php code.
PHP is loaded and executed sequentially... You are defining $conn after using it. Try doing it before. I.e.: Move the second blob of PHP above the first one.
For more on PHP execution order, check out this question.