SQL Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

SQL Function

Post 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:

Code: Select all

sql("SELECT * FROM table");
and outputs the data in an array, e.g:

Code: Select all

$row['article_title'];
But, if I try and use the function again in the page, it does not work.
Any ideas?
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

Re: SQL Function

Post 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)) 
{
     ....
}
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: SQL Function

Post by jaceinla »

You're saying if you refresh the page it doesn't work again? Or is there an event triggering it?
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

Re: SQL Function

Post 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)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL Function

Post 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.
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

Re: SQL Function

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL Function

Post 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.
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

Re: SQL Function

Post by ralphchadkirk »

Thank you, I understand now.

Would using mysqli_query help, or would another function have to be written?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL Function

Post 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.
ralphchadkirk
Forum Newbie
Posts: 6
Joined: Thu Dec 09, 2010 1:02 pm

Re: SQL Function

Post by ralphchadkirk »

What in my code would have to be changed? - I assume separating the type of database from the querying?
Post Reply