Select only rows with certain critiera then output data - php

So im developing a web page with the following sql query:
$sql=mysql_query("SELECT * FROM `fotf_images` WHERE `image_fotfnum` = '$Fivedigits'");
now, $fivedigits is a $_POST from a previous forms input data. So basically the form parses the mysql db for rows that contain ONLY $Fivedigits in a specific column. What i want to do, is output EVERY row that has these criteria. So far i used the following:
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
print_r($row);
}
this only seems to output the first row, when i know as a fact there are exactly 2 rows that contain the criteria. Please help! Thanks!

try using
Select * from fotf_images
where Concat(image_fotfnum, '', field2, '', fieldn)
like concat('%','",$Fivedigits,"','%')
since its not only numeric you may need to use LOWER or UPPER case while comparing. This will help you search in every mentioned feilds of fotf_images table

Related

How to select rows from different database tables without joining them in any way and show results in the loop

I have two mysql tables. I want to get the data from these tables and show it in a loop. The data is totally unrelated to each other and should stay that way. I just need to show data from these two different tables in the same place.
I tried the mysqli_multi_query, but I couldn't show the results from an individual column like I can with a normal query.
For each of these two tables, I need 2 SELECT statements with two WHERE clauses. Does anyone know how to do this?
I've tried all different ways of trying to get the info from both tables and just show them in one loop. I've tried mysqli_multi_query, but don't know how to save specific column results in a variable.
$sql = "SELECT *
FROM misc
WHERE height LIKE '$height_input'
SELECT *
FROM bolts
WHERE name LIKE '$bolt_name_input'
;
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$height_row = $row['height'];
$bolt_name_input = $row['name'];
?>
Height Row: <?php echo $height_row; ?>m<br />
bolt Name: <?php echo $bolt_name_input; ?><br />
} //while
My error message is generally "Trying to get property 'num_rows' of non-object".
Yes everyone, I have taught myself PHP and MySQL through online tutorials and have not had a lot of experience with them yet. I apologise, I am still getting the hang of this.
I have just worked out now by trial and error that I can show information from a table outside of the while loop. I had no idea I could do this! Which changes everything. So I can select information from various database tables using different SELECT statements and then echo them all into one place in my HTML table underneath it on my web page. The results don't have to be shown in the while loop, they can be outside it.
So my structure now looks like this basically:
SELECT from table 1 column 1 WHERE (form input value)
num rows
while loop
$height = $row_height['height'];
end num rows
end while loop
SELECT from table 2 column 1 WHERE (form input value)
num rows
while loop
$bolts = $row_bolts['bolts'];
end num rows
end while loop
<table>
<tr>
<td>echo $height</td>
<td>echo $bolts</td>
</tr>
</table>
So, this is working for me to retrieve multiple results from my different tables. I have to change the variable names of the sql statement each time, I guess I could do it with a function so i can repeat it and make it look neater, but this is working for me this way. So, with bolts I am using the variable names $sql_bolts, $result_bolts and $row_bolts and with height I am using variable names $sql_height, $result_height and $row_height and so on.
$bolts_input = $_POST['bolts'];
$sql_bolts = "
SELECT *
FROM bolts
WHERE name LIKE '$bolts_input'
;";
$result_bolts = $conn->query($sql_bolts);
if ($result_bolts->num_rows > 0) {
// output data of each row
while($row_bolts = $result_bolts->fetch_assoc()) {
$bolts = $row_bolts['name'];
} //bolts while
} // bolts num rows
I am not using mysql, but mysqli. I did a whole bunch of tutorials on PDO and how to connect to the database and retrieve information and just couldn't figure out how to show values from a row like I can with the above. It's frustrating because I want to use the latest methods, but I can't find how to online that makes sense to me.
Thank you for eveyone's comments so far.

MYSQL Skip the fetching of a column after query

I need to run queries in mysql and dislay the output in some way, however the queries are provided by a third party and I can't change them. An example would be:
SELECT j.`First Name`, j.`Last Name`, c .*
FROM PersonalInfo j
INNER JOIN
Evaluations c
ON j.UIN = c.UIN
Where table PersonalInfo has people's personal info and table Evaluations stores people's evaluations. The Evaluations table contains blob fields where the (sometimes heavy) pdf's of the evaluations have been stored.
The problem with this query is that since Evaluations have blob fields and these are selected in the SELECT statement I have no option than to read those fields when I do $result->fetch_assoc() as below
if ($result = $mysqli->query($DTIS->query)){
while ($row = $result->fetch_assoc()) {
// at this point I believe the (heavy) blob info has been fetched and put into $row["blobfield"] right ?
// if field is blob display just a link, otherwise the info.
}
}
Is there a way to SKIP THE FETCHING of a specific COLUMN so I can display the table quickly without reading any of the heavy blob info ? Remember I can't change the query string. Any code template is also appreciated it.
Thanks for the help.
If queries are provided by a third party and you can't change them - no.
You can use below way to access your data field wise, however it is some what lengthy but may be useful to you:
First find the number of rows for that query // which you can get using mysql_num_rows
Loop using that number in below way, to get only selected field data one by one
$result = mysql_query($query);
$count = mysql_num_rows($result);
$i = 0;
while ($i < $count) {
$firstColumnData[] = mysql_result($result, $i, 'first_field_name');
$secondColumnData[] = mysql_result($result, $i, 'second_field_name');
$i++;
}
In this way, you may get only desired field data from mysql result set.

PHP MySQL While loop for SELECT from two tables?

Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.

Get row numbers from mysql_fetch_array result

I have pulled in the data from a mysql database using select * with the intention of using the data several times without doing repeated sql enquiries using WHERE.
Using this data I am extracting rows that contain a search element using
while($row=mysql_fetch_array($query_result)){ <<<if match add to new array>>> }
As there are thousands of rows this is taking a longer time than I want.
I am trying to use:
$row=mysql_fetch_array($query_result);
$a = array_search($word_to_check, $row);
echo $a;
This extracts the correct sql headings but not the row number. What I want to achieve is
if $word is found in mysql_fetch_array($query_result) the add the row where it was found into the new array for processing.
Any thoughts? Thanks in advance.
Don't use mysql_* functions they are depracated. Use mysqli or pdo instead.
It's not wise to search in array of mysql results in php while it can be done in mysql. Let's say you have table and you want to find all numbers in number column that are greater than 5
SELECT FROM table_name WHERE number>5
to find text you can use simple clause
SELECT FROM table_name WHERE name = 'username'
You can also create more complex conditions.
From MYSQL manual:
WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected. where_condition is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no WHERE clause
Check this link
If you want to limit the query to only once, fetch all the results into temporary array and do the search from it like below
<?php
$all_rows=array();
$match_rows=array();
$i=0;
$limit=100000;
while($row=mysql_fetch_array($query_result)){
$all_rows[]=$row;
if($i % $limit == 0){ // this part only functions every 100,000 cycles.
foreach($all_rows as $search_row){
if(array_search($word_to_check, $search_row)
$match_rows[]=$search_row;
}
$all_rows=array();//reset temporary array
}
$i++;
}
//This solution assumes the required word can be found in mulitple columns

only retrieving 1 row of data from MYSQL database

I am trying to input multiple pieces of data through a form and all the data will be separated by (,). I plan to use this data to find the corresponding id for further processing through an sql query.
Below is the code I use.
$key_code = explode(",", $keyword);
//$key_count = count($key_code);
$list = "'". implode("','", $key_code) ."'";
//$row_count = '';
$sql4= "SELECT key_id FROM keyword WHERE key_code IN (".$list.")";
if(!$result4 = mysql_query($sql4, $connect)) {
mysql_close($connect);
$error = true;
}else{
//$i = 0;
while($row = mysql_fetch_array($result4)) {
$keyword_id[] = $row['key_id'];
//$i++;
}
//return $keyword_id;
}
The problem i see is that keyword_id[0] is the only element that contains any data (the data is accurate). Even if I input multiple values through the aforementioned form.
I thought it might be an error in the sql but I echo'ed it and it looks like:
SELECT key_id FROM keyword WHERE key_code IN ('WED','WATER','WASTE')
The values in the brackets are exactly what I inputted.
I even tried to figure out how many rows are being returned by the query and it shows only 1. I assume something is wrong with my query but I cannot figure where.
Any help will be greatly appreciated.
Edit: Alright Solved the problem. Thanks to suggestions made I copied and pasted the $sql_query I had echo'ed on the website into mysql console; which resulted in only 1 row being retrieved. After taking a closer look I realized that there was a whitespace between ' and the second word. I believe the problem starts when I input the key_code as:
WED, WATER, WASTE
Instead inputting it as
WED,WATER,WASTE
fixes the problem. I think I should make it so that it works both ways though.
Anyway, thank you for the help.
I am pretty sure that the query is ok. How many rows do you get with just
SELECT key_id FROM keyword
I think that there is just one line that matches your WHERE.
Check the query directly in the database(with phpmyadmin, or in the mysql console), however this query seems to be working as you may assumed. If it returns only 1 row when you use it directly in the db, then maybe there is only one row in your table wich matches this query.

Categories