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.
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 1 year ago.
Improve this question
When I put a number myself the database gets updated to Success but when I try to access the same number from $_POST It complete the transaction but doesn't affect the rows even thought it's using the same number.
Example of a query that works perfectly and updates the database
$sql = "UPDATE `transactions` SET `status` ='Success' WHERE `transactions`.`txn_id` = 65765756";
Example of a query that doesn't work
$sql = "UPDATE `transactions` SET `status` ='Success' WHERE `transactions`.`txn_id` = ".$_POST['m_payment_id'];
First, check what's in the value, and make sure it is the same as what you are manually entering.
var_dump($_POST['m_payment_id'])
Second, the code without any other checks is a SQL injection vulnerability.
You could convert the value into an integer to protect against this, e.g. intval($_POST['m_payment_id'])
Ideally though, you would be using bindings.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 am a beginner in PHP and SQL. I have been trying to delete rows in SQL table using the following code but it doesn't work. Please help.
<?php
/*
DELETE.PHP
Deletes a specific entry from the 'db' table
*/
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM db WHERE 'Report No.'= '$id'")
or die(mysql_error());
// redirect back to the view page
header("Location: view.php");
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view.php");
}
?>
Apply backticks(`) around table field name "Report No." (its not standard way to define a table field name)
Try this
$result = mysql_query("DELETE FROM db WHERE `Report No.`= '$id'");
Fix your query by removing single quote of name table:
$result = mysql_query("DELETE FROM db WHERE `Report No.`= '$id'");
Make sure that you type right for Report No. column name. Actually for naming Report No. is not recommended.
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";
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 is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
$sql="SELECT product.title, product.description, product.price, product.product_id,
FROM product
INNER JOIN (SELECT * FROM product_category WHERE category_id='$categoryid') AS a
ON a.product_id=product.product_id";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
and the i get this warning:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in......
You have a comma even after the last selected field.
Might be ambigous, try to change it to:
...
FROM product p INNER JOIN
...
and replace the product. prefix in the first part of your SELECT statement by p.
If this still does not work, copy the complete command to phpmyadmin and execute it there (replace $categoryid with a real value first), you usually get a clue as to what is wrong with your statement.
Also there are commands to return more information on mysql errors in php as well (mysql_error)