What's wrong with my php query?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tdrsam
    New Member
    • May 2015
    • 97

    #1

    What's wrong with my php query?

    I don't know why I keep getting error messages for this line of code:

    Code:
    $result = bool mysqli::real_query ( string $query )or die(mysqli::$error());
    Is there some thing wrong with it? I've tried the object orientated style as well as the procedural style, but I get error messages with both.
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    What exactly are you trying to do?
    Normally to run a MySQL query in PHP, you would so something like:
    Code:
    $getter=mysql_query($sql) or die(mysql_error)));
    while($d=mysql_fetch_assoc($getter)){
    }

    Comment

    • tdrsam
      New Member
      • May 2015
      • 97

      #3
      I'm trying to bring all the records from a table in the database and display them on the web page. I used the method with mysql_query etc on my last website and had a lot of trouble when it came it deploying the site to a live server, so I'm trying to use the new and improved version with the mysqli extensions instead. I think it needs more detail as I keep getting error messages saying that the lines of code can't be used statically.

      Comment

      • computerfox
        Contributor
        • Mar 2010
        • 276

        #4
        :facepalm: Because it's standard database practice to loop through the results. No database, as far as I know, can grab all the records into an array the way you're trying, especially since you're trying to make it into a bool. That's just incorrect....

        Try this:
        Code:
        $getter=mysql_query($sql);
        while($d=mysql_fetch_assoc($getter)){
         print $d[column_name];
        }
        No matter if you use the old version or the "new and fancier" mysql, you still need to loop through to print the results.

        Edit:
        SQLite can, but you're not using SQLite.

        Comment

        • tdrsam
          New Member
          • May 2015
          • 97

          #5
          Thanks, but I am using a loop. A while loop to be exact. I just didn't add all the code to the question because I thought it was a simple question that someone would know the answer to right away. The problem is with the new MySQL extension. I'm fairly sure there must be something that I'm missing.

          Comment

          • computerfox
            Contributor
            • Mar 2010
            • 276

            #6
            Would it be possible to post the full code?

            Comment

            • tdrsam
              New Member
              • May 2015
              • 97

              #7
              Yes.

              Code:
              <?php
              $mysqli = mysqli_init();
              if (!$mysqli) {die('mysqli_init failed');}
              
              if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {die('Setting MYSQLI_INIT_COMMAND failed');}
              
              if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');}
              
              if (!$mysqli->real_connect('localhost', 'root', '', 'pca')) {die('Connect Error (' . mysqli_connect_errno() . ') '.mysqli_connect_error());}
              
              $query = "select * from news";
              
              $result = bool mysqli->real_query ( string $query )or die(mysqli->$error());
              
              $row = mysqli_result::fetch_array($result);
              
              echo "<table class='displayReviews' border='1' style='width:100%;'>";
              
              echo "<tr stlye='display:block;margin:0em auto;'><th>date</th><th>Headline</th><th>Body</th><th>Image</th></tr>";
              
              while ($row = mysqli_result::fetch_array($result))
              
              {
              echo "<tr><td>"; 
              echo $row['date'];
              echo "</td><td>";
              echo $row['headline'];
              echo "</td><td>";
              echo $row['body'];
              echo "</td><td>";
              echo $row['image'];
              echo "</td><td>";
              echo '<a href="edit.php">Edit</a>';
              echo "</td><td>";
              echo '<a href="delete.php">Delete</a>';  
              echo "</td></tr>";
              }
              echo "</table>";
              
              $mysqli->close();
              				
              ?>
              Also, is there a better way than just sticking it in a table as well? I know tables are a bit old fashioned, but I suppose they still work ok.

              Comment

              • computerfox
                Contributor
                • Mar 2010
                • 276

                #8
                Okay.....

                So I got the code to work. Try the changes.
                I also cleaned up your code to be more readable.
                To be honest, I can already see TONS of future issues with this "new and improved" version of MySQL and I have a number of years coding for it. Messiest thing!

                Also, please remember that if you have passwords set to no, you need to use null for that parameter. Is there anyway you can revert to the normal MySQL? You do know that just because it's installed, doesn't mean you have to use it... Have you tried the old code?

                Anyway... Here's the code:

                Code:
                <?php
                 $mysqli = mysqli_init();
                 if(!$mysqli){
                  print "MYSQLI failed...";
                 }
                 if(!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')){
                 }
                 if(!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)){
                  print 'Setting MYSQLI_OPT_CONNECT_TIMEOUT failed';
                 }
                 $mysqli->real_connect('localhost', 'root',null,'pca');
                 if($mysqli->connect_error){
                  print 'Connect Error '.$mysqli->connect_error;
                 }
                 $query="select * from news";
                 $result=$mysqli->query($query);
                 print "<table class='displayReviews' border='1' style='width:100%;'>";
                 print "<tr stlye='display:block;margin:0em auto;'><th>date</th><th>Headline</th><th>Body</th><th>Image</th></tr>";
                
                 while($row=$result->fetch_assoc()){
                  print "<tr><td>";
                  print $row['date'];
                  print "</td>";
                  print "</td><td>";
                  print $row['headline'];
                  print "</td><td>";
                  print $row['body'];
                  print "</td><td>";
                  print $row['image'];
                  print "</td><td>";
                  print '<a href="edit.php">Edit</a>';
                  print "</td><td>";
                  print '<a href="delete.php">Delete</a>';
                  print "</td></tr>";*/
                 }
                 print "</table>";
                 $mysqli->close();
                ?>


                If you must use the this version, this should be under your pillow:


                Good luck!

                To answer your other question, it's a web standard to use tables when showing critical data as crawlers can't access the tables. I have designed and implemented a few special GUI's that use divs instead of tables, but you need to know how to prevent robots from crawling the pages (or be willing to put the site on your intranet instead) and understand how the design should work. Stay with tables, it might not look nice, but it's a web standard and tons of organizations use them. What you could do is spend hours styling the table with CSS.

                Also, I just noticed that the edit and delete pages won't do anything as it's just going to the page. You should be passing an identifier for the row. May I ask what you're writing this for?

                Comment

                • tdrsam
                  New Member
                  • May 2015
                  • 97

                  #9
                  Thanks for the help Computerfox, that seems to have gotten it going.

                  The password thing is only because the site is still in development, that password will have to change once it goes into production.

                  I'm not really sure about which version of MySQL to use. I've had people telling me off for using the old version, now I have you saying it's better, but I don't really have enough experience yet to know which is better.

                  Thanks for the link to that manual, I've been trying to use it, but it doesn't seem to be working for me.

                  The table is fine. I heard someone saying they're very old fashioned but I think that was for general building in html, which I wouldn't really do in tables. I'm fine with having my database retrieved data in tables.

                  I'll be getting to the edit and delete pages next. I'm writing this for a new responsive site for my company. This is part of the news page. News items will be stored in the database, then displayed on the news page in the site. And, there's an admin section where the news items are generated.

                  Thanks again.

                  Comment

                  • computerfox
                    Contributor
                    • Mar 2010
                    • 276

                    #10
                    Anytime. Please mark the question as answered.
                    As an added note, I understand. Some developers like going with the newest and coolest stuff, but it's not always NEEDED to jump on the wagon. The "old" version of MySQL was actually stable and functional. I believe some of the reasons they changed it was to enable OOP style coding and of course prevent database injections. I've been using the same version since I built my server and even wrote API's for the database and all is fine.

                    A responsible developer tries not to fix what's not broken. I bet all your old code needed was some styling and it could have looked really good.

                    Anyway, I'm rambling. Please mark the question as answered and have a great night.

                    Comment

                    Working...