the where ___ in ____ sql command in my query using php - php

I am trying to take a user's input of #,#, or just normal statements and then pull those results from a SQL database if the search results exist in the "query" column of a table called "job" in my database. Here is the error I am getting:
Unknown column 'Array' in 'where clause'
Here's my code:
$result_all = $_POST['result_name'];
$result_all_array= explode(',',$result_all);
$query = "select last_count, query, job_id from twitterinblack46.job where
query in (".$result_all_array.") order by last_count desc;";
$result= mysql_query($query);
if($result === FALSE) {
die(mysql_error());
}
else{
//Sets up table
echo "<table border='1'
<tr>
<th>Job ID</th>
<th>Last Count</th>
<th>Result</th>
</tr>";
//Populates table
while($row = mysql_fetch_array($result)){
echo"<tr>";
echo "<td>" . $row["job_id"] . "</td>";
echo "<td>" . ltrim($row["last_count"],'0') . "</td>";
echo "<td>" . str_replace(array('%23', '%40', '%20', 'q='), array('#','#',' ',''),
$row['query']) . "</td>";
echo "<tr>";
}
echo "</table>";
Anyone know what I am doing wrong?
Update:
Here was the confusion, I did not forget to copy the $ for query. "query" is the name of the column I want to get the information from in my query. So what I am saying is, what is the difference between these two sets of code and why doesn't it work for the first query?
Code that doesn't work:
$result_all= $_POST['result_name'];
$query = "select last_count, query, job_id from twitterinblack46.job where
query in (".$result_all.") order by last_count desc;";
$result= mysql_query($query);
Code that does work:
$job_id_all= $_POST['job_id'];
$query = 'select last_count, query, job_id from twitterinblack46.job where job_id in
('.$job_id_all.') order by last_count desc;';
$result= mysql_query($query);

It is obviously a problem of the form input, being multiple checkboxes checked for example.
Try this in both queries:
$result_all= implode(',', mysql_escape_string($_POST['result_name']));
$query = "select last_count, query, job_id from twitterinblack46.job where query in (".$result_all.") order by last_count desc;";
$result= mysql_query($query);

Related

How to remove empty field after matching data?

I have created a database and and its tables. Insertion has done successfully. Now I want to get data from two tables using left join.
The problem is some fields which are not matching are empty. I want to remove the empty fields and to show the matching fields data.
How can I do this?
My code is:
<?php
include 'connection.php';
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model";
$result = mysqli_query($conn, $sql);
echo "<table border = 1px>";
echo "<tr>
<th>S/No</th>
<th>Web Name</th><th>POS Name</th> <th>POS Name</th></tr>";
$i=1;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $i . "</td>
<td>".$row['model'] ."</td>
<td>". $row['Description'] . "</td>
</tr>";
$i++;
}
}
echo "</table>";
?>
Maybe you need to modify your sql query:
//joining of two table column
$sql = "select * from oc4e_product left join item ON item.Description = oc4e_product.model where item.Description <> ''";
In this query i added filter not empty record item.Description <> ''

Using MySQL Count function

I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp

Output distinct values in SQL column with PHP

I have a table with two collumns (shortened), NAME and CATEGORY.
I want to output the number of distinct categorys. For an examle: Sport : 5 , Houses : 10.
I use this one:
$test = mysqli_query($con,"SELECT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
This work then I run the code in SQL Shell, but I have no clue on how to output it in PHP. I have searced Google up and down without any successfull solution.
Any help?
I want to output it in a table format.
EDIT: Here is my full code: (tablename is changed, and $con is removed)
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
while($row = mysql_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
echo "<table border='1'>";
while($row = mysqli_fetch_array($test)) {
echo "<tr>";
echo "<td>" . $row['lkategori'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
echo "</table>";
This will output all the categories and the count returned by the sql statement into a table. Also as a sidenote you should look into PDO.
EDIT: to make sure you do get the distinct values you should use the DISTINCT keyword in your sql statement:
$test = mysqli_query($con,"SELECT DISTINCT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
use this
while($row = mysqli_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
Thanks

Retrieve 1 value that links to all values from database

So I'm trying to display a list of all users in my database... each one with a link that will display their own information (in this case only displays user again and password), heres my code...
<?php
mysql_connect('localhost','user','password')or die ('Connection Failed: '.mysql_error());
mysql_select_db('name')or die ('Error to select database '.mysql_error());
$result = mysql_query("SELECT * FROM usuarios ORDER BY ID");
echo "<table border='0'>
<tr>
<th>UserName</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo '<td>' . $row['usuario'] . '</td>';
echo "</tr>";
}
echo "</table>";
?>
I get the ID of each user through the URL to be a new variable in my user.php page to recognize each one...
<?php
$numusu = $_GET['id'];
$result = mysql_query("SELECT * FROM usuarios WHERE id=`$numusu`");
while ($row = mysql_fetch_array($result))
{
echo "<table><tr>";
echo "<td>User:" . $row['usuario'] . "</td>";
echo "<td>Password:" . $row['password'] . "</td>";
echo "</tr></table>";
}
?>
But for some reason I'm not able to display anything in user.php, I get the ID value and all just missing the information I just get an error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /site/test/test/test/login_php/user.php on line 15
What am I doing wrong? Please help me!
The query should be SELECT * FROM usuarios WHERE id='$numusu'. Backticks only work for table and database names.
When you get Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource, it usually means $result is null and/or mysql_query failed. If you change the query to
$result = mysql_query("...") or die(mysql_error());
It should tell you that something like Unknown column '1' in 'where clause'.

Unknown column 'mush' in 'where clause'

I was testing a simple employee application and got this Unknown column 'mush' in 'where clause' error. There is someone called 'mush' in the name's column.
Here's my code
<?php
// Connects to your Database
mysql_connect("localhost", "myuser", "mypass") or die(mysql_error()) ;
mysql_select_db("peoplesdb") or die(mysql_error()) ;
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees WHERE name = $_GET[name]") or die(mysql_error());
echo "<table border=\"1\">";
echo "<tr>";
echo "<th>First Name:</th>";
echo " <td>Last Name</td>";
echo "</tr>";
echo "<tr>";
echo "<th rowspan=\"3\"><img src=\"../about/images/".$data['photo']."\" width=\"205\" height=\"205\" alt=\"\" title=\"\"></th>";
echo $data['name'];
echo "<td>".$data['name'] ."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$data['lastname'] ."</td>";
echo "</tr>";
echo "</table>";
?>
The aim was to display the detail on a table with a picture to the left of the table.
Here's how I tried calling the application:
http://localhost:8080/displaymembers.php?name=mush.
I have a table which contains these columns:
name, photo, telephone, lastname and dob.
Is there anything I'm going wrong that stops the details from displaying? I would like your help.
Helen.
First, you need to quote your inputs, second you need to escape them:
mysql_query("SELECT * FROM employees WHERE name = '".
mysql_real_escape_string( $_GET['name'] ) ."'");
You need to enclose it in quotes - e.g.
SELECT * FROM employees WHERE name = '{$_GET[name]}'
I would also suggest you use mysql_real_escape_string:
$data = mysql_query("SELECT * FROM employees WHERE name = '" . mysql_real_escape_string($_GET['name']) . "';") or die(mysql_error());
Try those put single quotes around your variable.
$_GET['name'];
And use mysql_real_escape_string to avoid SQL Injections.
you should try
$data = mysql_query("SELECT * FROM `employees` WHERE `name` = '".mysql_real_escape_string($_GET['name'])."'") or die(mysql_error());
as name may be reserved for mysql purposes, and I believe it is ...

Categories