This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 6 years ago.
I was using mysqli to retrieve a blob image from MySQL, but I've been moving everything over to PDO. As far as I can tell, this should be working. I've searched and searched the forums for the answer, but I can't see why this doesn't work.
// Connection
include 'pdo_db.php';
// Get Image Id for DB Query
$recipe = $_GET['recipe'];
// Execute Query<br>
$query = $db->prepare("SELECT * FROM myrecipes WHERE Id = ?");
$query->execute(array(
"Id" => $recipe
));
// Display Image
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image/jpg");
echo $image;
I make two assumptions: 1) your id is numeric integer, and 2) you'd only need to retrieve 1 row at a time (1 per id).
$recipe = (int)$_GET['recipe'];
$query = $db->prepare("SELECT * FROM myrecipes WHERE Id = ? LIMIT 1");
$query->execute(array($recipe));
You are referencing the array in execute as an [id]=>$recipe but this is not needed. You only need as a minimum a numeric reference with is handled automatically.
You should not do SELECT * but instead specify only the column you want, rather than multiple table-columns.
Related
This question already has answers here:
How to check if a row exist in the database using PDO?
(3 answers)
Closed 9 months ago.
I want to show data input is already exist array
<?php
if(isset($_POST["med_nom"]))
{
$connect = new PDO("mysql:host=localhost;dbname=dbs", "", "password");
for($count = 0; $count < count($_POST["med_nom"]); $count++)
{
$query = "INSERT INTO medicinetbl (med_dorder, med_id, med_nom, med_qty, med_prc) VALUES (:med_dorder, :med_id, :med_nom, :med_qty, :med_prc)";
$statement = $connect->prepare($query);
$statement->execute(
array(
':med_dorder' => $_POST["med_dorder"][$count],
':med_id' => $_POST["med_id"][$count],
':med_nom' => $_POST["med_nom"][$count],
':med_qty' => $_POST["med_qty"][$count],
':med_prc' => $_POST["med_prc"][$count]
)
);
}
$result = $statement->fetchAll();
if(isset($result))
{
echo 'ok';
}
}
?>
This is how I check for duplicate Row ID's in my table:
# Generate random Row ID
$rid = rand(1000, 9999);
# start query (yours will be different as I have a class created for starting
# connections with PDO; nonetheless, you should still understand what im doing here)
$query_ridcheck = $db->connect->prepare("SELECT * FROM `table` WHERE rid=:rid");
$query_ridcheck->bindValue(':rid', $rid, PDO::PARAM_STR);
$query_ridcheck->execute();
# if it fetches any data back (while searching for the newly created $rid),
# then generate another random $rid again and check again. This while loop will
# keep going until it DOESN"T fetch any data back (meaning the random $rid has
# not been taken already).
while($query_ridcheck->fetch(PDO::FETCH_ASSOC)){
$rid = rand(1000, 9999);
$query_ridcheck = $db->connect->prepare("SELECT * FROM `table` WHERE rid=:rid");
$query_ridcheck->bindValue(':rid', $rid, PDO::PARAM_STR);
$query_ridcheck->execute();
}
Essentially, you can check for duplicate data before inserting, and choose what todo. I choose to regenerate the data and check again until I got a string/integer that wasn't a duplicate.
After this check, I go onto insert the data into the table using that row ID (since I now know it will not be taken)
Or like #Your_Common_Sense graciously pointed out, instead of repeating code you can also perform the same function like this:
# executes at lease once, then checks if the condition
# is true; If it is, continue looping until it's false.
do{
$rid = rand(1000, 9999);
$query_ridcheck = $db->connect->prepare("SELECT * FROM `table` WHERE rid=:rid");
$query_ridcheck->bindValue(':rid', $rid, PDO::PARAM_STR);
$query_ridcheck->execute();
}while($query_ridcheck->fetch(PDO::FETCH_ASSOC));
You should search into your data for the values you dont want to be repeated, if there is a record with the values, do not insert.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 1 year ago.
Basically, I try to fetch player id
it outputs id 17 it's a correct number
and I'm pretty sure it's correct to there. Then use these values to get a list of trade info.
The database looks like this:
The PHP function that I call
function listtrade() {
global $db;
$damnid = usertouserid($usernick);
//SQL queries really suck dude
$dt = "SELECT * FROM trades WHERE tradereceiver = '$damnid' ORDER BY tradeid DESC";
$dbdata = array();
$tsr = mysqli_query($db, $dt);
while ($dbdatadt = mysqli_fetch_assoc($tsr)) {
$dbdata[] = $dbdatadt;
}
return $dbdata;
}
Steps I tried
adding and removing '
removing order by
but it does not solve, any reason this happens?
It looks very dumb for me, it should run correctly, the syntax is correct.
Check below things:
1. global $db;->print this, check the connection is correct?
2. $damnid = usertouserid($usernick);-> print this getting value 17?
3. Check table name 'trades' exists in database?
4. $usernick is not defined in your code, it seems?
This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I'm trying to get values from the database depending on the url. So, in my db I have a column with a keystring that is a unique number that I am generating, what I want to do is: From the URL, like "example.com/?keystring=dss76da1" I can get the column value 'email' associated with this keystring and use it in the current page with a variable.
My sql table
I started the code that way but I don't know if it's right.
(URL: example.com/?keystring=dss76da1):
<?php
$key = intval($_GET['keystring']);
$results = mysql_query("SELECT * FROM users WHERE keystring='".mysql_real_escape_string($key)."'");
while ($row = mysql_fetch_array($results))
What should I do to get the value of the email column that is associated with the keystring and put it in a variable? So I can use on the current page..
<?php
$key = $_GET['keystring'];
$results = mysql_query("SELECT * FROM users WHERE keystring='".mysql_real_escape_string($key)."'");
while ($row = mysql_fetch_array($results)) {
}
?>
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 3 years ago.
I made a custom function to select data from database using WP methods. It looks like this;
function CheckUser($name){
/*DB connection */
$result = $mydb->get_results("SELECT *FROM users WHERE name = ".$name.";");
if(!empty($result)){
echo "OK!";
}
}
And here is a part in code:
/*This part is in foreach so thats why I am using $obj
$name_string = $obj->first_name." ".$obj->surname;
Prombutne($name_string);
But every time I not receive anything from DataBase, when I change !empty to empty I get OK so Select function always return's empty.
Replace
$result = $mydb->get_results("SELECT *FROM users WHERE name = ".$name.";");
with
$result = $mydb->get_results("SELECT * FROM users WHERE name = '".$name."'");
You have to give space after '*' and before 'table_name' and in where condition you are using name field which is string so you need to pass it in quotes.
This question already has answers here:
CodeIgniter - return only one row?
(11 answers)
Closed 9 years ago.
Having trouble with Codeigniter. I am trying to fetch the value of a product from mysql. The mysql table contain a field called cost_price. I am trying to fetch the result of a single product using the product ID. Here is the code im using,
$query = $this->db->query('SELECT * FROM items WHERE id="$id"');
$row = $query->result();
echo $row['cost_price'];
But nothing is happening ! What is the problem with my code?
I tried some wrong code to check whether the database is responding or not, but it seems that the database is working fine. Can't understand why my query can't find any result !
Try something like this:
$row = $this->db->get_where('items', array('id' => $id))->row();
Or, if you just need the price:
$price = $this->db->select('cost_price')
->get_where('items', array('id' => $id))
->row()
->cost_price;
EDIT
Your method was ok up to a point, look:
$query = $this->db->query('SELECT * FROM items WHERE id="$id"');
$res = $query->result(); // this returns an object of all results
$row = $res[0]; // get the first row
echo $row['cost_price']; // we have an object, not an array, so we need:
echo $row->cost_price;
Alternatively, if you want an array, you can use result_array() instead of result().
My way, row(), returns only the first row. It's also an object, and if you need an array, you can use row_array().
I suggest you read more about all that here.