How can I change the "Name of the Person" mysql column filed "updated_by"?
<?php
$updated_by=$_GET['updated_by'];
$sql = "SELECT * from neonat_patient where updated_by='Name of the Person';";
$query = $dbh -> prepare($sql);
$query-> bindParam(':updated_by', $updated_by, PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{
?>
just few changes replacing 'name of the person' with ? and some changes in bind parameters
<?php
$updated_by=$_GET['updated_by'];
$sql = "SELECT * from neonat_patient where updated_by=?";
$query = $dbh -> prepare($sql);
$query-> bindParam(1, $updated_by, PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_ASSOC);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{
?>
Related
I have the following code:
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
$flight = "SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id";
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$flight.'</td>';
echo '</tr>';
}
}
?>
I need to echo the result of the SELECT flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id query. But when I save the file, what I get on the page is a link with that query instead of the result.
Should I start a new $sql = with that query under the first $sql =?
Or is there other way?
I tried UNION and SELECT *, flight_id FROM flights INNER JOIN clients ON flights.nif_id=clients.nif_id FROM clients ORDER BY nif_id ASC; but then I get mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given.
You don't need two queries. Just use the joined query.
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT c.prm_nome, c.apelido, c.nmr_tlm, c.mail, c.nif_id, f.flight_id
FROM clients c
JOIN flights f ON f.nif_id = c.nif_id
ORDER BY c.nif_id ASC;";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
$flight = $row['flight_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$flight.'</td>';
echo '</tr>';
}
}
?>
Here I have a translator that must get data from mysql and then show them like a $_name = array ($_en, $_ru, $_ua) and then I want to choose data by cases [0] or [1] or [3]
I need to show mysql result an array
I've already try this it but it doesn't work
<?php
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
t_name = array ('t_ru', 't_ua', 't_en' )
}
?>
$result = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result))
$t_names[] = $row;
Try
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
$t_names[] = array ('t_ru'=>$row['t_ru'],'t_ua'=> $row['t_ua'],'t_en'=>$row['t_en'] );
}
or
while ($row = mysql_fetch_array($result1)) {
$t_names[] = $row;
}
now data available on $t_names
I tried it but I have to get also $t_name from mysql and then have it like an array
$result1 = mysql_query("SELECT t_name, t_ru, t_ua, t_en FROM transl_db ");
while ($row = mysql_fetch_array($result1)) {
('t_name'=>$row['t_name'] )= array ('t_ru'=>$row['t_ru'],'t_ua'=>
$row['t_ua'],'t_en'=>$row['t_en'] );
};
?>
$koneksi = mysqli_connect($host_name, $username, $password, $database);
$query = mysqli_query($koneksi, "SELECT * FROM pembeli");
$hasil = mysqli_query($query);
while ( $buyer = mysqli_fetch_assoc($hasil)){
echo $buyer ['Nama'];
echo $buyer ['Barang'];
echo $buyer ['Retribusi'];
}
I have that line of code, its produce syntax error unexpected '$query'(T_VARIABLE). Whats wrong ?
In mysqli no need to write :- mysql_select_db($database);
because fourth param in is database name
example :- mysqli_connect($localhost, $username, $password, $database);
Update your code as shown below
$database = "jaka_crud_ci";
$koneksi = mysqli_connect($host_name, $username, $password, $database);
$query = mysqli_query($koneksi, "SELECT * FROM pembeli");
while ( $buyer = mysqli_fetch_assoc($query)){
echo $buyer['Nama'];
echo $buyer['Barang'];
echo $buyer['Retribusi'];
}
I'm running a query against a database and have to check for multiple conditions to be true. The code I have pulls the data I want, but if I had to check many more columns, it could get out of control very quickly. I don't want a query that where I have to write out a dozen or so "AND" conditions. I'm learning some MySQL as I go and any help would be appreciated.
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Set up and run my Query
$sql = "SELECT Project, Client, DateReceived, LastName, FinalReviewDate FROM Projects
WHERE FinalReviewDate IS NOT NULL
AND DateDelivered IS NULL
AND DateAccepted IS NULL
ORDER BY DateAccepted DESC";
$result = $conn->query($sql);
//Display results
if ($result->num_rows > 0) {
echo "<table><tr><th>Client</th><th>Project</th><th>Point of Contact</th><th>Date Project Received</th><th>Final Review Date</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row ["Project"]. "</td><td> " . $row["LastName"]. "</td><td> " . $row ["DateReceived"]. "</td><td> " . $row["FinalReviewDate"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
You can use an array for your and conditions. But in general you have to be careful if you are going to have prepared statements and bind values. Also, you can create your WHERE clause dynamically in a similar way like the following:
EDIT:
$where = array(
array(
'column'=>'FinalReviewDate',
'operator'=>'AND',
'condition'=>'IS NOT NULL',
'value'=>'',
),
array(
'column'=>'DateDelivered',
'operator'=>'AND',
'condition'=>'IS NULL',
'value'=>'',
),
array(
'column'=>'DateAccepted',
'operator'=>'AND',
'condition'=>'IS NULL',
'value'=>'',
),
array(
'column'=>'other_column',
'operator'=>'OR',
'condition'=>'>=',
'value'=>5,
),
);
$temp = array();
foreach($where as $key => $value) {
if ($value['value'] == '') {
$temp[] = $value['operator'].' '.$value['column'].' '.$value['condition'];
} else {
$temp[] = $value['operator'].' '.$value['column'].' '.$value['condition'].' '.$value['value'];
}
}
$where_sql = '1 = 1 '.implode(' ', $temp);
echo $where_sql;
Result:
1 = 1 AND FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL OR other_column >= 5
OLD ANSWER
$ands = array(
array(
'column'=>'FinalReviewDate',
'condition'=>'IS NOT NULL'
),
array(
'column'=>'DateDelivered',
'condition'=>'IS NULL'
),
array(
'column'=>'DateAccepted',
'condition'=>'IS NULL'
),
);
$temp = array();
foreach($ands as $key => $value) {
$temp[] = $value['column'].' '.$value['condition'];
}
$and_sql = implode(' AND ', $temp);
echo $and_sql;
Result:
FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL
I'm executing a query with PDO that doesn't retrieve any result via PHP but works in phpMyAdmin.
I'm sure about the connections setting as it's not the first query of my script and the other ones work fine.
Here the PHP code :
$retour = array();
$filters = array();
$filters['media_type'] = 'mytype';
$filters['libelle'] = 'sometext';
$start = 0;
$count = 9;
$sql = "SELECT * FROM ".DB_PROD_PREFIX.$this->table." t ";
$sql .= " LEFT JOIN ".DB_PROD_PREFIX.$this->table."_lang l ON t.id = l.id AND l.langue = :langue";
$sql .= " WHERE 1";
if (count($filter)>0){
foreach($filter as $field => $value){
$sql .= " AND ".$field." LIKE :".$field;
}
}
$sql .= ($order!='' ? " ORDER BY ".$order : '');
$sql .= ($count != '' ? " LIMIT ".($start != ''?':start':'0').", :count" : '');
$stmt = $db->prepare($sql);
if($start != '') $stmt->bindParam('start', $start, PDO::PARAM_INT);
if($count != '') $stmt->bindParam('count', $count, PDO::PARAM_INT);
if ($langue != '') $stmt->bindParam('langue', $langue);
if (count($filter)>0){
foreach($filter as $field => $value) {
$f = '%'.$value.'%';
$stmt->bindParam($field, $f, PDO::PARAM_STR);
}
}
echo $stmt->queryString.print_r($filter, true);
if (!$stmt->execute()) echo $stmt->errorInfo();
$res = $stmt->fetchAll();
foreach($res as $id => $row){
$retour[]=$row;
}
return $retour;
If I unset one of the 2 keys of $filter array, the query work fine.
Here is the SQL generated :
SELECT * FROM table1 t
LEFT JOIN table1_lang l ON t.id = l.id AND l.langue = :langue
WHERE 1
AND media_type LIKE :media_type
AND libelle LIKE :libelle
ORDER BY position LIMIT :start, :count
Thanks for your help!
Pierre M.
It doesn't work better but thanks for the tip!
Here is a piece of code managing the LIKE parameters that works :
$media_type = '%'.$filter['media_type'].'%';
$stmt->bindParam('media_type', $media_type, PDO::PARAM_STR);
$libelle = '%'.$filter['libelle'].'%';
$stmt->bindParam('libelle', $libelle, PDO::PARAM_STR);
Why doing the same with a foreach on the array doesn't work ?
if (count($filter)>0){
foreach($filter as $field => $value) {
$value = "%$value%";
$stmt->bindParam($field, $value, PDO::PARAM_STR);
}
}