I have been working on this problem for a few weeks now and everything I have tried will not work. I am new to working with PHP and MySQL and have never use a forum, so you can say I am new to everything right now.

When I try to load the page in the web browser, I get this error message:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Users/herkinsfrancois/Sites/divine_endings/inculudes/function.php on line 33
Database query failed:

Here is the function I have been using and line 33 is the line in red.

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}

Any suggestions or feedback would be greatly appreciated!

-HFK

just stating that $connection is global won't help much
$connection (see php.net mysql_connect) is ususally defined earlier than line 33 but after the global, something like

$connection="mysql_connect('localhost', 'admin', '1admin') or die(mysql_error())";// host account password

which may be included in a 'connect.php' script if $connection is also accesssed elsewhere, from the look of the error message, there is no database connection
if there is a $connection defined, are the server account and password correct

I have had problems in the past trying to pass a resource like this and make it work. It was simpler to open a new connection. You will end up with more open connections and that might be a problem in some cases but it has never been a problem for me.

Having said that, it may also be that your original connect didn't work or you have lost the pointer before it was passed.

Oh Ok, I am going to work at it some more with all your suggestions and keep you updated on any solutions! Thanks so much for both of your help! This online forum stuff is kinda nice! Stay Tuned!!!

-HFK

Ok, so the function I have posted earlier is still not working. When I take this function out of the code, everything else works. The rest of the coding is able to access the database, but when I include this one it does not work and gives me the error code. Something about the "query" is not working.

I do have the

1.
      $connection="mysql_connect('localhost', 'admin', '1admin') or die(mysql_error())";// host account password

as you suggested already in my "includes" page and it is working for the other functions. Its just that line 33 that is not working.

Here is everything I have on my function.php page:

<?php
       function confirm_query($result_set) {
               if (!$result_set) {
                       die("Database query failed: " . mysql_error());
               }
       }
       
       function get_all_subjects() {
               $query = "SELECT *
                               FROM subjects
                               ORDER BY position ASC";
               $subject_set = mysql_query($query);
               confirm_query($subject_set);
               return $subject_set;
       }
       
       function get_all_for_subject($subject_id) {
               $query = "SELECT *
                       FROM pages
                       WHERE subject_id = {$subject_id}
                       ORDER BY position ASC";
               $pages_set = mysql_query($query, $connection);
               confirm_query($pages_set);
               return $pages_set;
       }
       
       function get_subject_by_id($subject_id) {
              
               $query = "SELECT * ";
               $query .= "FROM subjects ";
               $query .= "WHERE id=" . $subject_id ." ";
               $query .= "LIMIT 1";
               $result_set = mysql_query($query, $connection);
               confirm_query($result_set);
               // REMEMBER:
               // if no rows are returned, fetch_array will return false
               if ($subject = mysql_fetch_array($result_set)) {
                       return $subject;
               } else {
                       return NULL;
               }
       }
?>

Not sure where to go from here?!?! Any other suggestions? Thanks for your help thus far!

$query = "SELECT * FROM subjects  WHERE id='$subject_id' LIMIT 1";
//
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";

text field quotes? there arent any in the concatenated version

Ok, so the function I have posted earlier is still not working. When I take this function out of the code, everything else works. The rest of the coding is able to access the database, but when I include this one it does not work and gives me the error code. Something about the "query" is not working.

I do have the

$connection="mysql_connect('localhost', 'admin', '1admin') or die(mysql_error())";// host account password

as you suggested already in my "includes" page and it is working for the other functions. Its just that line 33 that is not working.

Here is everything I have on my function.php page:

<?php
       function confirm_query($result_set) {
               if (!$result_set) {
                       die("Database query failed: " . mysql_error());
               }
       }
       
       function get_all_subjects() {
               $query = "SELECT *
                               FROM subjects
                               ORDER BY position ASC";
               $subject_set = mysql_query($query);
               confirm_query($subject_set);
               return $subject_set;
       }
       
       function get_all_for_subject($subject_id) {
               $query = "SELECT *
                       FROM pages
                       WHERE subject_id = {$subject_id}
                       ORDER BY position ASC";
               $pages_set = mysql_query($query, $connection);
               confirm_query($pages_set);
               return $pages_set;
       }
       
       function get_subject_by_id($subject_id) {
              
               $query = "SELECT * ";
               $query .= "FROM subjects ";
               $query .= "WHERE id=" . $subject_id ." ";
               $query .= "LIMIT 1";
               $result_set = mysql_query($query, $connection);
               confirm_query($result_set);
               // REMEMBER:
               // if no rows are returned, fetch_array will return false
               if ($subject = mysql_fetch_array($result_set)) {
                       return $subject;
               } else {
                       return NULL;
               }
       }
?>

Is this what you meant by text field quotes? Not sure where to go from here?!?! Any other suggestions? Thanks for your help thus far!

in a where clause where column [I]operator[/I] number is ok where column [I]operator[/I] 'text' text is quoted

query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id='" . $subject_id ."' ";// note extra single quotes around $subject
$query .= "LIMIT 1";

my opinion (humble or otherwise) this is a bad way to set out a query, such a risk of typo, all the joins parse to

$query = "SELECT * FROM subjects  WHERE id='$subject_id' LIMIT 1";

is simpler, variables parse inside dquotes. or at least

$query = "SELECT * FROM subjects  WHERE id='".$subject_id."' LIMIT 1";

without some quote mysql sql will assume the text is a resource :- column, table,dbase -: name

Hello,

I have changed over the codes as you have suggested to make things simpler and my result is still coming up with the same error message... I have even rewrote it to match the two other functions that are working and it still gives me the same error.

Is there another particular way you would write this code. What I am trying to do is write the function to display the subject names on the content page for my website. My functions for pages and subjects work fine and they access the database, but this one has me stumbled.

I am still open to suggestions. Thanks so much for all your efforts with this! I will still be plugging away with it.

I have been working on this problem for a few weeks now and everything I have tried will not work. I am new to working with PHP and MySQL and have never use a forum, so you can say I am new to everything right now.

When I try to load the page in the web browser, I get this error message:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Users/herkinsfrancois/Sites/divine_endings/inculudes/function.php on line 33
Database query failed:

Here is the function I have been using and line 33 is the line in red.

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}

Any suggestions or feedback would be greatly appreciated!

-HFK

----------------------------------------------------------------------------
Hi,
I am new to PHP and Ruby on Rails
I don't know if you figured it out but the problem is in the WHERE clause. if you comment it out your page will work.
for some reason it is being passed as a string.

So do it this way:
$query .= "WHERE id=' " . $subject_id . " ' ";

it will work by wrapping single quotes around subject_id
i am still trying to figure out why though. I am thinking maybe its the version of PHP you are using.

1.
      $connection="mysql_connect('localhost', 'admin', '1admin') or die(mysql_error())";// host account password

If I may ever so gnetly ask a dumb question, why is the function call inside quotes? This results in $connection containing the string you see, rather than a handle referencing the desired database connection. To actually execute mysql_connect, you need to remove the double quotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.