Jump to content

php function stopped working


budimir
Go to solution Solved by tryingtolearn,

Recommended Posts

I'm rewriting my aplication to work with mysqli and one function that I have created is not working anymore. I don't know why connection to db is not working anymore. It worked on mysql.

 

Can you see what is the problem?

function zamjena_broja($kataloski_broj){
    
    global $conn_mysqli;
    
    $kataloski_broj_data = array();

    //Traženje zamjenskog broja
    $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj
                     FROM zamjene_brojeva
                     WHERE glavni_broj = '$kataloski_broj'
                     OR pocetni_broj = '$kataloski_broj'";
    $rezultat_zamjena = mysqli_query($conn_mysqli, $upit_zamjena) or die (mysqli_error($conn_mysqli));
    while($row = mysqli_fetch_array($rezultat_zamjena)){
        $kataloski_broj_data[] = $row["zamjenski_broj"];
        $kataloski_broj_data[] = $row["pocetni_broj"];

    }
      
    $kataloski_broj = array_map("unserialize", array_unique(array_map("serialize", $kataloski_broj_data)));
    //var_dump($kataloski_broj);
          
    return $kataloski_broj;

}

My connection:

<?php
//Veza na bazu - mysqli protokol
$DBServer = 'localhost'; // e.g 'localhost' or '192.168.1.100'
$DBUser   = 'xxxx';
$DBPass   = 'xxxx';
$DBName   = 'xxxdb';

$conn_mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
global $conn_mysqli;
 
// check connection
if ($conn_mysqli->connect_error) {
  trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}
?>
Link to comment
Share on other sites

I think I finally got it working. One of the problems I found a case where function couldn't return any data. Second thing is that the global connection wasn't setup properly for some reason. I don't understand why no error messages came up.  Anyways, now it's working.

 

Thank you for all you're help!

Link to comment
Share on other sites

if you are seeing no errors, it could be because you are doing a redirect on the page and you have php's output_buffing on, either in your code or in your php.ini and any error output was discarded due to the buffing/redirect.

 

also, the global $conn_mysqli; statement, where you are making the database connection at, doesn't do anything (well it does, by wasting your time typing that and wasting some time when the code runs.) the only place the global keyword does anything is inside of a function definition, and even there you should not use it, since it breaks the black-box model for function definitions. you should pass ALL external values into functions as call-time parameters.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.