Mysql query issues when passing string or date - php

I have a mysql query that works fine in phpmyadmin, but does not give the expected results on my php page. I am sure that I have something wrong in the code.
Here is the php code that passes the variable
print "<td><a href='transactions_by_transaction_date.php?var=\''".urlencode(
$row['transaction_date'])."'\''>".$row['transaction_date']."</a></td></tr><tr>";
And here is where I create my query
$var = $_GET['var'];
echo $var;
$stmt2 = $db->prepare("SELECT * FROM bo_transactions WHERE
transaction_date=:var ORDER BY transaction_id");
$stmt2->bindParam(':var',$var,PDO::PARAM_STR);
$stmt2->execute();
// set the resulting array to associative
$result = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
I have the echo $var line to verify that it is passing the correct date and it appears to be. The field transaction_date is a date type.
What have I missed?

I thing you have issue in single quotes and double quotes data passing
Just use this one
print "<td><a href='transactions_by_transaction_date.php?var='".urlencode($‌​
row['transaction_dat‌​e'])."'>".$row['tran‌​saction_date']."</a>‌​</td></tr><tr>";

My apologies to everyone. The problem was not in the code posted it was a typing error in my output. Thank you to all that gave me input

Related

Display an avergae using PHP and SQL

I'm currently working on a website with a friend and I need to display the average rating for a movie.
So, I have a database with numerous columns (name, mail, etc) including "note".
My friend wrote this code :
<?php
$moyenne = "SELECT avg(note) FROM `annee_1`";
$test = $db->prepare($moyenne);
$test->execute();
$resultat = $test->fetchAll(PDO::FETCH_ASSOC);
echo $resultat;
?>
I'm not overly familiar with php or mysql. I know something is wrong (since this doesn't display a number, but just "Array"), but I don't know what.
Any suggestiong, or solution to my problem?
Thanks ! :)
You can access to your result by passing parameter to your array with a while loop. Replace your echo $result by print_r($resultat) and you can check the result you have received.
Your query will return one row with one column. An easy way to get a single value from a query like that is to use
$resultat = $test->fetchColumn();
Instead of
$resultat = $test->fetchAll(PDO::FETCH_ASSOC);
You're seeing "Array" currently because $resultat is an array (because that's what fetchAll reutrns), and when you try to echo it, it gets converted to a string. In PHP, the string representation of any array is "Array". See the documentation here:
Arrays are always converted to the string "Array"; because of this, echo and print can not by themselves show the contents of an array.
But if you use fetchColumn() instead, $resultat won't be an array. Based on your comments, it should be an int.

Why is this string operator not registering valid match with fetch_assoc()?

As the code is, I think the associative value of 'aurapass' is picking up as a string rather than the corresponding fetch value. Everything is returning positive. How do I select the fetch_assoc() value?
$recruiter=$_POST["recruiter"];
$aurapass=$_POST["aurapass"];
$recruitfetch=mysqli_query($maindb, "SELECT * FROM auras WHERE auraname = $recruiter");
$recruitcheck=mysqli_fetch_assoc($recruitfetch);
if($recruitcheck['aurapass']==$aurapass){
if($recruitcheck['recruitbadge']=="valid"){
echo "<script>alert('Recruiter badge verified.')</script>";
}
}
I have since changed the variables to be different words, so I can test the true value of the variables, and the column names, and string value of "valid" matches the table database values, so what I the problem, here?
With changes, the current code reads:
$recruiter=$_POST["recruiter"];
$recruitpass=$_POST["recruitpass"];
$recruitfetch=mysqli_query($maindb, "SELECT * FROM auras WHERE auraname = '$recruiter'");
$recruitcheck=mysqli_fetch_assoc($recruitfetch);
if($recruitcheck['aurapass']==$recruitpass){
if($recruitcheck['recruitbadge']=="valid"){
echo "<script>alert('Recruiter badge verified.')</script>";
}
}
I am struggling to receive any sort of printout, or echo, to show the values of anything. I am extremely new to this, and struggling. Unfortunately, I have not found sites that talk about exact syntax, especially with PHP7.0. Any advice on troubleshooting this would be fantastic!
Adding the quotes to recruiter did change the results, so apparently the code is correct in the second version, but I think I had some sort of character errors because I had to retype it to get it to work, but it is still the same exact text?
This is what I ended with:
$recruiter=$_POST["recruiter"];
$recruitpass=$_POST["recruitpass"];
$recruitfetch=mysqli_query($maindb, "SELECT * FROM auras WHERE auraname = '$recruiter'");
$recruitcheck=mysqli_fetch_assoc($recruitfetch);
if($recruitcheck["aurapass"]==$recruitpass){
if($recruitcheck["recruitbadge"]=="valid"){
I also figured out how to print a var_dump, and I understand its use. Thank you, all!

How to send data through anchor tag in php?

I am trying to send user_id and user_name as query string to another page through an anchor tag. I have to send two information user_id and user_name. I have tried several time but it is somewhat not working
here is my code:
$single_id = 30;
$user_name = 'jhon';
And my query string is:
echo '<a href="view-transaction.php?entry_id='.$single_id.'
">'.$user_name.'</a>';
I have to include just user_name as query string user_name.
Any kind of help will be appreciated.Thanks
You need to concat everything properly, especially the query string. Query strings start with a ? and each subsequent key value pair is appended with an ampersand &:
echo ''.$user_name.'';
You need to add the username in the query string. Like so:
echo ''.$user_name.'';

PHP MySQLi - Return a single value from a query with bound parameters?

This is probably an extremely simple question, but i just cant find the answer anywhere!
I have a query that returns one row from my database.
I'm using mysqli to bind parameters and then execute it like so:
$sql_query = $mysqli->prepare("SELECT ID FROM `Author` WHERE `Name` = ? LIMIT 1;");
$sql_query->bind_param('s', $author);
$sql_query->execute();
$sql_query->store_result();
$sql_query->bind_result($authorID);
I want to store the ID into a variable to use in another query but its not working. The only way ive got it to work is with a while loop like so:
while($sql_query->fetch()){
echo "ID: $authorID";
}
But because there is only ever one row, i dont want to use a while loop. How can i use the result variable without the while loop?
I think you just need to fetch the row from the table
$sql_query->bind_result($authorID);
$sql_query->fetch();
Now you are able to print your value
echo $authorID;

UPDATE in mysql didn't work in my PHP page.

First of all I'm a rookie to Programming, I created a PHP page to update a value from my mysql(myadmin) database, but the value is not updating. I also tried to retrieve values from database it's working just fine but this UPDATE code is not working! I don't know why, please check out my code below.
$qs=mysql_query("update staff set review=$newrate where name=$rateuser");
$resu=mysql_query($qs);
All variables are double defined, assigned with proper values, checked and I tested variables using echo, table name is also checked, it's all fine, but I think the problem is with Update query, I searched internet for the syntax but it's not different than mine. Please help me out
How are $newrate and $rateuser set?
mysql_query("UPDATE staff SET review = '".mysql_real_escape_string($newrate)."' WHERE name = '".mysql_real_escape_string($rateuser) ."'");
http://php.net/manual/en/function.mysql-real-escape-string.php
Try:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
Do not use second line.
You probably just need some " around your values $newrate and $rateuser
But if you did an echo, why not actually echo for us what the query-string becomes?
You need single quotes around string values on your query:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
(assuming both variables are strings)

Categories