I have some troubles with visualization of the date in a query with php. Here is the code:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "alternanza";
// connessione al server sql
$conn = mysqli_connect($hostname, $username, $password, $dbname);
if (!$conn) {
die("errore nella connessione");
} else {
echo "connessione avvenuta correttamente <Br/>";
}
// recupero dati passati dal form
$nome = $_POST["nome"];
$cognome = $_POST["cognome"];
$dal = $_POST["dal"];
$al = $_POST["al"];
$query = "select NomeS, CognomeS, Specializzazione, Denominazione
from studente, azienda, attivitàformativa
where CodFiscaleS=KCodFiscaleS and CodAzienda=KodAzienda and Data_inizio='$dal'
and Data_fine='$al' and CognomeS='$cognome' and NomeS='$nome'";
$risultato = mysqli_query($conn, $query);
if (!$risultato) {
echo " errore di comando <br/>";
exit();
}
while ($riga = mysqli_fetch_array($risultato))
if ($riga) {
echo "nome: " . $riga['NomeS'] . " <br/>";
echo "cognome: " . $riga['CognomeS'] . " <br/>";
echo "periodo stage dal: " . $riga['Data_inizio'] . " <br/>";
echo "al: " . $riga['Data_fine'] . " <br/>";
echo "presso azienda: " . $riga['Denominazione'] . " <br/>";
echo "con specializzazione: " . $riga['Specializzazione'] . " <br/>";
}
mysqli_close($conn);
echo " connessione chiusa";
?>
The errors I viewed are:
Notice: Undefined index: Data_inizio(date) in
C:\xampp\htdocs\scuola18\attestato.php on line 38 periodo stage dal:
Notice: Undefined index: Data_fine in
C:\xampp\htdocs\scuola18\attestato.php on line 39 al:
Do you know something about?
You have missed Data_fine and Data_inizio in your select query. Your select query should look like this
select NomeS, Data_fine, Data_inizio, CognomeS, Specializzazione, Denominazione
from studente, azienda, attivitàformativa
where CodFiscaleS=KCodFiscaleS and CodAzienda=KodAzienda and Data_inizio='$dal'
and Data_fine='$al' and CognomeS='$cognome' and NomeS='$nome'
However, I suggest you to use parepared statements to prevent from sql injections
Related
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()){
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 made a kind of plugin just for the database connection it works but if i activate my plugin the page wil go a little bit down so actually my site will become ugly
$servername = "xxxx"; // naam of ip van de machine ... 127.0.0.1
$username = "xxxx"; //naam van gebruiker
$password = "xxxx"; // wachtwoord
$dbname = "xxxx"; //db naam
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_GET["code"]) )
{
$code = $_GET['code'];
}
else
{
$code = '';
}
//$naam = '%DR%';
//$sql = "SELECT * FROM docenten WHERE naam=\"$naam\"";
$sql = "SELECT * FROM `cadeaubon` WHERE Kortingscode like \"$code\"";
//echo $sql; //debuggen
echo "<BR>";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo "<table>";
while($row = mysqli_fetch_assoc($result)) {
echo "- ID: " . $row["ID"]. " - Kortingscode: " . $row["Kortingscode"]. " - Aantal personen: " . $row["AantPersonen"] . " - Naam: " . $row["Naam"] . " - Achternaam: " . $row["Achternaam"] . " - Leeftijd: " . $row["Leeftijd"] . " - Datum: " . $row["Datum"]."<br>";
}
echo "</table>";
} else {
echo "";
}
mysqli_close($conn);
this is my code and i thought maybe it will help if the result will display at the bottom of the page but i dont know how
I guess you left there <br> from your debug echo:
//echo $sql; //debuggen
echo "<BR>";
However I don't know where in code this gets executed and how it integrates with WordPress. If the following table is shown inside content area of the page, the <br> shouldn't affect your site design other than pushing the table one line down.
It also seems to be a bit of overkill to create your own database connection - I would think WordPress already is connected and it would be preferable to use its own connection, unless it is a completely different database.
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
So I'm new to mysqli. All of the examples I find online seem to be the old (procedural) way of doing things. Can someone tell me why my code isn't working below? My db is 'templatedb'. My Table is 'template'. I have one entry in my table, but I'm receiving no output with my echo. I'm not getting any errors with my code.
<div id="templateSelector">
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "templatedb";
//connect
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect($hostname, $username, $password, $db);
if(!$database){
die("Could not connect to the database");
}
if ($database->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
$sql = "SELECT * FROM template";
if (!$result = $database->query($sql)) {
die('There was an error running the query [' . $db->error . ']');
} else {
echo "<label>Select Template</label>";
echo "<select name='templates'>";
while ($row = $result->fetch_assoc()) {
echo "hello";
echo $row['template_name'];
// echo "<option value='" . $row['template'] . "'>" . $row['template'] . "</option>";
}
echo "</select>";
}
}
?>
Try doing the following, worked for me
<div id="templateSelector">
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "templatedb";
$mysqli = mysqli_connect($hostname, $username, $password, $db);
if($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
$sql = "SELECT * FROM template";
$result = mysqli_query($mysqli, $sql);
if(!$result = $mysqli->query($sql)) {
die('There was an error running the query [' . $db->error . ']');
} else {
echo "<label>Select Template</label>\n";
echo "<select name='templates'>\n";
while($row = $result->fetch_assoc()) {
echo "<option>id = " . $row['id'] . "</option>\n";
}
echo "</select>";
}
}
?>
</div>
Make sure your DATABASE is called templatedb and the table it is in is called template and there is a row called id. I know that sounds trivial, but spelling mistakes will break your code.
I need some help with this code:
<?php
include("config.php");
$Servico=implode(" ", $_POST['service']);
$con=mysqli_connect("","","",""); //I hid the info here
if (mysqli_connect_errno())
{
echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
}
$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome LIKE '%$Servico%'");
$row = mysqli_fetch_assoc($to);
$row['Email'];
foreach ($row as $destino)
{
echo $destino;
}
?>
The array is created in other code, by selecting one or more users that exists in the database. Then it creates the array with the users selected.
When my array have only a name, it retrieves the email correctly, but when i have more than one, it gives me:
Warning: Invalid argument supplied for foreach() in /home/a5301436/public_html/criartarefa.php on line 49
Can someone help me?
Here's how to do it using IN.
$con=mysqli_connect("","","",""); //I hid the info here
if (mysqli_connect_errno())
{
echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
}
$Servico=implode(", ", array_map(function($s) use ($con) {
return "'" . mysqli_real_escape_string($con, $s) . "'";
}, $_POST['service']));
$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome IN ($Servico)");
while ($row = mysqli_fetch_assoc($to)){
echo $row['Email'];
}