I'm creating a database to CSV export function.
I'm using MySQL convert function to convert the date, but it doesn't seem to be working.
The error that I get is :-
string(140) "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate from users order by userid LIMIT 0,30" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(10),registrationdate, 101) as registrationdate from users order by user' at line 1
Code
<?php
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
$query = "SELECT userid,firstname,lastname,email,CONVERT(VARCHAR(10),registrationdate, 101) as registrationdate from users order by userid LIMIT 0,30";
var_dump($query);
$result = mysql_query($query,$db) or die(mysql_error($db));
$array = array();
# Headers
$array[] = array("Serial Number","First Name","Last Name","Email","Registraion Date");
while($row = mysql_fetch_array($result)) {
// $array[] = $row;
$array[] = array($row['userid'],$row['firstname'],$row['lastname'],$row['email'],$row['registrationdate']);
}
array_to_csv_download($array,"records.csv",",");
?>
What might be the problem? Any suggestions would be helpful.
MySQL does not have CONVERT(..., 101), that's SQL Server's way of formatting datetimes.
In MySQL, you could use DATE_FORMAT('%m/%d/%Y', registrationdate);
SELECT userid,firstname,lastname,email,
DATE_FORMAT('%m/%d/%Y', registrationdate) as registrationdate
from users
order by userid
LIMIT 0,30
Related
I’m trying to use "group by" instead of "DISTINCT" in my php file to select some rows that all of them have an specific column value and it’s "idchat".
And I want to get more than one columns
please help me!
I’ve checked every pages but I didn’t understand enything
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat FROM database where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
mysqli_close($connection);?>
and this code gives me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/---/test.php on line 12
See next output:
mysql> SELECT * FROM database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> SELECT * FROM mytable;;
ERROR 1146 (42S02): Table 'test.mytable' doesn't exist
ERROR:
No query specified
mysql>
The first query is not understood because database is a reserved word. You should not use that to name a table. A work around is to add backquotes around the name:
mysql> SELECT * FROM `database`;
ERROR 1146 (42S02): Table 'test.database' doesn't exist
mysql>
"Could you correct my codes please?": Ok, but untested:
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat,id FROM `database` where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
else {
echo mysqli_error($connection);
}
mysqli_close($connection);?>
}
I also did add id to the query because of comment from #Raymond
i got a point system that are like people can upgrade to [PRO1] user. everyones rights(pro1,pro2,user) are stored in my mysql users table. But i want to make a little feed, that shows the latest one that upgraded to [PRO1]. the upgrade code:
$insert = "UPDATE `users` SET `points` = (`points`-50) WHERE `username` = '".$username."' and points > 50";
mysql_query($insert);
if (mysql_affected_rows() > 0)
{
// other codes
$insert = "UPDATE users SET rights=' [PRO1]' WHERE `username` = '".$username."'";
mysql_query($insert);
header('location: succesupgrade.php');
}else{
echo "You don't have enough points";
}
?>
the upgrade code works fine(just incase i need to add a time/date. and tha code for where i want the"'username' wast the last to upgrade to [PRO1]" is in this code:
<?php
require("dbc.php");
$query = mysql_query("select * from users WHERE rights='[PRO1]' order by right DESC limit 1") or die(mysql_error());
while($array = mysql_fetch_array($query)) {
echo "{$array['username']}<br>";
}
?>was the last to upgrade to:
<?php
require("dbc.php");
$query = mysql_query("select * from users WHERE rights='[PRO1]' order by rights DESC limit 1") or die(mysql_error());
while($array = mysql_fetch_array($query)) {
echo "{$array['rights']}<br>";
}
?>
But that code gives me this error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC limit 1' at line 1
order by right must be order by rights in the first query of the second code block.
That query is going to do nothing to tell you who the last user to upgrade to rights='[PRO1]'. That is just a string field. You would need some sort of datetime/timestamp field that is updated when the users rights change, by which you can make the sort.
You also don't need to do 2 queries. You have two queries doing the exact same thing.
Just do:
SELECT username FROM users WHERE rights='[PRO1]' ORDER BY update_timestamp DESC LIMIT 1
Where update_timestamp would be the field that is updated when the rights change.
The reason is because right is a used keyword, you need a back stroke to solve this :;
Like :
select * from `users` WHERE rights='[PRO1]' order by `rights` DESC limit 1
So I am trying to update my table based on a singe parameter:
The dateEntered field must be blank.
And I want to randomly select 50 rows, and update the blank ownerID fields to "Tester"
Here is what I have:
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "SELECT * FROM contacts WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$firstid = $row['id'];
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
}
?>
It will update a single record, then quit and give me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
The first part that selects the records works fine, its query2 that won't update all 50 records, just one. Maybe I am writing this wrong.
mysql_query needs only one time
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
to
$result2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
These answers are spot on, so I will only add some additional information, and a suggestion. When you are querying mysql the first time, $query1 is being set to the result resource, which for
$query1 = mysql_query("UPDATE contacts SET ownerID = 'Tester' WHERE id = '$firstid'");
returns a result of 1 (Boolean TRUE), which is why your second query failed, cause "1" isn't a valid mysql query string. As Greg P stated, you can fix your current script by eliminating the secondary mysql query.
However, you could improve the script entirely, and make fewer sql calls, by using this.
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "UPDATE contacts SET owenerID='Tester' WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());
I have this code:
$local_id = $_GET['id'];
$sql = dbquery("SELECT * FROM `videos` WHERE `id` = ".$local_id." LIMIT 0, 1");
while($row = mysql_fetch_array($sql)){
$video_id = $row["youtube_id"];
// the rest
}
how can i check if $local_id does not exist in the db and display an error?
mysql_num_rows
if(mysql_num_rows($sql) == 0) {
//Show error
}
$sql = dbquery("select count(*) from videos where id = ".$local_id." LIMIT 0, 1");
$row = mysql_fetch_row($sql);
if($row[0] == 0)
echo 'error';
You can use the following query:
"SELECT COUNT(*) FROM `videos` WHERE `id` = ".mysql_real_escape_string($local_id)
This query will return one number: how many records have matched your query. If this is zero, you surely know that there are no records with this ID.
This is more optimal than other solutions posted in case you only want to check for the existence of the ID, and don't need the data (if you use SELECT * ..., all the data will be unnecessarily sent from MySQL to you). Otherwise mysql_num_rows() is the best choice, as #Ryan Doherty correctly posted.
Be sure to ALWAYS escape data that came from the outside (this time GET) before you put it into a query (mysql_real_escape_string() for MySQL).
If you fail to do so, you are a possible victim for SQL Injection.
You could have a $count variable and increment it in the while loop. After the loop, check the count, if it is 0, then echo an error message.
$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%$new_title%'
GROUP BY manufacturer";
$result2 = mysql_query($How_Many_Manufacturers, $connection) or die(mysql_error());
$num_rows = mysql_num_rows($result2);
if ($num_rows == 0)
{
echo "<div id=\"noMatches\">No Matches</div>";
}
else {
}
The if statement will not work.
How can I correct this script?
#Arjan You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-25,25' at line 4 -RPM
make sure you escape $new_title in the query.
$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%".mysql_real_escape_string($new_title)."%'
GROUP BY manufacturer";
SELECT COUNT will always return a row (even if the count is zero). Simply remove the COUNT, or fetch the row to see the count.