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 7 years ago.
Improve this question
I wan to load data from mysql with PHP like Facebook (load data by scrolling down). I checked in many tutorial where everyone is doing by order by ID but I can't do so while my fetching query is like follows.
mysql_query("SELECT * FROM conferencecreate WHERE ccFlag = 1 AND ccStartingDate >= '$nowTime' GROUP BY ccTitle");
If I want to implement ORDER BY ccId DESC then its not working. Any Idea how to solve this issue?
I tried this :-
mysql_query("SELECT * FROM conferencecreate
WHERE ccFlag = 1
AND ccStartingDate >= '$nowTime'
GROUP BY ccTitle
ORDER BY ccId DESC");
But this produced the error
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource
You really should not be using the mysql_ database extension any more but that said you need to learn how to process errors generated by any of the the mysql extensions
So try this
$res = mysql_query("SELECT * FROM conferencecreate
WHERE ccFlag = 1
AND ccStartingDate >= '$nowTime'
GROUP BY ccTitle
ORDER BY ccId DESC");
if ( ! $res ) {
echo mysql_error();
exit;
}
Now you should see an error message describing what is wrong with the query you have coded.
Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to order multiple statements by their id from one table, but I keep getting this error "mysql_fetch_array() expects parameter 1 to be resource, boolean given in..."
$sql="SELECT * FROM news WHERE categorie IN ('sports', 'movies', 'politics') ORDED BY id DESC";
$result=mysql_query($sql);
Should I group them first somehow?
For debugging php errors first consult with documentation
At first: The function is deprecated. Do not build new code on deprecated functions as they will be removed in the future.
Second: See return values, it says:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
That means your SQL query triggered an error and mysql_query returned false. Oh and your error points to mysql_fetch_array but your code example does not have any.
Now, where is the error? It is syntax error
Your query contains ORDED BY instead of ORDER BY
The $result is returning false hence it is throwing error message.
Please you can check with die statment in the mysql query as below : $result=mysql_query($sql)or die(mysql_error());
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 have a bit of a problem with a script I'm writing.
$sql ="UPDATE users SET adagio = 1 WHERE username = '$_SESSION[username]'";
The main problem here is that, while I've tested that
this part of the code will run
I've successfully connected to the correct server
3) $_SESSION[username] gives me the the correct username,
the database isn't updated when I run the code. adagio is a boolean (well, a tinybit, really), and I've uploaded my phpMyAdmin screen of the database here:
(http://imgur.com/HUFdx0p)
I'm not entirely sure why it isn't working, and although I've searched online and found similar threads, there wasn't a fix in any thread that worked for me. I'm wondering if one of you could possibly see what I'm doing wrong here?
Edit extra:
if(($_POST['adagio']) == 1){
$sql ="UPDATE users SET adagio = 1 WHERE username = '$_SESSION[username]'";
}
$_SESSION[username] is almost certainly a string so it must be in quotes:
$sql ="UPDATE users SET adagio = 1 WHERE username = '$_SESSION[username]'";
FYI, MySQL would be happy to tell you about SQL errors if you check for them. You should be using the appropriate error reporting functions in whatever MySQL library you are using.
Can you try this.
if (1 == $_POST['adagio']) {
$sql = sprintf ( "UPDATE users SET adagio = 1 WHERE username = '%s'", mysql_escape_string($_SESSION['username']) );
}
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.