0

Having a problem with the 2nd query. when it gets to mysql_fetch_row - it kicks back

mysqli_fetch_row() expects parameter 1 to be resource, boolean given

Now I have also tried fetch assoc. But that as well kicks back an error. I have tried doing the 2nd query with '$id' and without it, yet still nothing. I think i am missing something very obvious.

I have went through many of the common post on here that refer to the error I am given but still haven't found a solution.

$con1 = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$con2 = mysqli_connect($dbhost,$dbuser2,$dbpass2,$dbname2);

// Check connections
if($con1)
    { echo("1st Connection Successful "); }
else
    { echo("1st Connection Failed "); }
if($con2)
    { echo("2nd Connection Successful "); }
else
    { echo("2nd Connection Failed "); }

//start first database query
$query = "SELECT id, email FROM ow_base_user ORDER by ID LIMIT 30";
$result = $con1->query($query);

while($row = $result->fetch_row()) {
  $rows[]=$row;

  $id = $row[0];
  $email_row = $row[1];

  $email_sub = substr($email_row, 0, 2);

  if ( $email_sub == "dk" ) {
    $email = "1"; }
  elseif ( $email_sub == "sv" ) {
    $email = "3"; }
  elseif ( $email_sub == "no" ) {
    $email = "4"; }
  elseif ( $email_sub == "nl" ) {
    $email = "5"; }
  elseif ( $email_sub == "de" ) {
    $email = "2";  }
  elseif ( $email_sub == "fi" ) {
    $email = "6"; }
  else {}

     if (isset($email)) {       
            //start 2nd database query
            $query2 = "SELECT country_id FROM website_user WHERE userId = $id LIMIT 1";
            $result2 = $con2->query($query2);

            $row2 = mysqli_fetch_row($result2);
            $country = $row2[6];

            if (isset($country)) {

    }

    if (DEVMODE) {
        echo "user id = ".$id." -- ";
        echo "email = ".$email." -- ";
        echo "COUNTRY = ".$country."<br />";
    }


    }
}

So changed the code around a bit to print out errors incase the query wasn't going through.

        If($result2 === false) {
              echo "error while executing mysql: " . mysqli_error($con2);
        } else {

after adding this in I received the error.

mysqli_error() expects parameter 1 to be mysqli, null given

Ok so, these errors where caused by a typo in the databse table name thanks for pointing out the error report all method in the comments.

UPDATE: so after fixing the connection issue. I receive Notice: Undefined offset: 5 in $country = $row2[6];

UPDATE: after change $country = $row2[0]; - This solved the final issue.

N8Core
  • 25
  • 6
  • Have you checked the errors coming back from the query - https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments – Nigel Ren Oct 21 '18 at 06:23
  • @NigelRen hey yes i was actually doing that and updated the post when I noticed your comment. – N8Core Oct 21 '18 at 06:28
  • First you wrote in your code: `$row = $result->fetch_row()` and that seemed to work. Second you then write `$row2 = mysqli_fetch_row($result2)` and that seems not to work. Why did you switch? (just curious?) the error seemed to indicate that result2 is not what mysqli_fetch_row is expecting? Why did the first query turn out a result set and the second a bool? – Caius Jard Oct 21 '18 at 06:28
  • Your `mysqli_error()` should use `$con2` - the database connection. – Nigel Ren Oct 21 '18 at 06:30
  • @NigelRen right i fixed that lol. also had a type on the database table.. that was the issue that was giving me the error. now I am getting | Notice: Undefined offset: 6 – N8Core Oct 21 '18 at 06:34
  • As your only returning `country_id`, which will be `$row[0]`, not sure what your expecting to get? – Nigel Ren Oct 21 '18 at 06:36
  • @NigelRen ok that fixed it, can I ask why is it $row[0}? is it because is specified it directly in the query? – N8Core Oct 21 '18 at 06:39
  • `mysqli_fetch_row()` returns an array of the results starting at element 0. – Nigel Ren Oct 21 '18 at 06:40
  • Possible duplicate of [How to get MySQLi error information in different environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) – Nigel Ren Oct 21 '18 at 06:40

1 Answers1

0

for development build you can use

error_reporting(E_ALL);
ini_set('display_errors', 1);

which showed the database table was misspelled. after that, had to change the which row was being pulled since it was only 1 row, instead of all rows.

N8Core
  • 25
  • 6