SQL (multiple) query error

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

    #1

    Tech SQL (multiple) query error

    Any of the programmer types on here able to tell me why this is kicking out the following error?

    [20-May-2022 23:33:21 America/Chicago] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /blah/blah/domain.com/members/test/index.php on line 62
    [20-May-2022 23:33:21 America/Chicago] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /blah/blah/domain.com/members/test/index.php on line 63
    [20-May-2022 23:33:21 America/Chicago] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /blah/blah/domain.com/members/test/index.php on line 74
    I've tried 2 different sets of php code and its still kicking errors out on me

    Here was the original code:

    <?php

    $con=mysqli_connect("localhost","db","pass","user" );

    $result = mysqli_query($con,"SELECT * FROM Title WHERE Category REGEXP 'air' AND REGEXP 'fryer' ORDER BY Title ASC;");

    echo "<table border='0'>

    <tr>

    </tr>";

    while($row = mysqli_fetch_array($result))

    {

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

    echo "<tr>";

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

    echo "</tr>";

    }

    echo "</table>";

    mysqli_close($con);

    ?>
    This is what I'm currently using (ditched regexp):

    <?php

    $con=mysqli_connect("localhost","db","pass","user" );

    $sql = "SELECT * FROM Recipe WHERE Title RLIKE 'fryer' AND RLIKE 'Air' ORDER BY Title ASC;";
    $result = mysqli_query($con,$sql);
    while($r = mysqli_fetch_array($result))
    if (!$check1_res) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
    }
    echo "<table border='0'>

    <tr>

    </tr>";

    while($row = mysqli_fetch_array($result))

    {

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

    echo "<tr>";

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

    echo "</tr>";

    }

    echo "</table>";

    mysqli_close($con);

    ?>
    Basically, I'm trying to get a set of recipes displayed where they contain 2 categories, in this instance 'air' and 'fryer' which should display any Air Fryer recipes, but it doesnt, any help would be greatly appreciated, this is my first time screwing with multipe queries to display results
    Extreme Link List - v1.0
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #2
    Oh, i should add, when using only 1 query, the code works perfectly
    Extreme Link List - v1.0

    Comment

    • redwhiteandblue
      Bollocks
      • Jun 2007
      • 2793

      #3
      Is this all of the code? What are lines 62, 63 and 74?
      Interserver unmanaged AMD Ryzen servers from $73.00

      Comment

      • CurrentlySober
        Too lazy to wipe my ass
        • Aug 2002
        • 38941

        #4
        Originally posted by redwhiteandblue
        Is this all of the code? What are lines 62, 63 and 74?
        Easy, they are the lines that come between lines 61 and 64, plus the line between 73 & 75 respectively... come on... keep up


        👁️ 👍️ 💩

        Comment

        • sarettah
          see you later, I'm gone
          • Oct 2002
          • 14297

          #5
          Not sure how any of that code is running.

          You mention 2 queries but you only have one query in there. It has 2 conditions, but it is one query.

          $sql = "SELECT * FROM Recipe WHERE Title RLIKE 'fryer' AND RLIKE 'Air' ORDER BY Title ASC;";
          Needs to be:
          $sql = "SELECT * FROM Recipe WHERE Title RLIKE 'fryer' AND Title RLIKE 'Air' ORDER BY Title ASC";

          You have to have the field being interrogated listed in each condition.

          I am not sure if that is your only problem.

          PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /blah/blah/domain.com/members/test/index.php on line 62
          This would indicate that you did not have a good connection. The first parameter on mysqli_query is the connection. The error message says that is null which would mean a bad connection.

          The second set of code that was posted should not even run because of the errors in there. It shows 2 different wile loops but only one is closed. That doesn't matter though because the 2 different while loops make no sense. You are looping through the resuult and inside the loop you are looping through the results again. I don't think you want ot do that.

          .
          All cookies cleared!

          Comment

          • redwhiteandblue
            Bollocks
            • Jun 2007
            • 2793

            #6
            Try replacing the line

            $con=mysqli_connect("localhost","db","pass","user" );
            with

            $con=mysqli_connect("localhost","db","pass","user" ) or die("Error connecting to database");
            and if you see the message "Error connecting to database"...
            Interserver unmanaged AMD Ryzen servers from $73.00

            Comment

            • redwhiteandblue
              Bollocks
              • Jun 2007
              • 2793

              #7
              BTW the parameters need to be in the order ("localhost", user, password, database_name)

              But you said it works fine if you only have one query so it may be you had the correct code for connecting to the database. Try using the query Sarettah described, the ones you wrote don't make sense.
              Interserver unmanaged AMD Ryzen servers from $73.00

              Comment

              • Publisher Bucks
                Confirmed User
                • Oct 2018
                • 1330

                #8
                i fixed it early this morning.

                This issue was a missing Category as Sarettah mentioned above.

                SELECT * FROM Recipe WHERE Category RLIKE 'air' AND Category RLIKE 'fryer' ORDER BY Title ASC LIMIT 60
                I appreciate all the responses

                Must stop coding while drinking Jameson, that will solve most of the issues
                Extreme Link List - v1.0

                Comment

                Working...