Solved

PHP/MySQL 2nd query problem

Posted on 2013-01-05
13
396 Views
Last Modified: 2013-01-08
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 ) ;
      }

?>
0
Comment
Question by:dresdena1
  • 5
  • 5
  • 3
13 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 38748003
It's probably these items:

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

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

Open in new window

The reason that is a problem is because that is a different method of getting data from the database and it will not share the connection you made using the first method at:
$connect = mysql_connect("localhost","xxx","xxx");

Please look here http://us2.php.net/manual/en/book.mysql.php for the proper methods.  Note that you must setup a new result set if you make a second connection or even a 2nd request in the middle of the loop for the first results.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38748335
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!
0
 

Author Comment

by:dresdena1
ID: 38748751
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38748806
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?
0
 

Author Comment

by:dresdena1
ID: 38748844
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38748866
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:dresdena1
ID: 38749207
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38749305
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.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38749435
Ray, you should read my first comment.  He's mixing two different MySQL methods, that the #1 reason it doesn't work.
0
 

Author Comment

by:dresdena1
ID: 38750499
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.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38750545
Dave, I agree that he is mixing methods, but there is much more wrong than that -- it looks like the script has been cobbled together from other parts, and some of the class/function definitions are missing!

@dresdena1: Please read this web page: http://sscce.org/ -- I am hoping it will give you the guidance you need to be able to capture the central issues (and there are more than one) in isolated test cases, so you will be able to solve them.  We can help, but we need to see certain specific information, such as the structure of tables, the source code with accurate line numbers, and the structure of the data base.  We are experts, but not mind readers, and you have to work with us to make this information available.

All of the PHP functions are documented in the online man pages.  When you get "wrong parameter count," please don't waste your time posting that sort of thing here.  Instead, just make a quick check of the PHP.net web site.  Put the name of the function into the search box in the upper right of the page.  Example: var_dump().

And if all of this is overwhelming, you have a couple of choices to make.  In respect of your time you might want to hire a professional developer who can get hands-on into the issues.  They can be solved very quickly by a pro, probably before lunch.  Or you can try to learn PHP and data base programming.  That will take several weeks of intensive study.  If you choose to start down that path, start with this book.
http://www.sitepoint.com/books/phpmysql5/

Best regards, ~Ray
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38751740
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.
0
 

Author Closing Comment

by:dresdena1
ID: 38756871
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.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now