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
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;
}
}
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?
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?
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.