Why is this Kicking out an Error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1347

    #1

    Tech Why is this Kicking out an Error?

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /blah/blah/domain.com/test/index.php on line 45

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /blah/blah/domain.com/test/index.php on line 48
    I'm pretty sure I have the mysqli query setup correctly (was originally using REXEGP but swapped it out for LIKE as REGEXP wasn't working either):

    $result = mysqli_query($con,"SELECT * FROM Recipe WHERE Ingredients REGEXP 'cat|dog';");
    // query of fetching posts
    $query = mysqli_query($con,"SELECT * FROM Recipe WHERE Title LIKE '%dog%' OR WHERE Title LIKE '%cat%' ORDER BY id LIMIT $page_limit OFFSET $page_offset");

    // check database is not empty
    if(mysqli_num_rows($query) > 0){
    Any ideas?
    Extreme Link List - v1.0
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1347

    #2
    Line 45 appears to have been a misplaced mysqli_close($con); which I just moved to the end of the page.

    Line 48 is still showing an error though.
    Extreme Link List - v1.0

    Comment

    • k33n
      Confirmed User
      • Feb 2009
      • 201

      #3
      Most likely your query is failing. Test it in phpmyadmin or in mysql server and see if you get results.

      Comment

      • k0nr4d
        Confirmed User
        • Aug 2006
        • 9231

        #4
        That error indicates your query is wrong.

        I don't want to know what you are cooking that has cat or dog as ingredient.
        Mechanical Bunny Media
        Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

        Comment

        • Publisher Bucks
          Confirmed User
          • Oct 2018
          • 1347

          #5
          Thanks.

          I'll redo it and try again.

          k0nr4d - Korean food
          Extreme Link List - v1.0

          Comment

          • Colmike9
            (>^_^)b
            • Dec 2011
            • 7217

            #6
            Originally posted by Publisher Bucks
            Thanks.

            I'll redo it and try again.

            k0nr4d - Korean food
            Ah, that makes sense now
            Join the BEST cam affiliate program on the internet!
            I've referred over $1.7mil in spending this past year, you should join in.
            I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

            Comment

            • Publisher Bucks
              Confirmed User
              • Oct 2018
              • 1347

              #7
              Okay so I had a play around and now I'm getting even more errors.

              The data that I want to display is showing what it needs to however, it isn't splitting the results across multiple pages of 10 like its supposed to.

              These are the errors I'm getting, which I assumed are related to a misplaced mysqli_close statement somewhere (?) however, I don't see where that close statement can be causing an issue:

              Warning: mysqli_query() expects at least 2 parameters, 1 given in /blah/blah/domain.com/test/index.php on line 47

              Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /blah/blah/domain.com/test/index.php on line 48

              Warning: mysqli_query() expects at least 2 parameters, 1 given in /blah/blah/domain.com/test/index.php on line 52

              Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /blah/blah/domain.com/test/index.php on line 53
              This is the code I'm using for the pagination, it displays the navigation on the bottom of the page with no issue, but only has a single page showing, even if there are supposed to be 5 or 6:

              if (isset($_GET['pageno'])) {
              $pageno = $_GET['pageno'];
              } else {
              $pageno = 1;
              }
              $no_of_records_per_page = 10;
              $offset = ($pageno-1) * $no_of_records_per_page;

              mysqli_connect("localhost","username","password"," database");
              // Check connection
              if (mysqli_connect_errno()){
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
              die();
              }

              $total_pages_sql = "SELECT COUNT(*) FROM Recipe";
              $result = mysqli_query($total_pages_sql);
              $total_rows = mysqli_fetch_array($result)[0];
              $total_pages = ceil($total_rows / $no_of_records_per_page);

              $sql = "SELECT * FROM Recipe LIMIT $offset, $no_of_records_per_page";
              $res_data = mysqli_query($sql);
              while($row = mysqli_fetch_array($res_data)){
              //here goes the data
              }
              Below that I have this in the HTML in order to display the correct results, which it is doing:

              <?php

              $con=mysqli_connect("localhost","databse","passwor d","username");
              $result = mysqli_query($con,"SELECT * FROM Recipe WHERE Ingredients REGEXP 'salt';");

              echo "<table border='0'>

              <tr>

              </tr>";

              while($row = mysqli_fetch_array($result))

              {

              $link = "/test/recipes.php?id=".$row['RecipeID'];

              echo "<tr>";

              echo "<a href = ". $link . ">" . $row['Title'] . "</a><br>";

              echo "</tr>";

              }

              echo "</table>";
              ?>
              Any ideas what I should be looking for error wise? From what I'm seeing online it appears to be something relating to either the mysqli close statement or something to do with global $con, which I know nothing about

              Initially I thought it may have been an issue with the variable naming but that does not seem to be the case.

              I'm at a loss, any pointers in the right direction would be appreciated.
              Extreme Link List - v1.0

              Comment

              • plsureking
                bored
                • Aug 2003
                • 4937

                #8
                hmm can i auto reply to this guy with the stackoverflow search results?



                use your brain a little bit. we all spent lots of sleepless nights figuring shit out.



                #

                Comment

                • k33n
                  Confirmed User
                  • Feb 2009
                  • 201

                  #9
                  Warning: mysqli_query() expects at least 2 parameters, 1 given
                  It is saying right there, solve this and the mysqli_fetch_array one will go away too, assuming that the query will not fail. Here is a hint to put you on the right track:

                  You are using mysqli_query correctly in other part of your script.

                  $con=mysqli_connect("localhost","databse","passwor d","username");
                  $result = mysqli_query($con,"SELECT * FROM Recipe WHERE Ingredients REGEXP 'salt';");

                  Comment

                  • fuzebox
                    making it rain
                    • Oct 2003
                    • 22367

                    #10
                    Originally posted by plsureking
                    hmm can i auto reply to this guy with the stackoverflow search results?



                    use your brain a little bit. we all spent lots of sleepless nights figuring shit out.
                    Can GFY admins merge all his beginner php questions into one thread?

                    Comment

                    • plsureking
                      bored
                      • Aug 2003
                      • 4937

                      #11
                      Originally posted by fuzebox
                      Can GFY admins merge all his beginner php questions into one thread?
                      i thought there used to be a nube forum? i lurked at cozyfrog a lot in my nube days lol

                      #

                      Comment

                      • Colmike9
                        (>^_^)b
                        • Dec 2011
                        • 7217

                        #12
                        Originally posted by plsureking
                        i thought there used to be a nube forum? i lurked at cozyfrog a lot in my nube days lol

                        #
                        There's the Educational Series subforum on here, but I think those OPs are mostly people explaining things as a tutorial and less people asking questions.

                        My noob days were me riding a bus downtown to the library and checking out books for C64 BASIC then always coming home with a bunch of sesame chicken.
                        Join the BEST cam affiliate program on the internet!
                        I've referred over $1.7mil in spending this past year, you should join in.
                        I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

                        Comment

                        • Publisher Bucks
                          Confirmed User
                          • Oct 2018
                          • 1347

                          #13
                          Originally posted by k33n
                          It is saying right there, solve this and the mysqli_fetch_array one will go away too, assuming that the query will not fail. Here is a hint to put you on the right track:

                          You are using mysqli_query correctly in other part of your script.
                          Thanks, I'll play with that tomorrow, just got out of the ER (12 fucking hours) and headed to bed.
                          Extreme Link List - v1.0

                          Comment

                          • zerovic
                            Confirmed User
                            • Apr 2010
                            • 1115

                            #14
                            Hey,

                            Open up a new PHP file and do it like this

                            $mysqli = new mysqli("localhost","username","password","database ");

                            if ($mysqli -> connect_errno) {
                            echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
                            exit();
                            }

                            $data = mysqli_query($mysqli, "SELECT * FROM Recipe WHERE Ingredients REGEXP 'salt';");
                            while($result = mysqli_fetch_array($data)) {
                            //here goes the data
                            }
                            Let me know if this works. The main difference is how you connect to your db.

                            I just tried this on one of my scripts and it works like charm.

                            Also, in my case, using

                            LIKE '%salt%'
                            was faster then

                            REGEXP 'salt';
                            Tried it on a POS script I wrote for a few bars and restaurants in my town and here's the result

                            SELECT * FROM `orders` WHERE `ordered_item` REGEXP 'cola';
                            Showing rows 0 - 24 (4683 total, Query took 0.0010 seconds.)

                            SELECT * FROM `orders` WHERE `ordered_item` LIKE '%cola%'
                            Showing rows 0 - 24 (4683 total, Query took 0.0007 seconds.)
                            Cheers,
                            z
                            php, html, jquery, javascript, wordpress - contact me at contact at zerovic.com

                            Comment

                            Working...