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());
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 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
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)
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
Forgive me but I am very new to PHP and SQL.
I am simply trying to assign the results of an SQL query to a PHP variable ($num). The SQL query is counting how many times customer_id appears in a table. However when i use the code below i get the error :
Warning: mysql_result() expects parameter 1 to be resource, boolean given in ... line **
session_start(); is at the top of the page and {$_SESSION['userid']} is actually an integer value of the currently logged in user which corresponds to the customer_id in the bookings table. Database connection is the include/db_connection.php (which i know works). The code i am using currently is :
<?php
include 'include/db_connection.php'
$num = mysql_result(mysql_query("SELECT COUNT(*) FROM bookings WHERE customer_id={$_SESSION['userid']}"),0);
?>
The fact that a boolean is given suggests that the query failed, and the mysql_query returned FALSE. You could try to use mysql_query("foo") or die(mysql_error()); to find out what's wrong.
You need to tell php to fetch the row - mysql_query() simply returns a boolean (true/false) result. Store the query result in a variable and fetch the result with $row = mysql_fetch_assoc($myqueryresult) or $row = mysql_fetch_array($muqueryresult) and use the field result from $row['column'] where needed.
It might be easier to use the count rows function in php rather than COUNT in sql too.
Ideally you should use the mysqli api to query mysql instead as it is much more secure and modern.
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 9 years ago.
Improve this question
$SQL="SELECT first_name FROM people WHERE fname = '$fname' INSERT INTO (first_name) VALUES (fname)";
Anything wrong with this? Trying to insert a value from a user defined variable into a mysql table
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\wamp\www\Pxxxx\process.php on line 44
This is the error
$fname is a user defined variable
first_name is the column I'm trying to insert it into and it's in a table called people
You have the order inverted. It seems like you are looking for INSERT .. SELECT syntax (see MySQL documentation here: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html)
INSERT INTO target_table (first_name)
SELECT fname
FROM people
WHERE fname = ?
It was unclear from your example what the name of the table you were trying to insert data into is, so I just listed it as target_table here.
Your SQL statement has to be reordered like this:
"INSERT INTO people (fname) SELECT '$fname' FROM dual;"
This will select the value of $fname from the pseudo table "dual" and insert the value into "people".
Maybe this is more suitable:
"INSERT INTO people (fname) VALUES ('$fname');"
This snippet show you a simple insert statement.
Note: Please have a look for SQL Injection at Wikipedia. The code you are writing is open for these kinds of attacks. If you are writing PHP code, have a look for Prepared Statements and mysqli to prevent these attacks.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm new in php.
Usually my update query is working, only in this block resulting a query fail, I get the officer value from $_SESSION[id] that determined before, and contact_id from a form before.
Please help...
<?php
$server="localhost";
$db_user="root";
$pass="h6194v";
$db="my_db";
$koneksi=mysql_connect($server,$db_user,$pass)
or die ("Connection Error!!");
$dateline=date("Ymd");
$query="UPDATE `contact` SET `date`=\'$dateline\', `officer`=\'$_SESSION[id]\' WHERE `contact_id`=\'$_POST[no]\'";
$result=mysql_query($query) or die ("Query fail");
?>
First of all
Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial.
Other than that and if you use mysql:
select a database after you made a connection with mysql_select_db($db,$koneksi) or use database name in front of the table name in UPDATE statement like UPDATE my_db.contact ...
fix your UPDATE statement as suggested by others
use correct format for date date('Y-m-d') or better yet ditch it altogether and use NOW() in your UPDATE statement
I formatted your query and try this.
$query="UPDATE `contact` SET `date`='{$dateline}', `officer`='{$_SESSION['id']}' WHERE `contact_id`='{$_POST['no']}'";
Note: Dont use MySQL functions and they are deprecated instead use PDO or MySQLi functions. You code is open to SQL injection try to add validation or use prepared statement to protect.
try this -
$query="UPDATE `contact` SET `date`='".$dateline."', `officer`='".$_SESSION['id']."' WHERE `contact_id`='".$_POST['no']."'";
But validate $_POST or $_GET before using in query or anywhere.
It would help to know what kind of error you're getting.
Don't put user input or session variables straight into a query, you should sanitize them first, perhaps with prepared statements.
When you get an error like this, try using var_dump and die to see what's going on. I.e. before your query var_dump($_SESSION['id']). (And now that I write that, I see you don't have quotes on your array index, which will cause problems.
You need to access your array with quotes: $_SESSION['id'], $_POST['no']
Change your update query like this
$query="UPDATE `contact` SET `date`='".$dateline."', `officer`='".$_SESSION[id]."'
WHERE `contact_id`=".$_POST[no];
If contact_id is string, do like this
$query="UPDATE `contact` SET `date`='".$dateline."', `officer`='".$_SESSION[id]."'
WHERE `contact_id`='".$_POST[no]."'";