Issue with mysql fetch array [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using below code to loop records and display the Country value. If there are duplicate values they are all listed like Australia, Australia, United States, United States, United States. How can I remove any duplicates and just show Australia, United States? I cant use DISTINCT in mysql_query as I need to use an existing mysql_query. Thanks
<?php
while($row = mysql_fetch_array($result)) {
echo $row['Country'];
}
?>
I cant use DISTINCT in mysql_query as I need to use an existing mysql_query.
I am using
$result = mysql_query("SELECT * FROM specials WHERE expiry > CURDATE() ORDER BY Country;") or die(mysql_error());

The best way to do this would be to use DISTINCT clause in your MySQL query:
SELECT DISTINCT Country FROM table_name
But however, since you added this:
I cant use DISTINCT in mysql_query as I need to use an existing mysql_query.
You can simply store all the countries in an array and then use PHP's built-in function array_unique() to remove the duplicate values, like so:
while($row = mysql_fetch_array($result)) {
$countries[] = trim( $row['Country'] );
}
$countries = array_unique($countries);

Related

Two selects to while function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have one table which need to find all date and put the name just once (this is workig = $sql), but i have problem to select the that name of data and count how much time is repeats in same table. So where is name Test(down) there needs to be count for how much time that name repeats in whole table.
If I put one more while function the table dies.
Im stacked. Im not very good with php and mysql. If someone can help.
$sql = "SELECT DISTINCT one,two FROM results";
$sql2 = "select test, count(*) as foo_count from results group by test;";
$result = mysqli_query($conn,$sql);
$result2 = mysqli_query($conn,$sql2);
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["one"]."</td><td>".$row["two"]."</td><td>";}?> Test</td></tr>
I'm having a little bit of a hard time following your question, but I think you are looking for a single sql statement like this
$sql = "select test, count(one) as foo_count from results group by test";

php report undefined index when used select * in mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have problem only if I use select * but if I select exact field from my database it is working fine
$sql = "SELECT * FROM `product id`;";
$resutl = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo $row["product name"];
};
It is working if I use
SELECT `product name` FROM `product id`
Thank you
$sql = "SELECT * FROM `product id`";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
print_r($row);
}
and check your array and traverse it perfectly as when you call all rows it will not be same as fetching one row.
Try this it should work fine, and you will get more idea.
only 2 errors I found is $result variable was not correct and semicolon in query!
Table names e column names that include white-space are not a good idea because may be in conflict with mysql sintax (when mysql parse the query). You can use var_dump($row).
Use mysql_fetch_array() instead of mysql_fetch_assoc()
While the two are similar mysql_fetch_assoc() only returns an associative array.
Also you should think of moving from mysql to mysqli or PDO. mysql is being removed as of PHP6 and already depreciated as of PHP5.5.

MySQL solution for multiple column totals [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am retrieving multiple records from a table in MySQL. Is there a way that I can retrieve one row which totals each of the columns from this query? I was hoping that there might be an SQL solution, as I'll be repeating the process for many tables as it's being used for a dynamic report.
If there is no SQL solution, is there a way to easily add together all the nested arrays inside of an array? This would achieve the same result, but in PHP.
I can't seem to find any information on either approach.
You can do this using using aggregation. You don't give much guidance on what your query looks like, but something like:
select sum(col1) as sumcol1, sum(col2) as sumcol2, . . .
from t;
Without the group by clause, this returns one row, which is a summary of everything in t.
I am not sure what you exactly want to achieve by reading your question as you did not provide any codes or data. Try the following:
SELECT id, col_1, col_2, col_3, col_4, sum(t.Col)
FROM
(SELECT id, col_1, col_2, col_3, col_4,
SUM(col_1+col_2+col_3+col_4)
AS Col
FROM datas
WHERE id = '1'
GROUP BY id)t
You can see the fiddle here

Use WHERE in an INSERT INTO statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to let users insert a subject to an image. I know I can't use a WHERE clause in combination with an INSERT INTO statement, and I know that I should use SELECT. I am very new to mysql and I didn't understand the results I tried to search on google. So I needed a more specific answer;)
$classed = mysql_query("INSERT INTO images (subject) VALUES ('$_POST[subject]') WHERE image_id='$_POST[id]'");
You can't use WHERE in your INSERT statement. Consider trying UPDATE if you want to update existing rows, or INSERT without the WHERE to insert new rows.
Don't use the mysql_query function, as it deprecated. Try using PDO or mysqli_query instead.
Don't ever use unfiltered input in your query.
if you already have image_id value then you should UPDATE your table not INSERT.
you should escape your variables before inserting them in your table. by
mysql_real_escape_string()
try this:
$subject = mysql_real_escape_string($_POST['subject']) ;
$id =mysql_real_escape_string($_POST['id'] ) ;
$classed = mysql_query("UPDATE images
SET subject = '".$subject."'
WHERE image_id='".$id."' ");
You cannot use INSERT in existing rows, try UPDATE the SQL will be something like
UPDATE images SET subject = '...' WHERE id=x;

Get a name from a database using the id of the same row. In PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Im very new to php and i want to try and get a name from a database by using the id number.
Eg id=1 name=test
i want to say when id=1 make name=test
I thought this would do it, I have been trying make up the php myself. Rather than follow specific tutorials. Im assuming what i want to do is very commonly done but i cant understand where my code is going wrong.
$id = "1"; // later i will add it so this is defined by the link you click on
$firstname = mysql_query("SELECT firstname FROM $info_table WHERE id = $id");
$secondname = mysql_query("SELECT secondname FROM $info_table WHERE id = $id");
$name = $firstname $secondname;`
echo name;
the table is $info_table and then the firstname and secondname parts are the columns. Is that how it should be or not?
Anyone help clear up my confusion.
You can select a number of fields from your table with a single query, like so:
$records = mysql_query("SELECT `firstname`, `lastname` from `$info_table` WHERE id = $id");
while ($row = mysql_fetch_array($records)) {
echo "Name: $firstname $lastname";
}
However, please keep in mind that the mysql set of functions are deprecated, meaning if you use them, your code will likely not work in future versions of PHP. So to ensure your code is future proof, I would look into the mysqli* set of functions, or PDO.
One final thing, the code you have provided above provides NO protection against SQL injection, this is important if you are accepting any values from the user to be used in SQL queries.
You can select all the records by doing this:
$firstname = mysql_query("SELECT * FROM $info_table WHERE id = $id");
while($row = mysql_fetch_array($firstname)) {
echo $row['firstname'] . $row['secondname'];
}
Use mysqli_* instead of mysql_*.
If you are going to do this staticly then Moeed Farooqui answer is ok, but if you are looking for dynamically the you have to make id dynamically generated on any event.
for example link(as you say):
click
and then get this id on yourlink.php and your code look like as:
$id=$_GET['id'];
$firstname = mysql_query("SELECT firstname FROM $info_table WHERE id = $id");
$row=mysql_fetch_array($firstname);
echo $row['firstname'];

Categories