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";
Related
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 2 years ago.
Improve this question
I'm a begginer to PHP and I want to know how can I fetch some text from the corresponding ID and store it as a variable in PHP.
The table is like
ID----NAME----ACCOUNT----PASSWORD
1----name1----accont1----password2
2----name2----accont2----password2
3----name3----accont3----password3
Now if I want to get the account2 as text and save it in an variable (say acc2) then what should I do. Assuming that I have connection information in connect.php.
Edit: I want to select the account2 using the ID like from ID 2 select account.
Thanks In Advance!!!
Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:
$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']
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.
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 8 years ago.
Improve this question
I need to insert image row from table 'users2' to 'allbets'. But suddenlly this code don't working and I don't know why.. What is wrong with this?
$q2 = $pdo->prepare('INSERT INTO allbets (image) SELECT users2.image FROM users2 WHERE username = ?');
$q2->bindValue(1, $_SESSION['name']);
$q2 -> execute();
This code did not suddenly stop working, it never could have worked with its present query syntax. Change the query to this -
$q2 = $pdo -> prepare('INSERT INTO allbets (user, bet, komanda, teams, cof, data, image) VALUES ($user, $bet, $komanda, $teams, $cof, $data, (SELECT `users2`.`image` FROM `users2` WHERE `username` = ?));
Do yourself a service and error checking to your PHP code and to your PDO. This will let you know where to look when errors occur.
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 8 years ago.
Improve this question
I am a code noob. I want to display a number from a mysql database based on the value of 2 dropdown boxes i.e
box a = 100
box b = 600
return value 36.50
box a= 40
box b= 100
return value 125.95
Please help with the php code.
I assume you are using MySQLi and not officially removed MySQL API.
Here is the Query:
$query = "SELECT price FROM database WHERE pallets = ? AND SKU = ?";
$stmt = $DB->prepare($query);
$stmt->bind_param("ii", $aValue, $bValue);
$stm->execute();
$stmt->get_result();
while($row = $stmt->fetch(MYSQL_ASSOC))
{
echo "This is the price: " . $row['price'];
}
Note that your php version should at least be 5.4 for the above code to work.
$aValue and $bValue are respectively the value of two dropBoxes.
"ii" means respectively that values are of integer type.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I'm quite new to MySQL, so don't judge! Anyhow, what I'm trying to do is store the 10 newest rows in a MySQL database into a PHP array. My variable $news contains the MySQL data I got from running the fetch_news() query (which I told only to grab content that has a column called type with the value "news" in it). However, I've been unable to do this successfully.
If you guys could offer some help, or guidance, it'd be much appreciated as I have been stuck on on this for quite some time now!
$query = 'SELECT *
FROM `table_name`
WHERE `type`="news"
ORDER BY `article_id` DESC
LIMIT 10';
$query = mysqli_query($link, $query);
$result = array();
while ($row = mysqli_fetch_assoc($query)) {
$result[] = $row;
}
print_r($result);
$newest_ten = array();
$command = "select * from table_name order by date DESC limit 10";
$result = $db->query($command);
while($data = $result->fetch_assoc()){
$newest_ten[] = $data;
}
The command is getting all columns from the table table_name, putting them in order based on their date (so newest to oldest), and then only giving you the first ten of those.
The while loop goes through the results and adds them to an array.
While your "type" column tells you if the row is a news item or not, it doesn't tell you anything about when it was added to the table.
You need to use some other column as well, either some sort of incremental counter (like the "article_id" might be), or a time stamp when the item was added to your table (this is probably the "article_timestamp" in your case).
With one of those you can sort the results the way you want.
When you have sorted your results, you need to limit them to just the 10 you want.
You can find more information about ORDER BY and LIMIT in the Official MySQL documentation.