"Supplied argument is not a valid MySQL result resource"? As the manual says, mysql_query returns FALSE if a SELECT query caused an error. FALSE is not a result resource, and trying to use it as one in mysql_fetch_array, mysql_num_rows or something like that will give you this error message. Just search these forums for "supplied argument is not a valid MySQL result resource" for plenty of examples and suggestions to use mysql_error to help diagnose what was wrong with the query. And read those manual pages.
If you feel that we missed out some. Just PM moderators of this forum.
All replies to this thread when this thread is unlocked/being edited will be deleted.
When you have got your problem solved please click the "Mark Thread Resolved" at the bottom of the thread. This makes it easier for people go to threads where there are more chances of them finding a working solution.
Do's / Don'ts
1) The General overview:
If it's improper to say something in a public place comprised of mixed races and ages, it's inappropriate to post it here in the forums.
2) Advertising on the board:
We can't allow this in any form. If someone is interested in getting exposure through the site, please contact the administrator via email at staff@phpbuilder.com . Footers may contain a pitch - it's a bit of a trade-off. The user is contributing to the community and in return they get exposure via their footer. Posting useless stuff and a huge ad in their footers is a complete NO.
3) Off topic posts:
Please use the Echo Lounge forum.
4) Footer size:
We need to limit this to 5 lines. Obviously if there are 20 posts on a page and we allow more, we'll end up with 100 extra lines to scroll when reading messages.
5) Job posts
PHPBuilder.com have a contract with Dice that doesn't allow them to host job openings. There are no exceptions to this.
[edited to change the MySQL manual link - P]
Last edited by Weedpacket; 03-26-2013 at 03:41 PM.
Great Post! Just a couple additions that I've seen asked MANY times.
If you ever have a problem with MySQL. Eg: Won't insert or update, nothing happens, etc. Here is the two best ways I have found to error check any MySQL problem.
Create two variables, one that holds the statement you want to execute, and a seperate one to actualy execute it using mysql_query()
PHP Code:
$string = "INSERT INTO table (first, last, email) VALUES('$first', '$last', '$email')";
$query = mysql_query($string);
Now, to do the error checking, print out the string to be executed so you can see what MySQL is ACTUALLY executing. Then, use mysql_error() along with mysql_query() for error checking.
PHP Code:
$string = "INSERT INTO table (first, last, email) VALUES('$first', '$last', '$email')";
print $string . "<br>";
$query = mysql_query($string) or die(mysql_error());
That should help you solve any MySQL problem. Also, watch the use of reserved words. Make sure you're not using datatypes or built-in MySQL functions for column names.
That is MySQL's built in function for inserting the current date/time. Create a date or timestamp field for example, then to insert the current date/time, use
PHP Code:
$query = mysql_query("INSERT INTO table (inputed_time) VALUES(NOW())") or die(mysql_error());
Notice how I didn't create two seperate variables. If I did it like this:
PHP Code:
// This is the wrong way
$string = "INSERT INTO table (inputed_time) VALUES(NOW())";
$query = mysql_query($string) or die(mysql_error());
It wouldn't work. NOW() is not a PHP function; it needs to be executed as a mysql function, thus, the first way is the correct way to do it.
How to retrieve and display data from database
Once you have your table setup and information is stored in it, all you do to put the information on the browser is query the database then display it.
// replace these with your credentials
$server = "localhost";
$user = "cgraz";
$password = "phpb";
$database = "contacts";
// Connect to Server
mysql_connect($server, $user, $pass) or die(mysql_error());
// Select Database
mysql_select_db($database) or die(mysql_error());
// Query the Database
$query = mysql_query("SELECT * FROM table"); // replace table with your table name
while($row = mysql_fetch_array($query)) { // put results into an array
echo "Name: " . $row["name"] . "<br>"; // (assuming field name in db is 'name'
echo "Email: " . $row["email"] . "<br>"; // (assuming field name in db is email)
// and so forth
// close the while loop
}
And that's it!
Cgraz
Last edited by bradgrafelman; 11-15-2011 at 11:18 AM.
Reason: fixed typo w/ code comment
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 10 results per page and your script comes out with 12 results.
this will allow your script to say next 2 if you're on the first page and previous 10 if you're on the second page. */
in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT.
This will limit how many results are actually displayed on the page. */
$strSQL = mysql_query("SELECT * FROM $table LIMIT $start,$pagelimit") or die(mysql_error());
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output ten results per page.
change the $pagelimit variable to whatever to output more than 10 result per page. */
?>
There are plenty of ways to do this; this is just my way. It's all commented so make sure you understand how it works before copying and pasting.
Yikes! Never, ever use a *_numrows() call when you are simply finding the number of rows. Use SELECT COUNT( * ) FROM table instead, it is far more efficient.
Preset table names
Use variables or definitions in database queries instead of hard-coding table names.
PHP Code:
define('TBL_SESSIONS', 'tbl_sessions');
$result = mysql_query('SELECT * FROM ' . TBL_SESSIONS)
Use Prefixes
Prefix table and field names with short, meaningful identifiers. For example:
tbl_config
configId
cfgProfileId
cfgName
cfgValue
Use a consistent naming scheme
Whether your fields are called cfgProfileId or cfg_profile_id, stick to that convention with the rest of your fields. (Same for table names)
I ran into a little trouble today when upgrading from MySQL 4.0 to MySQL 5. As of MySQL 4.1 the way the PASSWORD() function works has changed, see the manual for more details
The following will cause an error on MySQL 4.1 and above:
PHP Code:
$query = mysql_query("
SELECT username, password
FROM admin_table
WHERE username = '$user'
AND
password = PASSWORD('$pass')");
Instead you must simply use the function OLD_PASSWORD() which funny enough does what the old PASSWORD() function use to do.
PHP Code:
$query = mysql_query("
SELECT username, password
FROM admin_table
WHERE username = '$user'
AND
password = OLD_PASSWORD('$pass')");
As a lot of online tutorials & books still demonstrate using the PASSWORD() function I thought it would be wise to post this here. I hope this prevents someone from locking themselves out of their own script Naturally I would only suggest using this as a temporary fix until you upgrade the whole of your security.
Bookmarks