I do an INSERT into mySQL database like this:
$sDate=date("Y-m-d h:i:s");
INSERT INTO tbl_1 (datum) VALUES ('$sDate')
And it'll do the INSERT
But then when I try to find out the unique ID by selecting the date like this:
SELECT id AS nyPost FROM tbl_1 WHERE datum ='$sDate'
It will return nothing, not even an error message. The datum format is datetime.
Please whats wrong?
CODE:
<?php
$sDate=date("Y-m-d h:i:s");
echo $sDate;
If ($strNy)
{
$_nyPrSQL="INSERT INTO begagnads (`anvId`, `tabort`, `datum`) VALUES ('8' , 'null', '$sDate')";
// echo $_nyPrSQL;
if (!mysqli_query($con,$_nyPrSQL))
{die('Error: ' . mysqli_error($con));}
else
{
echo "<br>1 record added";
}
$result = mysqli_query($con,"SELECT id FROM begagnads WHERE DATE(datum) ='$sDate' ");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " " . $row['datum'];
$nyPost= $row['id'];
echo "<br>";
}
?>
MySql DATE() function Extracts the date part of the date or datetime expression expr.
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
If you want to find by DATE only
SELECT id FROM begagnads WHERE DATE(datum) = DATE('$sDate')
And if you find with date and time then no need DATE() function
SELECT id FROM begagnads WHERE datum = '$sDate'
You need this:
INSERT INTO tbl_1 (datum) VALUES ($sDate)
Try looking at your table named tbl_1 after the above, by using:
SELECT * FROM tbl_1
Related
I am trying to work with a voting database system. I have a form to display all the candidates per candidate type. I'm still trying to to explore that. But this time, I want to try one candidate type, lets say for Chairperson, I want to display all candidate names for that type in the ballot form. However, there's an error with the line where i declare $query and made query statements, can somebody know what it is. I am very certain that my syntax is correct.
function returnAllFromTable($table) {
include 'connect.php';
$data = array ();
$query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1'; //ERROR
$mysql_query = mysql_query ( $query, $conn );
if (! $mysql_query) {
die ( 'Go Back<br>Unable to retrieve data from table ' . $table );
} else {
while ( $row = mysql_fetch_array ( $mysql_query ) ) {
$data [] = $row;
}
}
return $data;
}
As #Darwin von Corax says, I sure that you have a problem between $table and WHERE
Your query:
$query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1';
If $table = 'Chairperson';
You have:
'SELECT * FROM ChairpersonWHERE candidateId=1';
The your query should be:
$query = 'SELECT * FROM ' . $table. ' WHERE candidateId=1';
i want to search data in an array .How can i search using mysql select command. i wrote the query as
$this->db->query("SELECT * FROM client_details WHERE dob IN ({implode(',', $data})");
$data is an array of dates .Please Help me for solving this..
try with
$data =array('0'=>'2015-01-23','1'=>'2015-01-22','2'=>'2015-01-21');
$tmp = implode('","', $data);
$tmp ='"'.$tmp.'"';
$sql= 'SELECT * FROM client_details WHERE dob IN ('.$tmp.')';
echo $sql;
$this->db->query($sql);
You can do something like this:
Option 1
$tmp = implode(',', $data);
$this->db->query("SELECT * FROM client_details WHERE dob IN ($tmp)");
Option 2
$this->db->query("SELECT * FROM client_details WHERE dob IN (".implode(',', $data).")");
Also, there is a mistake in your bracket formation.
It should be dob IN ({implode(',', $data)}"); [Curly braces should close after the closing parenthesis].
Cant figure out how to get this result:
ProductID Variantno
53121 5197, 5198,5199
From this data collection.
ProductID Variantno
53121 5197
53121 5198
53121 5199
Tried with group by but no good result, total sql noob...
Try this..
SELECT
ProductID,
GROUP_CONCAT(Variantno)
FROM tbl
GROUP BY ProductID
select ProductID , group_concat(Variantno)
from table
group by ProductID
Try this:
WITH cte AS (
SELECT
ProductID,
CAST('<r>' + REPLACE(variantNo, ',', '</r><r>') + '</r>' AS XML) AS VariantNos
FROM TestTable
)
SELECT
ProductID,
xTable.xColumn.value('.', 'VARCHAR(MAX)') AS VariantNo
FROM cte
CROSS APPLY VariantNos.nodes('//r') AS xTable(xColumn)
Without showing your code and database. I assumed that you are working with php and mysql.
I have run and tested this code and it works. that means it will work for you. my connection is PDO. Give me a shout if are still having issues
<?php
// pdo connection
$db = new PDO (
'mysql:host=localhost;dbname=sectona_db;charset=utf8',
'root', // username
'root90' // password
);
?>
<?php
Echo 'Data Output:<br>';
include("pdo.php");
$result = $db->prepare("SELECT * FROM product where id='123'");
$result->execute(array());
while ($r = $result->fetch())
{
//$data = htmlentities($r['product'], ENT_QUOTES, "UTF-8");
?>
<?php echo htmlentities($r['product'], ENT_QUOTES, "UTF-8");?>,
<?php } ?>
I am trying to update a table (products) from another table (table 1). The model number, however, has many symbols in it, including the '/'. The data exporting works quite fine, except the case when there is '/'. It converts it to a date instead.
E.g the following cases
11/08/1944 should be 11/08/44
11/07/1944 should be 11/07/44
The following is what I have:
$query = mysql_query("SELECT `COL 1` AS id, `COL 7` AS model FROM `table 1` ORDER BY id ASC", $c2) or die(mysql_error());
while($r = mysql_fetch_array($query)):
$id = $r['id'];
$model = $r['model'];
mysql_query("UPDATE products SET model = '".mysql_real_escape_string($model)."' WHERE id = $id", $c1);
echo "MODEL: $model AND ID: $id <br />";
endwhile;
try those :
echo $model if its right .
look your column names if they are messed up.
I want to get all the values in the database where date is between two given parameters. Following is my code. Here Date is $date1' and '$date2 are the given parameters.
$result1 = mysql_query("SELECT * FROM model WHERE Date BETWEEN('$date1' AND '$date2')");
while($row=mysql_fetch_array($result1)){
$d=$row['Date'];
echo $d;
}
You have an error in your syntax. Remove the parentheses after between:
$result1 = mysql_query("SELECT * FROM model WHERE Date BETWEEN '$date1' AND '$date2' ");
I'm guessing this is your question.