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)) {
}
?>
Related
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:
Normalizing MySQL data
(4 answers)
Closed 3 years ago.
I am storing list of property types like Apartment, Penthouse etc for a given property in an array in a table. Now I want to display all the fields from that table whenever someone searches for Apartment or Penthouse.
Till now i have inserted the list of values using serialize into the table. An also able to fetch values using deserialize. Now i want search for particular value from that array. If value found, i need to display all details from that row.
if($_POST['action'] == "add_property"){
$ptype = serialize($_POST['ptype']);
$proomtype = serialize($_POST['proomtype']);
$query2 = query("SELECT * FROM properties");
confirm($query2);
while($row = fetch_array($query2)) {
$ptype = unserialize($row['ptype']);
$proomtype = unserialize($row['proomtype']);
echo "ptype: $ptype proomtype: $proomtype";
}
}
I'm a beginner to MySQl. Any help would be much appreciated. Thanks in advance. Also if there is any other simplest way where i can store a list of values in a table, fetch them, compare them and delete them. Would be much appreciated.
This will return if all values match.
SELECT * FROM properties
WHERE column1 LIKE
'%serialsize($ptype)%'
OR column2 LIKE
'%serialsize($proomtype)%'
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.
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.
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 9 years ago.
how do I get the output from this:
$VisitorsProfile = $mysql->query("SELECT SUM(views) FROM visitors_profile WHERE publisher = '123456'");
I tryed this but nothing prints me out!
<?php echo $VisitorsProfile['sum'] ?>
Any suggestions?
you need to name the column, I know it sounds weird but it should be
$VisitorsProfile = $mysql->query("SELECT SUM(views) as totals FROM visitors_profile WHERE publisher = '123456'");
and as indicated as below you need the mysql_fetch_assoc function on the query so you can reference it by name.
Then you can reference it by
<?php echo $VisitorsProfile['totals'] ?>
you just need to reference it "as" something. whatever you put in after the word as is what you can refer to it by, i wouldn't use something like "sum" as that is a mysql function and can cause problems.
PHP allows you to get the information by column name or by column index. The recommended way is to do it by column name, because this will allow you to change your query's column order without affecting your PHP code.
So to get it you need to add a column alias to your query:
$rs = mysql_query("SELECT SUM(value) AS total FROM visitors_profile WHERE publisher = '123456'");
if ($row = mysql_fetch_assoc($rs)) {
echo $row['total'];
}
On the first line you are actually running the query. Check the column alias: "total".
On the second line you are asking for an array or specifically a "map" from the result set. In other words, by calling mysql_fetch_assoc you are asking for an array whose elements are available by name. That array represents a row from your result set.
On the third line you are printing the actual sum.
Check the "if" in the $row assignment. This will help you validate there is a result row before trying to print it. I recommend you to do the same on the line where you call mysql_query.
EDIT:
Sorry, I think you are using objects, so it should look like this:
$rs = $mysql->query("SELECT SUM(value) AS total FROM visitors_profile WHERE publisher = '123456'");
if ($row = $mysql->fetch_assoc($rs)) {
echo $row['total'];
}