Avatar of dresdena1
dresdena1
 asked on

PHP/MySQL 2nd query problem

EE,
I am trying to write out a series of Gallery image pages. The data is in a MySQL database.
I am using php to write out static html pages.

I need two loops. One for the page itself and then a series of loops to create a table with multiple images.

I am trying to adapt a page that achieved the same result but connected to the database differently (my old ISP - I connect differently to my new ISP). My current problem is writing out data for the 2nd query.

The page connects to the database begins writing out the first page and then stops.
The error message is:
Fatal error: Call to a member function query() on a non-object in /home/abc123/public_html/maketable/make_galleries.php on line 62

Line 63 is where the 2nd query begins trying to extract data.
I will paste it below (it is greatly abbreviated from the original page, but I believe all necessary information is here - as it is the script will run to line 62).
Any help will be greatly appreciated.
Thank you.
dresdena1
<<
<?php

$connect = mysql_connect("localhost","xxx","xxx");

if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }


mysql_select_db("data");

$sql = 'select * from 402db where category = "Gallery" group by url;';
$result = mysql_query($sql, $connect);



if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}


while ($row = mysql_fetch_assoc($result)) {


            $id = $row["id"] ;
            $url = $row["url"] ;
            $meta_keywords = $row["meta_keywords"] ;
            $meta_description = $row["meta_description"] ;
            $title = $row["title"] ;
            $H1 = $row["H1"] ;
            $category = $row["category"] ;
            $img = $row["img"] ;
            $img_width = $row["img_width"] ;
            $img_height = $row["img_height"] ;
            $longdesc = $row["longdesc"] ;



                  echo "\n<BR><a href=\"http://www.abc123.com/maketable/"."".$url.""."\">Test $H1</a>"." "  ;

            $out = fopen( "/usr/home/abc123/public_html/maketable/$url", "w" ) ;

              fwrite( $out,"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"");
              fwrite( $out,"\n\"http://www.w3.org/TR/html4/strict.dtd\">");
              fwrite( $out,"\n<HTML> " ) ;
              fwrite( $out,"\n<HEAD> " ) ;
              fwrite( $out,"\n<title>" . strtoupper($category) . " </title> " ) ;
              fwrite( $out,"\n<META NAME=\"KEYWORDS\" CONTENT=\"$meta_keywords\"> " ) ;
              fwrite( $out,"\n<META NAME=\"DESCRIPTION\" CONTENT=\"$meta_description\"> " ) ;

              fwrite( $out,"\n</head> " ) ;
              fwrite( $out,"\n<body> " ) ;
              fwrite( $out,"\n<? include(\"header.html\" ) ?> " ) ;

              fwrite( $out,"\n<table width=780><tr><td width=600" ) ;

            $query = "select * from 402db where where category = 'Gallery' group by url" ;

            // Pass make the request.
            $q2->query($query);

$RowCount = $q->num_rows();
$i = 1 ;
            // loop trough the items in the categories
            while( $q2->next_record() ) {

                  $id = $row["id"] ;
                  $url = $row["url"] ;
                  $img = $row["img"] ;
                  $img_width = $row["img_width"] ;
                  $img_height = $row["img_height"] ;
                  $sm_img = $row["sm_img"] ;
                  $sm_width = $row["sm_width"] ;
                  $sm_height = $row["sm_height"] ;
                  $category = $row["category"] ;
                  $H1 = $row["H1"] ;



 fwrite( $out,"<td><a href=\"$img\" rel=\"Photo[pp]\" title=\"$H1\"><img src=\"$sm_img\" class=\"picthumb\" width=\"150\" height=\"150\" alt=\"$H1\" /></a></td>" ) ;

$i++ ;

            }
         fwrite( $out,"</tr></table>" ) ;



         fwrite( $out,"</body>" ) ;
         fwrite( $out,"\n" ) ;
         fwrite( $out,"</HTML>" ) ;

      fclose( $out ) ;
      }

?>
PHPMySQL Server

Avatar of undefined
Last Comment
dresdena1

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Please use the code snippet feature when you post code here at EE.  Click the grey "code" in the header block of the Post-a-Comment input area, and post your code in between the pseudo-tags.  This will give us line numbers and a unispace font that makes everything easier to read and discuss.

We need some additional information. First, please add error_reporting(E_ALL) to the top of your script so we can be sure that your script is not accidentally relying on any undefined variables.

Next, in consideration of Fatal error: Call to a member function query() on a non-object in /home/abc123/public_html/maketable/make_galleries.php on line 62 please use var_dump() at line 61 to print out the thing that is expected to be an object.  Since we cannot see all of the code, we have to guess and it would be much easier to know instead of guessing.  Tell us the object variable name, tell us how it gets instnatiated, and show us the code that accomplishes the instantiation.  It would probably be helpful to see the class definition, too.

My guess is that there is some "copied code" in here, and that parts of it were procedural and the copied part was object-oriented, but the class definition and object instantiation did not get copied.  But that is just a guess and when we begin to flesh out the information we will be able to guide you in the direction of knowing, which is always a better way to work!
dresdena1

ASKER
DaveBaldwin and Ray_Paseur,
What I am interested in doing is initiating a second loop in the script.
I am sure it is the connection as the script used to work when the connection was made using:
      $q = new DB_Site;
      $q2 = new DB_Site;

I am not a student, programmer or coder I am using php/mysql to write out static html web pages. Based on the code that I pasted previously is it possible to determine how to initiate the second loop?

What the script will do when it is working properly, is create unique web pages based on data in the mysql database. Each web page will have a photo gallery in it with multiple photos. I need two loops, the first will generate the unique pages and the second will populate these pages with multiple images.

My old ISP used to help me with getting the connections and loops established and then I could figure out how to format the extracted data for the web page. My old ISP has gone out of business and I no longer connect with $q = new DB_Site;

Any help would be greatly appreciated.
If you need any additional information please let me know.
Ray Paseur

In looking at the code above, I see this query:
$sql = 'select * from 402db where category = "Gallery" group by url;';

... and this query:
$query = "select * from 402db where where category = 'Gallery' group by url" ;

What is the reason for running two identical queries?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
dresdena1

ASKER
Ray_Paseur,
In the old code the first query would call the unique category and create the category page. It had a concatenation in it of appending .html to the lowercase category name to assign the url. The concatenated url was then used throughout the page. I stripped it out as the page will not be laid out that way.

The second query would call the specific data to populate the category page itself.
It is originally from a script (that still works on another site but connects with $q = new DB_site; ).

I cannot connect to the site with those connection parameters so I was trying to alter them.

I changed them to call all data for both queries, but group them based on the Gallery itself.
Ray Paseur

OK, what things do we know?  Do you have the data base credentials so that you can connect to the server and select the data base?  Let's try to break this down into small enough chunks that we can make sense of it.  See http://sscce.org
dresdena1

ASKER
Ray_Paseur,
Thanks very much.
Yes, I can connect to the database.
When I run the script it will start to write out the first page. It will then choke when it gets to the second query.
I viewed the page source and it has written out the top part of the page. It stops exactly where the 2nd query starts.
The page has the correct URL,<title>,and <meta> description information as the first query populated it from the database.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

It will then choke ...
I don't believe that "choke" is a term of art in software development.  What actually happens (http://sscce.org)?  How do you visualize the error?  If we can figure out the exact error we can probably get it fixed.
Dave Baldwin

Ray, you should read my first comment.  He's mixing two different MySQL methods, that the #1 reason it doesn't work.
dresdena1

ASKER
Ray_Paseur and DaveBaldwin,
I believe you are correct.
I am understanding this a little better now as it is being broken down.
In the original script the two queries were made by two separate connections to the database:
$q = new DB_site;
$q2 = new DB_site;

The first set of data extractions were carried out by $q and the second set by $q2
Currently I have only 1 connection to the database and that appears to work until line 62 when it encounters the second query.

As DaveBaldwin mentioned, what I (believe) that I need to do is establish a second database connection and then run the second query and extract the second set of data from the database.

As per your instructions I inserted error_reporting(E_ALL); at line 2 and
var_dump(); at line 61

I got the following error messages:
Test 1996 Impala SS
Warning: Wrong parameter count for var_dump() in /home/xxx/xxx/make_galleries.php on line 61

Notice: Undefined variable: q2 in /home/xxx/xxx/make_galleries.php on line 63

Fatal error: Call to a member function query() on a non-object in /home/xxx/xxx/make_galleries.php on line 63

Test 1996 Impala SS is the echo of the name and URL for the page(s) that are being written out. When the script works properly I click on the link to view the pages that have just been written out.

Thank you again for all of the help.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

I was harping on the different methods because that is where he is getting stuck.  While you and I can see some other problems, I don't think he will until that particular error is fixed.
dresdena1

ASKER
Dave and Ray,
Thanks for the help.
I have split the points and will decide on how to proceed.

I can work around / clean up the bad code. My real issue is getting the 2nd query to work.

Thank  you again.