1

I am rewriting a php file which gets persons and their details from an MYSQL DB and layering it (to protect from hacking, etc...)

PHP file that I'm editing currently goes like this:

<div id='idLeft'>
<?php
@session_start();
$_SESSION['auth'] = 'ok';
$db_server = "localhost";
$db_user = "username";
$db_pass = "******";
$db_database = "mydatabase";
    $db_con = @mysql_connect($db_server, $db_user, $db_pass);
if(!$db_con) die("Database error!");
if(!@mysql_select_db($db_database, $db_con)) die("Database error!");
mysql_query("SET NAMES UTF8");

$query = "SELECT osobaID,ime,prezime FROM ssa_osoba";
$rs = mysql_query($query);
$i=0;
while( $row = mysql_fetch_assoc($rs)) {
?>
<div class='osoba'>
    <div class="brojosobe">
    <span class="otekst"><?php echo $i++.'.'; ?></span>
    </div>
    <div class ="imeprezime">
    <span class="otekst"><?php echo $row['ime'].' '.$row['prezime'];?></span>
    </div>
        <div class='hidden'><?php echo $row['osobaID'];?></div>
</div>
<?php
}
?>

I rewrote it like this:

 <?php
@session_start();
try
{   
    require "inc/func.php"; 
    require_once("db/db.php");
    $db = db_layer::get();
    $DBCONNECTION = $db->getHandle();
}
catch( Exception $Ex )
{
    exit();
    //die ( $Ex->getMessage() );
    //echo $Ex;
}

And the part that should output it goes like this.

    <?php
    $i=0;
while( $row = get_osoba() ) {
    ?>
<div class='osoba'>
    <div class="brojosobe">
    <span class="otekst"><?php echo$i++.'.'; ?></span>
    </div>
    <div class ="imeprezime">
    <span class="otekst"><?php echo $row['ime'].' '.$row['prezime'];?></span>
    </div>
<?php
}
?>

My functions are in /inc folder (one file functions.php) I have these functions in it:

function osoba_query()
{
$query ="SELECT osobaID,ime,prezime FROM ssa_osoba";
return mysql_query($query);
}
function get_osoba()
{
$rs =  osoba_query();
static $row = mysql_fetch_assoc($rs);
return $row;
}

But it always outputs only the first row. mysql_fetch_assoc should move the pointer to the next row, but in this case it doesn't.

What should I do?

EDIT1:

If i do this:

$query ="SELECT osobaID,ime,prezime FROM ssa_osoba";
$rs = mysql_query($query);
function get_osoba()
{
global $rs;
$row = mysql_fetch_assoc($rs);
return $row;
}

PHP outputs: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in D:\Xampp\htdocs\ssa\ilayer\inc\func.php on line 7

6
  • 6
    Please wipe those database credentials immediately from your question and change them on your server. Commented Apr 4, 2012 at 23:45
  • For future reference, please remember to erase database credentials prior to posting them on the Internet. Commented Apr 4, 2012 at 23:54
  • 2
    I wonder how many people per week get a nice, friendly DROP DATABASE by doing that kind of nonsense. So tempting. Commented Apr 5, 2012 at 0:02
  • Credentials are old, and useless :) But thanks, will remember in the future. Commented Apr 5, 2012 at 0:03
  • @SeeBeen: Everybody writes that after this was highlighted. Some even don't have it changed when they comment in such a way. Just change the credentials. Commented Jun 15, 2012 at 16:21

1 Answer 1

2

You're re-running the query every time osoba_query() is called, which resets everything.

Don't do that.

Sign up to request clarification or add additional context in comments.

1 Comment

If i do this: $query ="SELECT osobaID,ime,prezime FROM ssa_osoba"; $rs = mysql_query($query); function get_osoba() { global $rs; $row = mysql_fetch_assoc($rs); return $row; } PHP outputs: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in D:\Xampp\htdocs\ssa\ilayer\inc\func.php on line 7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.