Get information from database - php

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();

Related

How can fetch a variable in sql to php according to post id? [duplicate]

I am getting an error "Fatal error: Call to undefined function fetch_assoc()". Can you please advise on how I should proceed?
The code is mentioned below
$servername = "localhost";
$username = "s";
$password = "j";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "select idnew_table, new_tablecol from new_schema.new_table;";
$result = $conn->query($sql);
$r = count($result);
if ($result['num_rows'] != 10) {
while($row = $result[fetch_assoc()]){
echo "id: " . $row["idnew_table"]. " - Name: " . $row["new_tablecol"];
}
} else {
echo "0 results";
}
$conn->close();
This line:
while($row = $result[fetch_assoc()]){
should be:
while($row = $result -> fetch_assoc()){

I have a problem to insert into mysql all data from inputs and foreach loop ( only one data enters mysql)

Just the last data enters mysql with this program :
<?php
$servername = "localhost";
$username = "bern...";
$password = "password";
$dbname = "base";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo $_POST["quantité"];
$a = $_POST["trekking"];
foreach ($a as $v) {
echo $v . "<br />\n";
$sql = "INSERT INTO Donnesmi (commentaire) VALUES ('$v')";
}
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Would you have a solution for this basic concern , thank you very much
the reason for your actual problem is
foreach ($a as $v) {
echo $v . "<br />\n";
$sql = "INSERT INTO Donnesmi (commentaire) VALUES ('$v')";
}
so you overwrite your $sql variable each time, and at the end of the loop you are left with the last value. but see the comments for the various issues/suggestions with this code
Edit: 5th Mar 2019
issue with your code was, since within for loop $sql always gets overwritten only the final $sql is executed.
As pointed out by #jameson2012, one optimum way to do this would be,
<?php
$servername = "localhost";
$username = "bern...";
$password = "password";
$dbname = "base";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo $_POST["quantité"];
$a = $_POST["trekking"];
$values = [];
foreach ($a as $v) {
echo $v . "<br />\n";
$values[] = "('$v')";
}
$sql = "INSERT INTO Donnesmi (commentaire) VALUES " . emplode(',', $values);
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
basically rather than running multiple insert statements, you build a one single statement and execute it.
========================================================================
Old Answer
You must execute SQL inside foreach. Refer below.
<?php
$servername = "localhost";
$username = "bern...";
$password = "password";
$dbname = "base";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo $_POST["quantité"];
$a = $_POST["trekking"];
foreach ($a as $v) {
echo $v . "<br />\n";
$sql = "INSERT INTO Donnesmi (commentaire) VALUES ('$v')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>

Can't echo from database to form

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();
?>

Give argument php function to show database results in another file

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 select data from database

<?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"

Categories