I'm trying to query a database using a variable to detect the chosen column along with the search term.
For simplicity I removed the DB connection error handling code. The problem code is:
$find = mysqli_real_escape_string($dbc, test_input($_POST["find"]));
$field = $_POST["field"];
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE '$field' = '$find'");
//Results
while($result = mysqli_fetch_array($data)) {
echo "Result 1" . $result['number'];
echo "<br>";
echo "<br>";
echo "Result 2" . $result['description'];
echo "<br>";
echo "<br>";
echo "Result 3" . $result['contact'];
echo "<br>";
echo "<br>";
echo "Result 4" . $result['assignee'];
echo "<br>";
echo "<br>";
echo "Result 5" . $result['priority'];
echo "<br>";
echo "<br>";
}
$anymatches = mysqli_num_rows($data);
if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
I enter a term in a textbox ($find) and choose what Im searching for from a dropdown menu ($field) e.g. I select assignee.
Even though I know its in the database, I still returns the "Sorry, but we can not find an entry to match your query".
However, if I change the $field variable within the MYSQL query to an actual column name. Example:
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE assignee = '$find'");
it returns with the correct data. I even echo'd the $field variable during execution to make sure its holding the correct selection, and it does. Any ideas?
Thanks
The problem is you're surrounding the field name with quotes (') instead of backticks.
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE '$field' = '$find'");
to
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE `$field` = '$find'");
or
$data = mysqli_query($dbc, "SELECT * FROM ticket WHERE $field = '$find'");
Your current code will essentially be trying to find rows where one string matches another string, which will never be true.
Make query like
$data = mysqli_query($dbc, "
SELECT * FROM ticket
WHERE {$field} = '$find'
");
Related
Yo brothers, I am trying to echo all data from a table including column names and fields. I am able to successfully get the column names, I also want to get all the rows and fields, I am not sure if I am taking the right approach to solve my issue, please help.
//this is to echo coloumn names
$table = '<table border="1" cellspacing="10" cellpadding="10" style="width:60%">';
$table2 = "</table>";
$sql2 = "SHOW COLUMNS FROM $tablename";
$query2 = mysqli_query($conn,$sql2);
$tablename = $_POST['db'];
echo $table;
while($row = mysqli_fetch_array($query2)){
echo $row[0] . " ";
}
echo $table2 . "<br /><br />";
// successful in echoing coloumn names
// now i want to echo rows based on the number of coloumns
$countcol = mysqli_num_rows($query2);
$sql = "SELECT * FROM $tablename";
$query = mysqli_query($conn,$sql);
echo $table;
$i = "";
while($row=mysqli_fetch_array($query)){
for($i=0;$1<$countcol;$i++){
echo $row[0];
}
}
echo $table2 . "<br /><br />";
// this doenst get the all the rows based on the no of coloumns
in the final loop, is the echo $row[0] correct or should it be $row[$i]
I need to print a row from a database, i know how to print columns, but having a hard time printing rows. Can someone tell me how to?
<?php
$query = "SELECT * FROM categorias ";
$result = mysqli_query($conn, $query) or die (mysql_error());
while ($categoria = mysqli_fetch_array($result)) {
echo "<p>" . $categoria ['descricao'] . "</p>";
}
?>
This is how im printing columns
The answer is don't use SELECT * in PHP, it's extremely prone to errors. If you explicitly list the columns in your select statement you can concatenate them into a table in PHP.
Hope this helps.
Use print_r to debug selected data.
Also look for Mysql Fetch Row
Always Use Google
<?php
$query = "SELECT * FROM categorias ";
$result = mysqli_query($conn, $query) or die (mysql_error());
if(mysqli_num_rows($result)>0)
{
while ($categoria = mysqli_fetch_array($result)) {
echo "<p>" . $categoria['descricao'] . "</p>";
}
}
?>
<table><tr><?php
while ($categoria = mysqli_fetch_array($result)) {
echo "<td>" . $categoria ['descricao'] . "</td>";} ?></tr></table>
I use a table, where while the array is true places the values cell by cell in a row, because the loop is working inside the <tr> </tr> creating a new <td> for every record.
I'm having issues placing a PHP variable in MySQL string,
<?php
$con=mysqli_connect("***","***","***","***");
function getItem($itemNo)
{
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$itemNo'");
echo $itemNo;
echo "<br>";
while($row = mysqli_fetch_array($result))
{
echo $row['product_id'] . " " . $row['product_name'];
echo "<br>";
}
}
getItem(1001);
mysqli_close($con);
?>
The page shows my echo of the $itemNo, but thats all. If I just do select * from products, it gives my entire table like it should, so I know the database is working,
so I've narrowed it down to the placement of the variable.
EDIT:
product_id column is an int and also the primary key.
You can try a prepared statement to make using variables in your queries easier.
$stmt = $con->prepare("SELECT * FROM products WHERE product_id=?");
$stmt->bind_param($itemNo);
$stmt->execute();
$stmt->close();
$result = mysqli_query($con,"SELECT * FROM products WHERE product_id = " .$itemNo );
I have had a long road to get to this last question. Everything is my code is working now, but I can't get this last little issue. Right now I have:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"];
$plan = $row["currentplan"];
$remain = $plan - $val;
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
It only subtracts the first value as opposed to the values for all. displayed like this:
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>";
}
So my goal is to subtract all value_sums from all dataplans, but what I have now, gives me the first value for all columns. Thank you!
mysql_fetch_assoc() will always get one row. You can use it in loop, or better use PDO, eg. like this:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);
You can read about creating PDO connections here http://www.php.net/manual/en/book.pdo.php
I am trying to delete the records from the users table in mysql,
the code goes like this.
if(isset($_GET['id'])) {
//create query to delete the record
$query = "DELETE FROM users WHERE id =" . int($_GET['id']) or die(mysql_error());
//execute query
if($mysqli->query($query)) {
//print number of affected rows
echo $mysqli->affected_rows. " row(s) affected";
}
else {
//print error message
echo "Error in query : $query " . $mysqli->error;
}
}
else {
echo "Could not Execute the Delete query";
}
at the same time i am iterating the records from the users table in the database and it goes like this.
//query to get records
$query = "SELECT * FROM users";
//execute query
if($result = $mysqli->query($query)) {
// see if any rows were returned
if($result->num_rows > 0) {
// if yes then print one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" .$row[0] . "</td>";
echo "<td>" .$row[1] . "</td>";
echo "<td>" .$row[2] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
}
$result->close();
}
the problem is, i am able to get the records from the database and display it in the browser but when i try to delete the record the first condition does not pass i.e if(isset($_GET['id'])) instead it goes to else condition and print the message "Could not Execute the Delete query " , i guess it is not able to fetch the $_GET['id'] so only it refuses to enter the if condition,
P.S :i would appreciate if someone explains me in simple words, i am a newbie to programming, thanks..
You are missing an =:
echo "<td>Delete</td>";
HERE -------------------^
"DELETE FROM users WHERE id =" . int($_GET['id']) or die(mysql_error());
Shouldn't it be intval instead? There's no function int in PHP. There's also (less preferably) the cast to int, like this: (int) $_GET['id']).