Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

I want to correct this piece of code

I found this piece of code but it doesn;t work for my case.
It doesn't coming out the fields
I want to show the fields contact_id, firstname, lastname and Birthdate
how can I do it?



<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");
// initiate it!
$nav = new navbar;
// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT * FROM names ";


// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");
// handle the returned result set
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";


}
echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
0
vage78
Asked:
vage78
  • 14
  • 13
  • 2
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
Where is the navclass?

Does the execute method return a results set or do you need to use the class to retrieve the data too?

0
 
vage78Author Commented:
Sorry RQuadling
this is navclass:

navbar.php

<?php
/*
Class navbar
Copyright Joao Prado Maia (jpm@musicalidade.net)

Sweet little class to build dynamic navigation links. Please
notice the beautiful simplicity of the code. This code is
free in any way you can imagine. If you use it on your own
script, please leave the credits as it is. Also, send me an
e-mail if you do, it makes me happy :)
 
Below goes an example of how to use this class:
===============================================
<?php
$nav = new navbar;
$nav->numrowsperpage = 3;
$sql = "SELECT * FROM links ";
$result = $nav->execute($sql, $db, "mysql");
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";
}
echo "<hr>\n";
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
*/

class navbar {
  // Default values for the navigation link bar
  var $numrowsperpage = 10;
  var $str_previous = "Previous page";
  var $str_next = "Next page";
  // Variables used internally
  var $file;
  var $total_records;

  // The next function runs the needed queries.
  // It needs to run the first time to get the total
  // number of rows returned, and the second one to
  // get the limited number of rows.
  //
  // $sql parameter :
  //  . the actual SQL query to be performed
  //
  // $db parameter :
  //  . the database connection link
  //
  // $type parameter :
  //  . "mysql" - uses mysql php functions
  //  . "pgsql" - uses pgsql php functions
  function execute($sql, $db, $type = "mysql") {
    global $total_records, $row, $numtoshow;

    $numtoshow = $this->numrowsperpage;
    if (!isset($row)) $row = 0;
    $start = $row * $numtoshow;
    if ($type == "mysql") {
      $result = mysql_query($sql, $db);
      $total_records = mysql_num_rows($result);
      $sql .= " LIMIT $start, $numtoshow";
      $result = mysql_query($sql, $db);
    } elseif ($type == "pgsql") {
      $result = pg_Exec($db, $sql);
      $total_records = pg_NumRows($result);
      $sql .= " LIMIT $numtoshow, $start";
      $result = pg_Exec($db, $sql);
    }
    return $result;
  }

  // This function creates a string that is going to be
  // added to the url string for the navigation links.
  // This is specially important to have dynamic links,
  // so if you want to add extra options to the queries,
  // the class is going to add it to the navigation links
  // dynamically.
  function build_geturl()
  {
    global $REQUEST_URI, $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS;

    list($fullfile, $voided) = explode("?", $REQUEST_URI);
    $this->file = $fullfile;
    $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;
    reset ($cgi);
    while (list($key, $value) = each($cgi)) {
      if ($key != "row")
        $query_string .= "&" . $key . "=" . $value;
    }
    return $query_string;
  }

  // This function creates an array of all the links for the
  // navigation bar. This is useful since it is completely
  // independent from the layout or design of the page.
  // The function returns the array of navigation links to the
  // caller php script, so it can build the layout with the
  // navigation links content available.
  //
  // $option parameter (default to "all") :
  //  . "all"   - return every navigation link
  //  . "pages" - return only the page numbering links
  //  . "sides" - return only the 'Next' and / or 'Previous' links
  //
  // $show_blank parameter (default to "off") :
  //  . "off" - don't show the "Next" or "Previous" when it is not needed
  //  . "on"  - show the "Next" or "Previous" strings as plain text when it is not needed
  function getlinks($option = "all", $show_blank = "off") {
    global $total_records, $row, $numtoshow;

    $extra_vars = $this->build_geturl();
    $file = $this->file;
    $number_of_pages = ceil($total_records / $numtoshow);
    $subscript = 0;
    for ($current = 0; $current < $number_of_pages; $current++) {
      if ((($option == "all") || ($option == "sides")) && ($current == 0)) {
        if ($row != 0)
          $array[0] = '<A HREF="' . $file . '?row=' . ($row - 1) . $extra_vars . '">' . $this->str_previous . '</A>';
        elseif (($row == 0) && ($show_blank == "on"))
          $array[0] = $this->str_previous;
      }

      if (($option == "all") || ($option == "pages")) {
        if ($row == $current)
          $array[++$subscript] = ($current > 0 ? ($current + 1) : 1);
        else
          $array[++$subscript] = '<A HREF="' . $file . '?row=' . $current . $extra_vars . '">' . ($current + 1) . '</A>';
      }

      if ((($option == "all") || ($option == "sides")) && ($current == ($number_of_pages - 1))) {
        if ($row != ($number_of_pages - 1))
          $array[++$subscript] = '<A HREF="' . $file . '?row=' . ($row + 1) . $extra_vars . '">' . $this->str_next . '</A>';
        elseif (($row == ($number_of_pages - 1)) && ($show_blank == "on"))
          $array[++$subscript] = $this->str_next;
      }
    }
    return $array;
  }
}
?>


0
 
Richard QuadlingSenior Software DeverloperCommented:
What version of PHP?

Try changing ...

 $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;

to

 $cgi = $REQUEST_METHOD == 'GET' ? $_GET : $_POST;

Richard
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vage78Author Commented:
php 4
0
 
Richard QuadlingSenior Software DeverloperCommented:
4.?

V4.2.0+ is different to V4.1.*-

Try the above mod and see if that makes any difference.

This class is written using the older (and hopefully soon defunct) register_globals settings.

Richard.
0
 
vage78Author Commented:
php 4_3.0.2 win 32
0
 
vage78Author Commented:
Hi QQuadling
I'm sorry maybe I have not explain very well.
It is working very well as navigation next previous but how can I show also the contents of my database
I have myfirstdb the name of my database and my table is names with
FirstName, LastaName, birthdate and contact_id
How can Ishow them
Can you please correct my code in order to show them
0
 
Richard QuadlingSenior Software DeverloperCommented:
<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");

// initiate it!
$nav = new navbar;

// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT FirstName,LastName,BirthDate,ContactID FROM names ";

// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");

while($row = mysql_fetch_assoc($result)
     {
     echo "Name : {$row['FirstName']} {$row['LastName']}<br>Date of birth : " . date('Y-m-d',$row['BirthDate']) . "<br>Contact ID : {$row['ContactID']}<br><br>";
     }


echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
 echo $links[$y] . "&nbsp;&nbsp;";
}
?>


sort of thing?
0
 
vage78Author Commented:
Hi RQuadling
I'm receiving this error :

Parse error: parse error, unexpected '{' in c:\program files\apache group\apache\htdocs\exam1.php on line 21
0
 
vage78Author Commented:
the problem seem to be :


while($row = mysql_fetch_assoc($result)
    { //here why ???
0
 
Richard QuadlingSenior Software DeverloperCommented:
OOps.

My typo..

while($row = mysql_fetch_assoc($result)


should be


while($row = mysql_fetch_assoc($result))

extra ) at the end.

Richard.
0
 
vage78Author Commented:
Hi RQuadling
the problem still exists

Parse error: parse error, unexpected '{' in c:\program files\apache group\apache\htdocs\exam1.php on line 21

0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you put the entire script here.
0
 
vage78Author Commented:
Hi RQuadling
this is the code

<?php
// database connection stuff
$db = mysql_connect("", "", "");
mysql_select_db("myfirstdb", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");


// including the navbar class
include("./navbar.php");
// initiate it!
$nav = new navbar;
// set how many records to show at a time
$nav->numrowsperpage = 10;
$sql = "SELECT * FROM names ";


// the third parameter of execute() is optional
$result = $nav->execute($sql, $db, "mysql");
// handle the returned result set
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++) {
  $data = mysql_fetch_object($result);
  echo $data->url . "<br>\n";


}
echo "<hr>\n";
// build the returned array of navigation links
$links = $nav->getlinks("all", "on");
for ($y = 0; $y < count($links); $y++) {
  echo $links[$y] . "&nbsp;&nbsp;";
}
?>
0
 
Richard QuadlingSenior Software DeverloperCommented:
I can see nothing wrong with the code you've got.

Hmmm.

<?php
$db = mysql_connect('', '', '');
mysql_select_db('myfirstdb', $db) or die(mysql_errno() . ': ' . mysql_error() . '<br>');
include('./navbar.php');
$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names;';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
     {
     $data = mysql_fetch_object($result);
     echo $data->url . "<br>\n";
     }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
     {
     echo $links[$y] . '&nbsp;&nbsp;';
     }
?>

(Just reformatted and converted " to ' for non embedded code).

Richard.
0
 
vage78Author Commented:
Hi RQuadling
I receive this error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\exam2.php on line 9

--------------------------------------------------------------------------------
Previous page  1  2  3  Next page  

PS
without any DATA why??????
0
 
Richard QuadlingSenior Software DeverloperCommented:
What happened to the $sql?

Can you add ...

error_reporting(E_ALL);

to the top of the code and see what other errors are produced?

Also, try dropping the ; from the sql statement.

I think the class is not working properly or not returning the correct information.

The warning is synomymous with a False response from mysql_query.

Richard.
0
 
vage78Author Commented:
Hi RQyadling
I received this error:
Notice: Undefined property: url in c:\program files\apache group\apache\htdocs\exam2.php on line 16


Notice: Undefined property: url in c:\program files\apache group\apache\htdocs\exam2.php on line 16


--------------------------------------------------------------------------------

Notice: Undefined offset: 1 in c:\program files\apache group\apache\htdocs\navbar.php on line 85

Notice: Undefined variable: query_string in c:\program files\apache group\apache\htdocs\navbar.php on line 96
Previous page  1  2  3  Next page  
0
 
Richard QuadlingSenior Software DeverloperCommented:
Looks like the navbar class is not right.

What version of PHP is the server running?
0
 
vage78Author Commented:
php.4.3.0 win 32
But the strange thing is that without requesting data
the navbar is working. It gives me the
menu previous pages 1 2 3 etc
when I'm trying to take output from data then comes the problem
0
 
Richard QuadlingSenior Software DeverloperCommented:
Do you have this online?
0
 
baeumlCommented:
Hi!

Your script is not working, because in $db you only have a connection link to mysql, but not to a specific database.

Your code should be

$connection = mysql_connect('', '', '');
$db = mysql_select_db('myfirstdb', $connection);

Then it should work...

-baeuml
0
 
Richard QuadlingSenior Software DeverloperCommented:
Quite often, users are assigned to a default database, so the mysql_select_db can often be redundant.

Richard.
0
 
baeumlCommented:
Yeah right, but the navbar class expects the $db to be a database link and not a connection link...

If it's a connection link, the navbar class doesn't query the database and therefor mysql_num_rows produces an error because $result is empty.

-baeuml
0
 
Richard QuadlingSenior Software DeverloperCommented:
Yep!
0
 
vage78Author Commented:
Hi baeuml
the error now is "
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\exam2.php on line 4

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\exam2.php on line 7 "

and my code is
<?php
//exam2.php
//$db = mysql_connect('', '', '');
$db = mysql_select_db('myfirstdb', $connection);
error_reporting(E_ALL);

mysql_select_db('myfirstdb', $db) or die(mysql_errno() . ': ' . mysql_error() . '<br>');
include('./navbar.php');
$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
    {
    $data = mysql_fetch_object($result);
    echo $data->url . "<br>\n";
    }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
    {
    echo $links[$y] . '&nbsp;&nbsp;';
    }
?>


0
 
Richard QuadlingSenior Software DeverloperCommented:
No!!!

mysql_select_db returns a boolean and simply acts upon the connection.

$db in the class IS the db connection!

Look at line in the class ..

 $result = mysql_query($sql, $db);


and then the PHP definition of mysql_query ...

resource mysql_query ( string query [, resource link_identifier [, int result_mode]])

Link Idenitifier, not database name (string) and definitely NOT boolean!!!


Try ...


<?php
//exam2.php

// Set error logging BEFORE any action takes place!
error_reporting(E_ALL);

// Include any required classes.
include('./navbar.php');

// Make a connection to the DB server.
$db = mysql_connect('put_db_server_name_here', 'put_your_db_user_name_here', 'put_your_db_password_here');

// Select your db (optional, but may be necessary if your user is not given a specific db already).
mysql_select_db(put_your_db_name_here', $db);

$nav = new navbar;
$nav->numrowsperpage = 10;
$sql = 'SELECT * FROM names';
$result = $nav->execute($sql, $db, 'mysql');
$rows = mysql_num_rows($result);
for ($y = 0; $y < $rows; $y++)
   {
   $data = mysql_fetch_object($result);
   echo $data->url . "<br>\n";
   }
echo "<hr>\n";
$links = $nav->getlinks('all', 'on');
for ($y = 0; $y < count($links); $y++)
   {
   echo $links[$y] . '&nbsp;&nbsp;';
   }
?>


You also took out the connection to the Db!!!

So...

0
 
vage78Author Commented:
No I receive this error
"Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in c:\program files\apache group\apache\htdocs\exam2.php on line 18"
0
 
Richard QuadlingSenior Software DeverloperCommented:
mysql_select_db('put_your_db_name_here', $db);

Missed opening quote.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 14
  • 13
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now