SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question 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
    Code PHP:
    [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
    Code PHP:
    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
    Code PHP:
    [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.
    All those who wander aren't lost.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Can you try and paste your code again so that we don't see all the font tags?

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't know what caused that,but here code
    Code PHP:
    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;
    	}
    }
    Code PHP:
    	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
    Code PHP:
    echo $sel_subjects['menu_name'];
    All those who wander aren't lost.

  4. #4
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    All those who wander aren't lost.

  5. #5
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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?

  6. #6
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    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.
    All those who wander aren't lost.

  7. #7
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nofel View Post
    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?

  8. #8
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    this is the main file
    Code PHP:
    $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
    All those who wander aren't lost.

  9. #9
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $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?

  10. #10
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nope.the same error about the database.
    All those who wander aren't lost.

  11. #11
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the connection details are correct? Do other queries work? Do other queries on the same db table work?

  12. #12
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    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
    All those who wander aren't lost.

  13. #13
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nofel View Post
    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.

  14. #14
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    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
    Code 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
    [HIGHLIGHT=PHP]
    <?php include('includes/functions.php'); ?>
    <?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;
        }
     
    ?>
    <html>
        <head>
            <title></title>
        </head>
    <?php
    $con = mysql_connect("localhost","root","root");
    query_failed($con);
    $db_sel = mysql_select_db("widget_corp",$con);
    query_failed($db_sel);
    ?>
        <body>
            <ul>
                <?php
                $query = subjects();
                    while ($result = mysql_fetch_array($query)) {        
                        echo "<li><a href=\"connection.php?subj={$result['id']}\">{$result["menu_name"]}</a></li>";
                            echo "<ul>";
                            $query2 = pages($result['id']);
     
                            while ($result = mysql_fetch_array($query2)) {
                            query_failed($result);
     
                            echo "<li><a href=\"connection.php?page={$result['id']}\">{$result["menu_name"]}<a></li>";
     
                            }
                            echo"</ul>";
                }
                ?>
                <?php echo $sel_subjects['menu_name']; ?>
                <?php $sel_subjects['menu_name'];?>
                <?php echo "$sel_pg";?>
            </ul>
        </body>
    </html>

    [/HIGHLIGHT]
    All those who wander aren't lost.

  15. #15
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,838
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @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"
    Last edited by John_Betong; Aug 17, 2012 at 09:30. Reason: spelling: not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  16. #16
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did fix it. i mentioned earlier.that issue was solved but its the main reason
    All those who wander aren't lost.

  17. #17
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
        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:

    PHP Code:
    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.
    The only code I hate more than my own is everyone else's.

  18. #18
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oddz, like i said i already fixed the BY in limit, see my post number 14
    All those who wander aren't lost.

  19. #19
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    help guys?
    All those who wander aren't lost.

  20. #20
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,838
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by nofel View Post
    help guys?
    If you still want help please post your new problem, I thought it was fixed according to posts #14 and #18.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  21. #21
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    If you still want help please post your new problem, I thought it was fixed according to posts #14 and #18.
    nope, the problem remain the same.the code remain same as of post #14
    All those who wander aren't lost.

  22. #22
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What error are you now seeing, and where does that appear?

  23. #23
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    What error are you now seeing, and where does that appear?
    I was able to find the problem. i was silly mistake. i was assigning the get values above the connect function. now its working. oh silly me but thnx for ur help guys
    All those who wander aren't lost.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •