Debugging php function

Hi,
I m learning php and i am stuck at a place. i have created a function which throws a error saying no database selected. which i think is wrong because i pulled in global variable for the connection.i want to know how can i debug my function. here is the function

[COLOR=#0433FF][FONT=Monaco]function [/FONT][/COLOR][COLOR=#0220B2][FONT=Monaco]get_subj_get_id[/FONT][/COLOR][COLOR=#000000][FONT=Monaco]([/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco]$id[/FONT][/COLOR][COLOR=#000000][FONT=Monaco]){[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco][COLOR=#000000]	[/COLOR]global [COLOR=#3c96a6]$con[/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#007A04][FONT=Monaco][COLOR=#000000]	[/COLOR][COLOR=#3c96a6]$query3 [/COLOR][COLOR=#0433ff]= [/COLOR][COLOR=#4c6085]mysql_query[/COLOR][COLOR=#000000]([/COLOR]"select * from subjects where id = {[COLOR=#3c96a6]$id[/COLOR]} LIMIT BY 1"[COLOR=#000000]);[/COLOR][/FONT][/COLOR]
[FONT=Monaco]	query_failed([COLOR=#3c96a6]$query3[/COLOR]);[/FONT]
[COLOR=#4C6085][FONT=Monaco][COLOR=#000000]	[/COLOR][COLOR=#0433ff]if[/COLOR][COLOR=#000000]([/COLOR][COLOR=#3c96a6]$result [/COLOR][COLOR=#0433ff]= [/COLOR]mysql_fetch_array[COLOR=#000000]([/COLOR][COLOR=#3c96a6]$query3[/COLOR][COLOR=#000000])){[/COLOR][/FONT][/COLOR]
[COLOR=#3C96A6][FONT=Monaco][COLOR=#000000]		[/COLOR][COLOR=#4c6085]echo [/COLOR]$result[COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#0433FF][FONT=Monaco][COLOR=#000000]	}[/COLOR]else[COLOR=#000000]{[/COLOR][/FONT][/COLOR]
[COLOR=#0433FF][FONT=Monaco][COLOR=#000000]		[/COLOR]exit[COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#0433FF][FONT=Monaco][COLOR=#000000]		[/COLOR]return [COLOR=#6b77f8]null[/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[FONT=Monaco]	}[/FONT]
[FONT=Monaco]}[/FONT]

the query_failed is a function which dies when query doesn’t execute. however i checked in my php error logs and it says
PHP Parse error: syntax error, unexpected ‘’ (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
The line which it incidates is

e[COLOR=#4C6085][FONT=Monaco]cho [/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco]$sel_subjects[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco][[/FONT][/COLOR][COLOR=#007A04][FONT=Monaco]'menu_name'[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco]][/FONT][/COLOR][COLOR=#000000][FONT=Monaco];[/FONT][/COLOR]

i m assigning the function mentioned above into

[COLOR=#000000][FONT=Monaco]	[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco]if[/FONT][/COLOR][COLOR=#000000][FONT=Monaco]([/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco]isset[/FONT][/COLOR][COLOR=#000000][FONT=Monaco]([/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco]$_GET[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco][[/FONT][/COLOR][COLOR=#007A04][FONT=Monaco]'subj'[/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco]][/FONT][/COLOR][COLOR=#000000][FONT=Monaco])){[/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco][COLOR=#000000]		[/COLOR]$sel_sub [COLOR=#0433ff]= [/COLOR]$_GET[COLOR=#0433ff][[/COLOR][COLOR=#007a04]'subj'[/COLOR][COLOR=#0433ff]][/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#0433FF][FONT=Monaco][COLOR=#000000]		}[/COLOR]elseif[COLOR=#000000]([/COLOR]isset[COLOR=#000000]([/COLOR][COLOR=#3c96a6]$_GET[/COLOR][[COLOR=#007a04]'page'[/COLOR]][COLOR=#000000])){[/COLOR][/FONT][/COLOR]
[COLOR=#3C96A6][FONT=Monaco][COLOR=#000000]		[/COLOR]$sel_pg [COLOR=#0433ff]= [/COLOR]$_GET[COLOR=#0433ff][[/COLOR][COLOR=#007a04]'page'[/COLOR][COLOR=#0433ff]][/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#0433FF][FONT=Monaco][COLOR=#000000]	}[/COLOR]else[COLOR=#000000]{[/COLOR][/FONT][/COLOR]
[COLOR=#3C96A6][FONT=Monaco][COLOR=#000000]		[/COLOR]$sel_sub [COLOR=#0433ff]= [/COLOR][COLOR=#6b77f8]NULL[/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[COLOR=#3C96A6][FONT=Monaco][COLOR=#000000]		[/COLOR]$sel_pg [COLOR=#0433ff]= [/COLOR][COLOR=#6b77f8]NULL[/COLOR][COLOR=#000000];[/COLOR][/FONT][/COLOR]
[FONT=Monaco]	}[/FONT]
[COLOR=#000000][FONT=Monaco]	[/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco]$sel_subjects [/FONT][/COLOR][COLOR=#0433FF][FONT=Monaco]= [/FONT][/COLOR][COLOR=#000000][FONT=Monaco]get_subj_get_id([/FONT][/COLOR][COLOR=#3C96A6][FONT=Monaco]$sel_sub[/FONT][/COLOR][COLOR=#000000][FONT=Monaco]);
[/FONT][/COLOR]

wht am i doing wrong and why the errors on web browser is different then php error log? how can i fix it.

Can you try and paste your code again so that we don’t see all the font tags?

Don’t know what caused that,but here code


function get_subj_get_id($id){
	global $con;
	$query3 = mysql_query("select * from subjects where id = {$id} LIMIT BY 1");
	query_failed($query3);
	if($result = mysql_fetch_array($query3)){
		echo $result;
	}else{
		exit;
		return null;
	}
}


	if(isset($_GET['subj'])){
		$sel_sub = $_GET['subj'];
		}elseif(isset($_GET['page'])){
		$sel_pg = $_GET['page'];
	}else{
		$sel_sub = NULL;
		$sel_pg = NULL;
	}
	$sel_subjects = get_subj_get_id($sel_sub); 

and here i echo


echo $sel_subjects['menu_name'];

On a note i figured out my query was wrong, so i fixed it an removed by in LIMIT.i have print_r the query and ran in mysql and its running.i wanna debug the later part

You know you shouldn’t be using global variables, right? They’re horrible and shouldn’t be used like that. Makes debugging virtually impossible.

That could even be what’s causing the issue here. Problem is that because you’re using the ‘global’ keyword, it’s hard to debug now. There could be another function in your application somewhere that has killed off that connection or done something to it. This is why the ‘global’ keyword is evil.

If you take this code here and paste it into a file in its own right and then pass it a valid database connection, does it work then?

no it doesn’t. i used global $con, coz it was saying a error about database not selected.but nothing changed.

Can you paste the code that sets up the database connection?

this is the main file

$con = mysql_connect("localhost","root","root");query_failed($con);
$db_sel = mysql_select_db("widget_corp");
query_failed($db_sel);

and the function mentioned is in function.php, so the main file is including the function.php on main page

I don’t use the mysql_* functions because they’ve been deprecated for a long time (you should look into PDO or mysqli btw), but according to the docs, you should be able to do this:


$db_sel = mysql_select_db('widget_corp', $con);
if (!$db_sel) {
    die ('Can\\'t use widget_corp : ' . mysql_error());
}

So, can you try that and paste the output?

Also, shouldn’t you be passing the $con variable to the mysql_select_db() function?

Nope.the same error about the database.

Are you sure the connection details are correct? Do other queries work? Do other queries on the same db table work?

yes its work because when i getting the vlues of variable passed in url via get, it was echoing the values but after this function, it causing problem

I’m not sure I’m understanding you.

Are you saying this code works if you pass the value via $_GET?

I’m not even sure what works and what doesn’t now.

Can you paste all of the code involved? I feel like I’m only seeing a snapshot here so it’s hard to see what other factors are involved.

functions.php


<?php
function query_failed($value){
    if(!$value){
        die ("The Query failed because ".mysql_error());
    }
}
function subjects(){
    $query = mysql_query("select * from subjects");
    query_failed($query);
    return $query;
}
function pages($result){
    $query2 = mysql_query("select * from pages where subject_id = {$result}");
    query_failed($query2);
    return $query2;
}
function get_subj_get_id($id){
    global $con;
    global $db_sel;
    $query3 = "select * from subjects where id = {$id} LIMIT 1";
    //echo $query3;
    $result_set = mysql_query($query3) or die(' mysql error ' . mysql_error());
    //query_failed($result_set);
    if($result = mysql_fetch_array($result_set)){
        return $result;
    }else{
        return null;
    }
}
?>
and this is index

```php

&lt;?php include('includes/functions.php'); ?&gt;
&lt;?php
    if(isset($_GET['subj'])){
        //$sel_sub = $_GET['subj'];
        $sel_subjects = get_subj_get_id($_GET['subj']);
        }elseif(isset($_GET['page'])){
        $sel_pg = $_GET['page'];
    }else{
        $sel_sub = NULL;
        $sel_pg = NULL;
    }
    
?&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
&lt;?php
$con = mysql_connect("localhost","root","root");
query_failed($con);
$db_sel = mysql_select_db("widget_corp",$con);
query_failed($db_sel);
?&gt;
    &lt;body&gt;
        &lt;ul&gt;
            &lt;?php
            $query = subjects();
                while ($result = mysql_fetch_array($query)) {        
                    echo "&lt;li&gt;&lt;a href=\\"connection.php?subj={$result['id']}\\"&gt;{$result["menu_name"]}&lt;/a&gt;&lt;/li&gt;";
                        echo "&lt;ul&gt;";
                        $query2 = pages($result['id']);
                        
                        while ($result = mysql_fetch_array($query2)) {
                        query_failed($result);
                            
                        echo "&lt;li&gt;&lt;a href=\\"connection.php?page={$result['id']}\\"&gt;{$result["menu_name"]}&lt;a&gt;&lt;/li&gt;";
                            
                        }
                        echo"&lt;/ul&gt;";
            }
            ?&gt;
            &lt;?php echo $sel_subjects['menu_name']; ?&gt;
            &lt;?php $sel_subjects['menu_name'];?&gt;
            &lt;?php echo "$sel_pg";?&gt;
        &lt;/ul&gt;
    &lt;/body&gt;
&lt;/html&gt;

@nofel

Check your SQL statement there is no "LIMIT BY " but there is an "ORDER BY "

Your script"

“select * from subjects where id = {$id} LIMIT BY 1”

Perhaps try:

“SELECT * FROM subjects WHERE id = {$id} LIMIT 1”

I did fix it. i mentioned earlier.that issue was solved but its the main reason


	if(isset($_GET['subj'])){
		$sel_sub = $_GET['subj'];
		}elseif(isset($_GET['page'])){
		$sel_pg = $_GET['page'];
	}else{
		$sel_sub = NULL;
		$sel_pg = NULL;
	}
        $sel_subjects = get_subj_get_id($sel_sub); // when NULL

This is your query:


function get_subj_get_id($id){
	global $con;

        // select * from subjects where id = NULL LIMIT BY 1
        // should be: select * from subjects where id IS NULL LIMIT BY 1
	$query3 = mysql_query("select * from subjects where id = {$id} LIMIT BY 1");

	query_failed($query3);
	if($result = mysql_fetch_array($query3)){
		echo $result;
	}else{
		exit;
		return null;
	}
}

The solution would be to exit before calling that function when an ad has not been passed. Also, you will want to use intval() at the very least to prevent sql injection. Right now your query is subject to SQL injection.

oddz, like i said i already fixed the BY in limit, see my post number 14

help guys?

If you still want help please post your new problem, I thought it was fixed according to posts #14 and #18.