PHP MySQL Query Where X = $Variable

Use PHP variables in MySQL Query with LIKE '%%'

Well first of all, I am guessing you are getting a MySQL syntax error when trying to execute that first query. This line:

WHERE books.BookName LIKE  '%''".($_POST['find'])."''%'

Should be

WHERE books.BookName LIKE  '%".$_POST['find']."%'

Because right now you are getting

WHERE books.BookName LIKE  '%''ABC''%'

when you should be getting

WHERE books.BookName LIKE  '%ABC%'

I don't admit to understand what you are doing with your second query, which just hard codes and has %% as one of the search criteria, which is, in essence meaningless.

Upload a variable to sql query

I suggest defining your post variable

$postVar = $_POST['variable'];

Then you should probably sanitise it before putting it in your SQL string.

I would prefer to use Prepared Statements.
You should read about them here https://www.w3schools.com/php/php_mysql_prepared_statements.asp and use them instead as it fits your case exactly

Edit: How did you determine that your SELECT query didn't execute?

Edit 2:
So I tested out modified version of your code:

if(isset($_GET['name'])){
$query = "INSERT INTO TestTable (name) VALUES ('$_GET[name]')";
echo $query;
echo "</br></br>";
}

$var = "$_GET[name]";
echo $var;
echo "</br></br>";

$query = "SELECT * FROM TestTable WHERE name = '$var'";
echo $query;

The output that this produced by going to fileName.php?name=getVariableCorrectlyPopulated is:

INSERT INTO TestTable (name) VALUES ('getVariableCorrectlyPopulated')

getVariableCorrectlyPopulated

SELECT * FROM TestTable WHERE name = 'getVariableCorrectlyPopulated'

As you can see that is with GET instead of POST but data is going in correctly, I suggest you do the same or similar when you first make your scripts.You can use XAMPP (for example) for Apache distribution and run your scripts locally.

Edit 3+4:

Tested and is working:

<?php
if(isset($_POST['name'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);

$array=array();
$postVar = $_POST['name'];
//DONT FORGET TO SANITIZE

$query1 = "INSERT INTO TestTable (name) VALUES ('$postVar')";
if(mysqli_query($conn,$query1, false)){
// echo 'Data Submit Successfully';
}else{
echo 'Try Again';
}

$query2 = "SELECT * FROM TestTable WHERE name = '$postVar'";
$result = mysqli_query($conn, $query2);
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row['name'];
}

echo json_encode($array);
}
?>

Please keep in mind there are way better ways to do this... See Prepared Statements above

Edit 5:
You should change your code to what I gave you in Edit 3+4 (and sanitise the post data as well) and edit your android code so that you handle the response fromdoInBackground method https://developer.android.com/reference/android/os/AsyncTask.

Comparing a PHP variable to values in MySQL column

Inside $sqlItemId you have the full table row (if any), not only its ID; change the SQL into a count and check the number of rows returned (if greater than 0 you have a duplicate):

$rowsCount = $dbCon->query("
SELECT COUNT(*)
FROM CVCinStoreCoins
WHERE itemId = '$itemId'
");

I don't know what $dbCon is (Doctrine DBAL? mysqli?) so I can't tell you how to use query's result.

How do I save individual MySQL results to PHP variables?

I figured it out! I was able to assign the results from my database query into variables that I could then recall through a simple <?php echo $variable; ?> at any time, as many times as needed. Here is what I did.

SQL Example

$result = $db->query('Select `Name`, `Age`, `Color` from `People` ');
while ($rows = $result->fetch()){
$Name = $rows['Name'];
$Age = $rows['Age'];
$Color = $rows['Color'];
}

In Webpage

<p>
Hello, my name is <?php echo $Name; ?>.
I am <?php echo $Age; ?> years old and my favorite color is <?php echo $Color; ?>.
</p>

Query in a class with variable

Thanks for the attempt to help @Ohgodwhy. $clocked_in was returning an array because I asked it to select all columns in the table. So when there was a result, it was an array. I changed the return of the function to return true instead of $result[0] because I only need to know if the user is logged in. I could have probably just changed the query to select that column as well. After doing that, it worked great until I provided a value for the table field (Making the user clocked_in). I then got a Undefined offset:0 error because I was trying to call the value of $result[0] when there was no array indexed because the query obviously returns array(0); I just changed the count to check to see if $result exists.

updated code is as follows in case someone comes across this

Timeclock Class

class Timeclock {
public $user_id;
public function __construct($user_id) {
$this->user_id = $user_id ;
$this->db = new Db();
//$this->clocked_in = is_user_clocked_in($user_id);
}
public function is_user_clocked_in(){
$result = $this->db->query("SELECT * FROM timeclock WHERE user_id = :user_id AND time_out IS NULL", array("user_id"=>$this->user_id));
if ( count ($result) > 0 ){
return true;
}else{
return null;
}

}
}


Related Topics



Leave a reply



Submit