MYSQL Skip the fetching of a column after query - php

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.

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.

Display MySQL Results in Different Sections of Page

I have a MySQLi query that returns all of the "assets" assigned to an employee based on their EmployeeID. This works great. The problem I'm facing is in the presentation.
I have an HTML table that has two sections: 1 for Hardware and 1 for software. What I am hoping to avoid is having to perform separate lookups that generate separate result sets for each type of asset. The end result needs to display as follows:
I can build the table just fine. The result sets contains a field of asset_type but I've not had any luck figuring out the code to use to iterate through my single result set. Is this even possible? Can I pull just the hardware assets from the result set with a while? Perhaps a
while($result['asset_type'] == "hardware"){
echo ""; // table row code
}
And then repeat the same thing later in my table code for asset_type software?
UPDATE 1
The code I've thought might work so far, but isn't doing anything, is
// SQL query
$q = "SELECT * FROM `assets_table` WHERE `emp_id` = '".$emp_id."'";
$r = mysqli_query($connect, $q);
$total_assets = mysqli_num_rows($r);
while($r){
if($r['category'] = "hardware"){
echo $r['asset_name']." - ".$r['hw_make']." ".$r['hw_model'];
}
}
I ended up going ahead and breaking out the query into multiple result sets and dealing with them that way. It'd be awesome if you could have a while() with a WHERE statement when iterating through result sets / arrays.

Select only rows with certain critiera then output data

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

MySQL fetch_assoc question

Is it possible to use a nested mysql_fetch_assoc Basically I used the first one to populate text boxes, now from another table I need to fetch and image. How do I go about doing this without closing the first one, because I have more text boxes below this image that need to be fetched.
while($row = mysql_fetch_assoc($result)){
?>
<form id="myform" name="myform" action="profiledo.php" method="post">
<p>First Name
<input type="text" name="firstname" id="textfield" value="<?php echo( htmlspecialchars( $row['FirstName'] ) ); ?>" />
<br />
in order to be able to fetch $result more times (I see no reason though), you'll have to make single query for each fetch. I see no reason for doing so though
I think you are looking for a single query with a join. Something like:
SELECT * FROM `Profile` NATURAL JOIN `Table with Image`
But the exact query depends on the schemas of your two tables.
I think that it is more beneficial to put all of the information you want quickly into an array and accessing the retrieved data from memory, as opposed to slowly adding it while the query is looping through it.
// Read records
$query = "SELECT * FROM table WHERE condition";
$query = mysql_query($query);
// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;
// Delete last empty one
array_pop($array);
You can use print_r($array) to see the results.
If the image is not on the same table, you could join the tables via a similar identifier (unique user id?).
$query = "SELECT * FROM table1, table2 WHERE table1.id=table2.id";
Then the first user's First name would be: $array[0]['FirstName'], second would be $array[1]['FirstName'], etc.
Blake
The answer is, yes it is possible to use nested mysql_fetch_assoc().
Though I'm not very sure on the question and what does your current implementation looks like but here are some suggestion anyways:
Use a JOIN query: though it is possible only if the data contained in your text-boxes is related to the image. From what little information you have provided, I guess the image is the user's image and is stored in a separate table. If that is true, then the most obvious implementation is that the image table is referenced by some user id. Assuming that there is a User and UserImage tables, this is how the JOIN query might look like:
SELECT `User.*`, `UserImage`.`image`
FROM `User`
INNER JOIN `UserImage` ON `User`.`id` = `UserImage`.`user_id`
WHERE `User`.`id` = xxx;
Please note that the above is only a template query and you'll need to change this as per your schema and logic. Also, I missed to mention that the above implementation might need some extra logic if there can be multiple images per user.
A nested mysql_fetch_assoc() is possible but it depends on your implementation. As you've mentioned that the first (or outer) mysql_fetch_assoc() is used to fill-in text boxes. And within this, you would like to run another mysql_fetch_assoc() to fetch the image. So, if the image query is run on a separate table and is run as a separate query, then you might store its result in a separate variable, say $result1 and then run the inner mysql_fetch_assoc() on that resultset.
while ($row = mysql_fetch_assoc($result)) {
// $row array now contains the profile details
$result1 = mysql_query('the query for fetching the image');
while ($row1 = mysql_fetch_assoc($result1)) {
// $row1 array now contains the image
}
}
It is important that the inner mysql_fetch_assoc() runs on a separate resultset.
Hope the above helps! If it is none of the above that you are trying to do, I'd appreciate if you could provide more inputs on what you require.

problem in fetching from complicated mysql table

I'm using an if clause to fetch the value of my mysql table data because my table schema is not normal.
now for getting these values, I wrote the below code:
$result = $db->sql_query("SELECT type, var, count from table_counter");
while ($row = $db->sql_fetchrow($result)) {
$type = $row['type'];
$var = $row['var'];
$count = intval($row['count']);
if(($type == "total") && ($var == "visits")) {
$totalVisits= $count;
}elseif(($type == "total") && ($var == "pageviews")) {
$totalPVisits= $count;
}
}
Is there any other way rather than using an if clause?!
SELECT `type`, `var`, SUM(`count`) AS `sum`
FROM table_counter
GROUP BY `type`,`var`;
There's no need for that intval() to convert the count field to integer. It's already defined as an integer in your MySQL table.
What you have here is some kind of EAV data structure and the if is a common way to fetch from it. There are other ways of pivoting them.
What are you aiming to do with the data? Just create the counts?
You could extract the data in more than one query.
Another option is to put the data into an array, then sort it using PHP array sort functions. It could then be split it into two separate arrays, if needed.
You will need to pivot the data. And that will require you to write some code to parse out what you need in php and mysql. This will can get heavy depending on how much data you want and how it's stored and how it needs to be retrieved. Based on your question text it looks you are storing very simple data, but for every type and set of data you are storing you will need to create logic for it.
Soooo, if you know exactly what the variables are going to be you could set up and array of the variable names you want set and loop through them.
$legal_values = array('visits', 'pageviews', etc...);
while ($row = $db->sql_fetchrow($result)) {
if (in_array($row['var'], $legal_values)) {
$$row['var'] = $intval($row['count'];
}
}
This is just a simple example and only works if all the types are count. Obviously if you want to filter it down you will need more logic. There isn't really a catch-all solution for this stuff, so it comes down to writing something that is simple so you understand it and you can easily extend it to handle more data that you might need later.
${$row['type'] . ucfirst($row['var'])} = $row['count'];
That reads total pageviews 107 to $totalPageviews = 107. Depending on where you want to use this, this may be a possibility.

Categories