MySQL Syntax Error with wpdb query/prepare - mysql

I'm simply trying to run a query using wpdb query and prepare statements.
On my page it returns:
[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 'myQuery' at line 1]
PHP Code:
<?php
$dirName1 = 'C:/wamp/www/c2c/wp-content/themes/flawless-v1-01';
$dirName2 = 'C:/wamp/www/c2c';
require_once($dirName1.'/config/setup.php');
require_once($dirName2.'/wp-config.php');
require_once($dirName2.'/wp-load.php');
$wpdb->show_errors();
$tableName = $wpdb->prefix . "user_orders";
$user = wp_get_current_user();
$userId = $user->ID;
$userName = $user->user_login;
// echo $tableName . ': ' . $userId . ': '. $userName;
// echo var_dump($tableName);
// echo var_dump($userId);
// echo var_dump($userName);
// These echo the correct formats for the prepare statement below
$myQuery = $wpdb->query(
$wpdb->prepare("
SELECT *
FROM $tableName
WHERE `user_id` = %d
AND `user_name` = %s",
$tableName,
$userId,
$userName)
);
$results = $wpdb->get_results(myQuery, ARRAY_A);
?>

You're missing the dollar sign before your variable name:
$results = $wpdb->get_results(myQuery, ARRAY_A);
should be
$results = $wpdb->get_results($myQuery, ARRAY_A);

Related

errors on mysql says check the manual

I've been receiving 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 'schoolPass = '300570'' at line 1.
please help here is my code for login.php.
<?php
session_start();
require_once '../cn.php';
require_once '../functions.php';
if (isset($_POST['email']) && ($_POST['password'])){
$email = sanitizeString($_POST['email']);
$pass = sanitizeString($_POST['password']);
$query = "SELECT schoolEmail,schoolPass FROM schools WHERE schoolEmail =
'$email' schoolPass = '$pass'";
$result = mysqli_query ($conn, $query) or die (mysqli_error($conn));
$count = mysqli_num_rows ($result);
if ($count == 1) {
echo "pwede na";
} else {
$err = "Invalid Log in Credentials";
};
if (isset($_SESSION['email'])){
$email = $_SESSION['email'];
echo ("hello.$email.");
};
} else {
echo "email/ password not set";
};
$conn->close();
?>
Replace your code line:
$query = "SELECT schoolEmail,schoolPass FROM schools WHERE schoolEmail =
'$email' schoolPass = '$pass'";
BY
$query = "SELECT schoolEmail,schoolPass FROM schools WHERE schoolEmail =
'$email' AND schoolPass = '$pass'";
$query = "
SELECT schoolEmail
, schoolPass
FROM schools
WHERE schoolEmail = '$email'
AND schoolPass = '$pass'
";
In between your two conditions on WHERE, it should be separated by an AND or an OR or a NOT.

Syntax error, unexpected '$query' (T_VARIABLE)

$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'];
}

PDO parameterized query on access in linux not working

This is with php , PDO on MS Access .mdb with linux drivers , have this code :
$sql = "SELECT * FROM F_ART WHERE CODART = :id";
$result = $this->cache->get($sql . $id);
if(!$result){
$stm = $this->db->prepare($sql);
$stm->bindParam(':id',$id);
$stm->execute();
$result = $stm->fetchAll();
$this->cache->set($sql.$id,$result);
}
return $result;
which output this:
Error at Line : syntax error near ?
syntax error near ?
Got no result for 'SELECT * FROM F_ART WHERE CODART = ?' command
with normal query string it works :
$sql = "SELECT * FROM F_ART WHERE CODART ='" . $id . "'";
$result = $this->cache->get($sql . $id);
if (!$result) {
$result = $this->db->query($sql)->fetchAll();
$this->cache->set($sql.$id,$result);
}
return $result;
where is the problem?

check the manual that corresponds to your MySQL server version for the right syntax to use near ' masalah = 'dsd' AND gambar = 'kerusi.JPG'' at line 1

I want to add record to a table which contain a picture. when i try to add, it shows 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 ' masalah = 'dsd' AND gambar = 'kerusi.JPG'' at line 1
so, here are the code
<?php
session_start();
include("Connections/connection.php");
$date = $_POST['date'];
$masalah = $_POST ['masalah'];
$gambar = $_POST ['gambar'];
$student_id = $_POST['student_id'];
$status = 'pending';
/*session yg di call tok lecturer tu*/
$student_id = "SELECT * FROM student WHERE student_id ='".$_SESSION['student_id']."'" ;
$result=mysql_query($student_id);
$getstudentid = mysql_fetch_assoc($result);
$student_id = $getstudentid['student_id'];
/*take 3 data from rc */
$sql = "SELECT * FROM aduan WHERE date = '$date', masalah = '$masalah' AND gambar = '$gambar' ";
$rr = mysql_query($sql) or die(mysql_error());
$tt = mysql_num_rows($rr);
if($tt > 0) {
header("Location: borang_aduan2.php?error=2");
} else { /*to check tarikh*/
$tarikh_user = strtotime($date);
$tarikh_harini = strtotime(date('Y-m-d'));
if($tarikh_user < $tarikh_harini) {
//error
header("Location: borang_aduan2.php?error=1");
} else {
//$No = $num_rows+1;
/*$sql_const = mysql_query ("Select MAX(user_name)as id from lecturer")or die (mysql_error());
$rows = mysql_fetch_array ($sql_const);
$id = $rows ['id'];*/
/*insert data*/
mysql_query("INSERT INTO aduan (date, masalah, gambar, student_id )
VALUES('$date','$masalah', '$gambar','$student_id')")
or die('Error: ' .mysql_error($conn));
echo "<script type='text/javascript'>
alert('Thanks make a report!')
location.href='borang_aduan2.php'
</script>";
}
//Freeing all memory associated with it
mysql_free_result($result);
//Closes specified connection
mysql_close($conn);
}
?>
Dont use "," in between two field selection criteria. So instead of:
SELECT * FROM aduan WHERE date = '$date',
^^
Use
SELECT * FROM aduan WHERE date = '$date' AND

Insert the values from the array into the column from the array?

I am trying to run the following but am getting the following mysql 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 'INSERT INTO hqfjt_chronoforms_data_addupdatelead SET f9f2ec4270a751f4f34980c325e' at line 2
<?php
$user = $_POST[cf_uid];
$form = $_POST[uid];
$date = date("d-m-Y");
$query = mysql_query("UPDATE hqfjt_chronoforms_data_addupdatelead SET $form = $date
WHERE cf_uid = $user
")
or die(mysql_error());
?>
what I am trying to do, is use the $USER to find the correct user record, then in that user record find the column $form and insert the $date into it,
EDIT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Ok this gets me halfway there, but still an error >>
<?php
$user = $_POST[cf_id];
$form = $_POST[uid];
$date = date("d-m-Y");
$query = mysql_query("UPDATE hqfjt_chronoforms_data_addupdatelead SET '".$form."' = '".$date."' WHERE cf_id = '".$user."'")
or die(mysql_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 ''185cfb5654aacf3038e3f26491f227356b5d768f' = '30-12-2011' WHERE cf_id = '14'' at line 1
This is not correct:
First you are trying to execute both select and insert together and second insert don't have set command ... I think you need an update command
update hqfjt_chronoforms_data_addupdatelead SET $form = $date
WHERE $user = $user
OR, I think you are trying to do something like this
INSERT INTO hqfjt_chronoforms_data_addupdatelead SELECT * FROM
hqfjt_chronoforms_data_addupdatelead WHERE $user = $user
EDIT:
Try This:
<?php $user = $_POST["cf_uid"];
$form = $_POST["uid"];
$date = date("d-m-Y");
mysql_query('UPDATE hqfjt_chronoforms_data_addupdatelead SET "$form" = "$date"
WHERE cf_uid = "$user"') or die(mysql_error()); ?>
as per your comment you'd be fine just doing a
$query = "UPDATE table SET $forms = $date WHERE cf_uid = $user";
$success = mysql_query($query);
or you can put it all into one line as well. But if you're just looking to update there's no need to SELECT ALL the data from the user. That's what the "WHERE" is for.