Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry!

I'm trying to print all sub-category items using my own defined PHP function. There is some thing wrong with this function. Could you please help me to find out the reason why this function doesn’t work. Thanks a lot in advance.

 

<?php
include ('connect.php');
include ('head.html');

echo '<div class="categ"><p class="b">Health</p>';
function retrieve_column($parent){
    $sql = "SELECT item FROM category WHERE parent = $parent ORDER BY item ASC";
    $r = mysqli_query($dbc, $sql);
        if (!$r) {
            echo "Couldn’t make a connection to DB.";
        } else {
            while($row = mysqli_fetch_assoc($r)){
                for ($i=0; $i < mysqli_num_rows($r); $i++) {
                    echo '<a class="text" href="">' . $row['item'][$i] . '</a><br />';
                }
            }
        }
    }

$par = 1;
retrieve_column($par);
mysqli_free_r($r);
mysqli_close($dbc);
?>

 

Link to post
Share on other sites

In what way is it not working? Like what isn't it doing that it should be doing or what is it doing that it shouldn't be? What is the output? What debugging steps have you taken and what were the results?

Link to post
Share on other sites

Hi Larry.

There is no any problem to get connection to database. I used mysqli_error($dbc) and print_r($dbc) to find out if there’s any error, but didn’t get any message. I expect that this function retrieve from database all items which parent value is $parent and show tehm all as a list. If I use a simple code to get connection to database for printing all these items of fields which has same parent value, it will succeed:

$sql = "SELECT 'suo_kat' FROM 'kategoria' WHERE 'parent' = '$parent' ORDER BY 'suo_kat' ASC";
$r = mysqli_query($dbc, $sql);
	if (!$r) {
		echo "Sorry. Try again later." . mysqli_error($dbc);
	} else {
		while($row = mysqli_fetch_assoc($r)){
			echo '<a class="teksti" href="">' . $row['suo_kat'][$i] . '</a><br />';
}

but I’ve to repeat so many times same code in same page to get accomplished the aim.

Link to post
Share on other sites

Ah, okay, that helps! Your function doesn't have access to the database connection so it can't execute the query. I would have thought it'd kick on the !$r error, though. But try passing $dbc to the function as an additional argument and see if it behaves better. 

Link to post
Share on other sites

Thanks for your attention Larry. I've done following changes to my code to confirm that function has access to database. But nothing happened!

function hae_sarake($dbc,$parent){
include ('mysqli_connect_0.php');
$sql = "SELECT 'suo_kat' FROM 'kategoria' WHERE 'parent' = '$parent' ORDER BY 'suo_kat' ASC";
$r = mysqli_query($dbc, $sql);
if (!$r) {
echo "Try again later!" . mysqli_error($dbc);
} else {
while($row = mysqli_fetch_assoc($r)){
for ($i=0; $i < mysqli_num_rows($r); $i++) {
echo '<a class="teksti" href="">' . $row['suo_kat'][$i] . '</a><br />';
}
}
}
}

 

Edited by phirux
Link to post
Share on other sites
38 minutes ago, phirux said:

Thanks for your attention Larry. I've done following changes to my code to confirm that function has access to database. But nothing happened!


function hae_sarake($dbc,$parent){
include ('mysqli_connect_0.php');
$sql = "SELECT 'suo_kat' FROM 'kategoria' WHERE 'parent' = '$parent' ORDER BY 'suo_kat' ASC";
$r = mysqli_query($dbc, $sql);
if (!$r) {
echo "Try again later!" . mysqli_error($dbc);
} else {
while($row = mysqli_fetch_assoc($r)){
for ($i=0; $i < mysqli_num_rows($r); $i++) {
echo '<a class="teksti" href="">' . $row['suo_kat'][$i] . '</a><br />';
}
}
}
}

 

The error message i've got as following:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for
the right syntax to use near ''kategoria' WHERE 'parent' = '1' ORDER BY 'suo_kat' ASC' at line 1

 

Link to post
Share on other sites

I've got the following error message:

Unknown column '$parent' in 'where clause'mysqli Object ( [affected_rows] => -1 [client_info] => mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $ [client_version] => 50010 [connect_errno] => 0 [connect_error] => [errno] => 1054 [error] => Unknown column '$parent' in 'where clause' [error_list] => Array ( [0] => Array ( [errno] => 1054 [sqlstate] => 42S22 [error] => Unknown column '$parent' in 'where clause' ) ) [field_count] => 0 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => 5.5.5-10.2.31-MariaDB [server_version] => 50505 [stat] => Uptime: 776155 Threads: 64 Questions: 118837232 Slow queries: 10 Opens: 910461 Flush tables: 1 Open tables: 2000 Queries per second avg: 153.110 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 3055259 [warning_count] => 0 ) 

 

Link to post
Share on other sites

I've done following changes to code and it's now working correctly:

function hae_sarake($parent, $dbc){
  $sql = "SELECT `suo_kat` FROM `kategoria` WHERE `parent` = '$parent' ORDER BY `suo_kat` ASC";

Thanks for your time.

Edited by phirux
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...