Page 1 of 1
SQL Function
Posted: Thu Dec 09, 2010 1:07 pm
by ralphchadkirk
Hi all,
I've written a small function in PHP, as follows: (I'm aware there isn't any security stuff in there yet)
Code: Select all
function sql($query)
{
include("config.common.php");
global $dbName;
@mysql_select_db($dbName) or die("<h1>Database Error</h1>Unable to connect to database");
$sql = "" . $query . "";
$result = mysql_query($sql);
define("result",$result);
unset($sql,$query);
}
The function works perfectly in a page:
and outputs the data in an array, e.g:
But, if I try and use the function
again in the page, it does not work.
Any ideas?
Re: SQL Function
Posted: Thu Dec 09, 2010 1:13 pm
by ralphchadkirk
Sorry - forgot to mention, the code used in a page is as follows:
Code: Select all
sql("SELECT * FROM table;");
while($row = mysql_fetch_array(result))
{
....
}
Re: SQL Function
Posted: Thu Dec 09, 2010 1:38 pm
by jaceinla
You're saying if you refresh the page it doesn't work again? Or is there an event triggering it?
Re: SQL Function
Posted: Thu Dec 09, 2010 1:42 pm
by ralphchadkirk
jaceinla wrote:You're saying if you refresh the page it doesn't work again? Or is there an event triggering it?
No, if I try and carry out two queries on the same page the second does not work.
e.g:
Code: Select all
sql("SELECT * FROM table;");
while($row = mysql_fetch_array(result))
{
....
}
(works)
and on the same page:
Code: Select all
sql("SELECT * FROM users;");
while($row = mysql_fetch_array(result))
{
....
}
(does not work)
Re: SQL Function
Posted: Thu Dec 09, 2010 2:12 pm
by requinix
1. Constants can only have scalar values. Resources, like the ones mysql_query return, are not valid.
2. You can't change the value of or redefine a constant.
Try something else.
Re: SQL Function
Posted: Thu Dec 09, 2010 2:14 pm
by ralphchadkirk
tasairis wrote:1. Constants can only have scalar values. Resources, like the ones mysql_query return, are not valid.
2. You can't change the value of or redefine a constant.
Try something else.
Could you explain the 'something else' in some more detail, and why mysql_query return is not valid? I'm afraid I don't understand!
Re: SQL Function
Posted: Thu Dec 09, 2010 10:48 pm
by requinix
"Something else" means "not using constants for your MySQL stuff". It's also supposed to imply "using something that's actually valid for PHP".
mysql_query returns either true/false (for queries that don't return a resultset) or a mysql resource (for those that do). Constants are only supposed to have simple scalar values, such as true/false, a number, or a string. Resources are not allowed. But that's what you try to do.
Even if it did work, which it may very well do at the moment, it won't allow you to run two queries on a page. Because doing so would involving changing the value of a constant. Which isn't allowed. Because it's a constant.
Re: SQL Function
Posted: Fri Dec 10, 2010 8:01 am
by ralphchadkirk
Thank you, I understand now.
Would using mysqli_query help, or would another function have to be written?
Re: SQL Function
Posted: Fri Dec 10, 2010 11:53 am
by requinix
ralphchadkirk wrote:Would using mysqli_query help, or would another function have to be written?
Lateral move. The mysqli extension is better (in my opinion) than the mysql extension, but which you use won't solve the problems with your code.
Your code has to be changed. If you want to start using mysqli too then that's great.
Re: SQL Function
Posted: Fri Dec 10, 2010 12:05 pm
by ralphchadkirk
What in my code would have to be changed? - I assume separating the type of database from the querying?