Jump to content
xisto Community
Sign in to follow this  
vblk

Need Help In Php (p.s. Yarrgh)

Recommended Posts

Dear yarrgh thankx for your elp before but i caught a ssyntax error in the coding.

The page displays but instead of the info there a long syntax error by mysql? The problem was with the array codin to get the array from mysql!

 

Heres the problem,

Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\XAMPP\apachefriends\xampp\htdocs\xampp\all.php on line 137
(Note:That i test my web pages in my own PC. It has Xampp 1.4.12 And phpmyadmin 2.6 AND Mysql 4.1 :) )

 

So you think you can help me on this? :D

 

Thankx,

VBLK :rolleyes:

 

Notice from KuBi:
Added
tags.

Share this post


Link to post
Share on other sites

This error occurs when you attempt to retrieve data from a failed MySQL query. For example, you may have something like:

$result = mysql_query('SELECT X FROM Y');$data = mysql_fetch_array($result);

If, say, MySQL had failed to connect, the field 'X' didn't exist in the table 'Y', the table 'Y' didn't exist at all, or any of a number of other problems, the query will fail. For this reason, I would recommend you use something like:

$result = mysql_query('SELECT X FROM Y');if( $result ) {   $data = mysql_fetch_array($result);}

If the MySQL query fails, $result will be assigned a value that evaluates to false; therefore, the data will only be retrieved in an array if $result does point to a valid MySQL resource.

Hope that makes sense, and helps.
Edited by Spectre (see edit history)

Share this post


Link to post
Share on other sites

I've never had that problem. Right now my tables are empty and mysql_fetch_array() doesn't give any errors.

 

 

EDIT:

 

I've just noticed that my results are in loops. For example:

 

<?php...$a = mysql_query("SELECT * FROM `news`");$b = mysql_num_rows($a);if ($b > 0) {   ...}...?>

or

 

<?php...$a = mysql_query("SELECT * FROM `news`");while($blah = mysql_fetch_array($a)) {   ...}?>

Maybe Spectre is right about it when there isn't anything in the table. I guess I've just always done loops and never really had this problem yet.
Edited by Yarrgh (see edit history)

Share this post


Link to post
Share on other sites

Maybe I'm right? Maybe? MAYBE?

 

Tables being empty doesn't mean that the query will fail. Tables not existing or explictly specified fields not existing within tables will. If you have error reporting turned off, you will not see any errors - even with the method you mentioned, if you had full error reporting turned on, you would see an error at least once (during the first evaluation of the while() loop's condition).

 

For example:

mysql_query('CREATE TABLE test_table (test_field tinyint(1))');$result = mysql_query('SELECT test_field FROM test_table');$data = mysql_fetch_assoc($result);

Obviously, the table 'test_table' has only just been created and so is empty, but this will not cause an error - the $data variable will simply be assigned a boolean value of FALSE instead of an associative array.
Edited by Spectre (see edit history)

Share this post


Link to post
Share on other sites

Dear Yarrgh,
Hey i read your's and spectres's posts on how to fix it but the unfortunate thing is i don't get a single thing spectre's telling.(maybe it's because i'm dumb :huh: ) But hey i've given all my coding below, can you edit that coding and post the corrected coding?

<?php 					//check to see if they are trying to view a certain projectif (isset($_GET["id"])) {	$id = $_GET["id"];	// Connect to the mysql and select the database	mysql_connect ( "localhost" , "" , "" );	mysql_select_db( "projects" );	// Get the data from the database   $a = mysql_query ( "SELECT * from `PROJECTS` WHERE `id`='".$id."'");	// Put the info into an array for easy use	$info = mysql_fetch_array($a);}else {   // Some other code to list the projects}?>

Note that i've created the databse and have already added info. an example Id is 001

Thankx For your help, ;)
Bye
VBLK

Share this post


Link to post
Share on other sites

Try this. I know I'm not 'Yarrgh', but I'm the next best thing... honest...

 

You may want to consider structuring your code a little better, too (I understand that it may have been structured and good old IPB screwed up the indentation - but if not, structure, structure, structure!) - it makes it infinately easier for others to quickly analyze and modify your code.

 

Also, can you please edit your post and correct the 'code' bbCode tags.

 

<?php //check to see if they are trying to view a certain projectif (isset($_GET["id"])) {$id = $_GET["id"];// Connect to the mysql and select the databasemysql_connect ( "localhost" , "" , "" );mysql_select_db( "projects" );// Get the data from the database$a = mysql_query("SELECT * from PROJECTS WHERE id = '$id'");// Put the info into an array for easy useif( $a ) {   $info = @mysql_fetch_array($a);} else {   // The script will process this if the query fails.   // If you want, put something here - such as an error message.}}else {// Some other code to list the projects}?>
Edited by Spectre (see edit history)

Share this post


Link to post
Share on other sites

Sorry it takes me so long to respond. I have school and work. Looks like I was beat to it.

I guess the first time I could've gave you a code that is more user friendly with the error handling just in case users try to view a project that doesn't exist or what ever. I'd say instead of just displaying an error if it doesn't exist to list the projects with an error near the top telling them that the requested project didn't exist.

//check to see if they are trying to view a certain projectif (isset($_GET["id"])) {   $id = $_GET["id"];     // Connect to the mysql and select the database   mysql_connect ( "localhost" , "" , "" );   mysql_select_db( "projects" );   // Get the data from the database   $a = mysql_query("SELECT * from PROJECTS WHERE id = '$id'");   // Put the info into an array for easy use   if( $a ) {	 $info = @mysql_fetch_array($a);   } else {	 list_projects("project doesn't exist");   }}else {   list_projects("");}function list_projects($error) {  //code to list the projects. $error is the variable to display the error.}

But of course you don't need to do this. You could just simply display an error.

If I did the function wrong I'm sorry. I'm at school and can't check myself right now
Edited by Yarrgh (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.