Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Calling Db Function


how do i call a db function from with in a db function which returns a result. can some one pls help me with the syntax, when i call a function from with in a function it says funtion does not allow to return a result set where as it returns only one values.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Calling A Php Function From A SELECT Statement
How can I call a php fucntion from within a MySQL SELECT statement?

What I have is:

$result = mysql_query("SELECT GetCountry(IPAddrss), IPAddrss FROM pgstats");

GetCountry is my php function, and it does work when I call it like this:

echo GetCountry($ip);

What am I doing wrong?

Failure Calling User-defined Function In Procedure
I have de following (a function that I created and a procedure that I also created) :

This function checks if already exist a user in the table Users. If exists it returns the user's Uid, otherwise returns NULL.

CREATE FUNCTION Check_User (_Username VARCHAR (64), _Password VARCHAR (64))
RETURNS SMALLINT
DETERMINISTIC
BEGIN
DECLARE Ret_Val SMALLINT;

SELECT Uid INTO Ret_Val FROM Users
WHERE Username = _Username AND Password = SHA1(_Password);

RETURN Ret_Val;
END;

The procedure makes use of the function defined above. First it check if there is a user with a particular user/password, if not then creates the new user : Code:

Calling Another Database?
I've encountered an issue using foreign keys with mysql: is it possible to call
another local database from within a table of a database? For example, if I've
got a database consisting of car manufacturers and another one consisting of
retailers, then I'd like to have the retailer database to reference the manufacturer
database in some way. For example:

CREATE TABLE retailer1 (
FOREIGN KEY car TEXT REFERENCES manufacturer.cars(car) ON DELETE CASCADE
ON UPDATE CASCADE;
) TYPE INNODB;

This way I hope to be able to have the retailer database up-to-date.
Is it possible to reference another database in this manor? How do I do it in that case?

Calling Mysql Db From Php
for some reason any php script I write will not call any mysql databases. I have loaded ,mysql 5.0.27 onto my computer as well as apache 2.0.52 and php 5.0.2 but I don't really know if I need to change some code which allows php to call the mysql db's

I'm using a windows machine if that makes any difference.

Calling One Value From Table
I got a table that has only 1 field 1 row of an INT value:

$query = "SELECT * FROM site_counter
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo "$row[0] People Visit My Site";

is there any shorter way to call up that value ?

Calling Php From Stored Procedure!
I was wondering if we can call a php program in stroed procedure in mysql so that I can produce reports for the tables.If possible please write the procedure to call the php from stored procedure.

Problem With Calling Procedures With Php
I am having problem in calling stored procedure using php, I can call them on command line successfully but not able to call them with php code,

Could Multiple Calling Of Mysql_fetch_array Possible?
just wanna ask if, it is possible to call the mysql_fetch_array twice or thrice in one query..

example..

$result=mysql_query("select * from event_history as e,company as c,jobs as j where e.event_id='11' and e.compid=c.cid and e.jobid=j.job_id");
$row=mysql_fetch_array($result);

echo '$row[companyname]';

<select name="jobs">
while($row=mysql_fetch_array($result))
{
<option value="'.$row[job_id].'"> '.$row[jobtitle].'</option>

}
</select>

Calling A Stored Procedure In VB.Net
I am a novice user of the SQL serversbut i have managed to create a Proc, however how do i go about calling it VB.net.

Calling Mysql C API In C++ Project
I am using mysql C API in my C++ project. I am using
mysql-4.1.4-gamma and gcc 3.2.2.

Problem with this code is that when add_data(i) is called second time
SEGMENTATION FAULT occurs at mysql_real_query(). First time it
executes the add_data() function successfully. Code:

Calling Image Into Table
I have 3 cells in each table row.
the cells are image1, image2, image3.

the data to go in cell image1 is: image1.gif, image2 is: image2.gif, image3 is: image3.gif

so can some tell me if it is possible to show these images in a webpage depending wether the image cell has the reference to the image in it.

if the image cell has no image reference in it then dont display it.

Parameters In SP When Calling A Report
Set up the connection to the DB in Crystal using MyODBC. I used the Database expert to call my Stored Procedure which has one Parameter and the code is real simple going like this:

call SuccessLetter (AppNo)

From the wizard i can see and place all the fields from the SP onto the report as long as i hard code an application number into my parameter. But as soon as i change it to a variable (as above) i get the following error from Crystal: Unknown Fields AppNo in order clause MySQL ODBC 3.51 error 42S21

On the VB code side i used
Success.SetParameterValue("AppNo", GeneralFunctions.ApplicationNo)
to set up the parameter and this does get passed thru so the error is comming from the database expert - almost as if it dont recognise that my stored procedure has a parameter. Which it does and it does work if i use the SP outside of Crystal.

I know the SP is working fine cause i have written another SP and then called SuccessLetter with the new SP using a parameter and the results are perfecr.

Calling A Variable From A Different Php File
I have defined a variable named $user in one php file (configure.php)

Code snippet:

public $user;


I want to call that $user variable in a different php file (initiate.php). I have made sure to require configure.php in initiate.php.

When I tell $user to echo in initiate.php, it shows up.

However, I want to create a table within a database with the name $user

mysql_select_db($db_name, $con);
$sql = "CREATE TABLE $user
(
WhatWant varchar(100),
Descript varchar(100),
Link varchar(100)
)";

mysql_query($sql,$con);

For some reason, it won't create the table.

It will create the table if I use something other than $user (either a variable defined in initiate.php, or plain text)

How do I get it to create the table with the name $user, if $user is located in a different php file?

Calling A Stored Procedure
From what I've read, you can't call a stored procedure directly from a select statement...is that correct?
I have this query I'm trying to run:

select * from projects where proj_desc like
'%".$searchterm."%' and proj_type = '".$proj_type."'
and GetDistance($lat1, $lat2, proj_lat, proj_lon) < 50 order by ".$order
This query is looking through a database of projects, and running a stored procedure(GetDistance) to find which ones are within a certain distance away (hence the lat and lon being passed to the sproc). The query is doing this while also processing the user's search term they've put in.

Calling Stored Procedute From A Query - Possible?
Is there any way of looping through a list of dates obtained from a table, e.g. "select mydate from mytable" and then for each of the dates call a stored procedure, e.g. CALL MySP( theCurrentDate ) ?
Or could I do it using a stored procedure - this would seem the obvious solution but I'm not sure if it's possible to loop through a resultset in stored procedures?
Any pointers would be greatfully received





Calling MySQL In Unix In One Line
I'm running MySQL in Unix and I need to run mysql without ever actually going into mysql. Basically I need to open the database, do what i need to do to the database and exit all in one line of code.

If I have to, I could save the sql in a file and go
mysql -h localhost -u ______ -p _________ << mysqlcode.txt
or something with Unix redirection/piping.

However, I'd like to do something more like
mysql -h localhost -u _________ -p ________ - (use tempdatabase; select * from test;)

Is there anyway to do this. The -e paramater looks like it might do that, but I can't figure out how to use it.

Calling Selected Data In A String
I'm doing a quick search in a table and I'm having a problem specifying my data to the table's column's string.

for example:
SELECT * FROM tableA WHERE columnA LIKE '%4%'

Problem is the columnA carries string such as 1,3,14,20, and as you can see, this row is selected as LIKE '%4%' even it doesn't have 4 in its string.

Is there a better way to do this?

Calling Mysql Fields Into A .php Webpage
Situation:
I have created a .php document that displays a youtube video and it works fine.

What I want:
I want this .php document to display a users youtube video, so i need to get the field from the database and make it so the .php document can call it and use the link from it to display the video.

The server is called: 'localhost'
The database is called 'db_vbulletin'
The table is called 'userfield'
The field is called 'field39'

But saying this, the youtube video link will be different between users, so im not sure what kind of factor that would have.

Display All Fields By Calling One Field
I created a testing db with three fields: id, title, text, where id is auto increment. I have this on my .php page:

$dbh=mysql_connect ("localhost", "username", "password");
mysql_select_db ("testing_db");
$result = mysql_query("SELECT id FROM demoTable");
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["id"] . "</P>");
}

this only displays all the id of each row of data. the way i want it to work is to call the id number (eg. page.php?id=1) and then display both the title and text defined for that id. i reckon this is a php syntax problem...would i hv more luck posting in the mysql forum?

Calling A Script Save In Emacs
I'm a Linux user and I written a script in Emacs Text Editor. I was wondering if there was a way that I can have MySql read my script by the file name instead of copying and pasting the script in Mysql.

IF Function,GROUP BY,aggregate Function Problems
Yep, I have all those problems in the title. So I'll explain each one at a time - I did have another thread relating to this very same query but I thought it was time to update where I am with the query because at the moment I feel like I am getting nowhere!

The query I have basically searches through an items_ordered table through each product and checks to see whether the item is VATable or not. This is not where I have the problem though. Where I am really having the first problem is when I am trying to use the IF function to check if the TOTAL of an order is over £300. IF it is then I multiply it my 0.95 (i.e. 5% off). With the query below I get no errors but neither do I get the desired result. It's as though it couldn't even see it. =....

Calling A Php Funtion Inside A Mysql Query
hi there, im trying to call a php function that i have created inside a query and im getting an error.when i remove the funtion statment the query is successful. i was wondering is it possible to call a php function inside a query, if so how do i go about doing so.

this is my code:

MySQL5 Trigger Calling Procedure Syntax
I have an UPDATE trigger on my inventory_transactions table, set to call the spUpdateInventoryCounts procedure for the iInventoryID (FK) record being modified.  Here's the code for my update trigger:

DELIMITER $$

DROP TRIGGER /*!50032 IF EXISTS */ sp_IT_Update$$

create trigger sp_IT_Update AFTER UPDATE on inventory_transactions
for each row BEGIN
    DECLARE tiKey INT;
    SET tiKey = inventory_transactions.iinventoryid;
    CALL spUpdateInventoryCounts(tiKey);
END;
$$

DELIMITER ;

...yet when I modify a row in the Inventory_transactions table, I get an error saying "Error No. 1109
Unknown table 'inventory_transactions' in field list"

...but it's the freakin' table itself!  The error would appear to be in this code, and not the spUpdateInventoryCounts because I dumbed that one down to simply say "update inventory set iqtyinstock=5" just to remove the possibility of that being the culprit.

Calling MySql Select Procedures In Perl
I am trying to call a "select" stored procedure from perl. I am not quite sure how to do it. Here is the stored procedure:

CREATE PROCEDURE spTest() begin select x AS X from tblTest;

It works fine when called from within MySQL.

mysql> call spTest():
+------+
| X |
+------+
| 24 |
| 3 |
+------+

I have the following code in perl:

my $spt = $db->prepare("Call spTest()");
$spt->execute();
$spt->bind_columns($id);
while($spt->fetchrow_array()){
print "X = $id";
}

I get an error that says I need to use "select into". How do I call this procedure from perl?

Calling MySQL Experts! Using LIMIT Within IN Subquery Question
I have 2 tables that I need to pull data from:

1. members (which contains all my members, of course)
2. blogs (which contains blog posts made by members)

The primary key of members is userid, which is a foreign key in blogs.

So.. what I'm trying to do is pull out the latest 30 entries and NOT show duplicate posts from the same member. The closest working query of this I wrote is this:

Now() Function
I am using now function to get current date when info is added to my table. I am using a hosting provider that is 6 hrs ahead in time zone so i always have to subtract 6hrs to get the correct time. Is there anyway I can format the time to be inserted to my time zone?

Self Run Function/..
I need to delete some records from my table every 5 minutes. I thought that would be better if done via a trigger, function, etc. But I don't know how to fire a trigger every 5minutes or is there any other way to do that?

Using The AVG Function
I am trying to use this query:

SELECT average(price) FROM `data` WHERE id IN ( '3', '3', '6' )

to average the price of items users select. The problem is that when a user selects a value more than once it only includes it in the average once. How can I make it average all the values?

Looking For A Particular Function
I have a Column which consists of only DATE's and an ID code. I want to enumerate all the DATE's of a certain ID to get a grand total of a ID. Does such a function exist?

Avg Function
Why doesn't this work:

SELECT avg(value) as avg_value from playerstats where avg_value = '100' and stattype = 'r' group by avg_value

I'm trying to select the avg_value of 'r' where the avg_value = '100'.

Last Function
How do you get the last row in a grouped query. I have a table with running balances. At the end of the day, I want to find out the last balance.

select *
from accounts
group by date(dateCol)

This always gives me the first row of the grouped column dateCol.

NOW() Function
We are developing a tool here that uses a php script to insert data into a MySQL 5.0.41 database running with the INNODB engine. While tracking a bug we noticed a strange effect.

Each insert query contains a call to the NOW() function to store the create data of the record into a field automatically. Therefore we can expect in my opinion that the timestamps of the records are increasing simultaniously with the autoincrement key of the table. This actually is not the case. On the contrary the timestamps are jumping up and down with large differences. For example we have the record with ID1 that got the timestamp 14 minutes 2 seconds where ID2 got 15 minutes 15 seconds and ID3 14 minutes 9 seconds. How could this be? Does it mean that the server does not execute the statements in the order he receives them? Is it possible that it took more than a minute to execute a simple insert statement?

Max Function
I have a table with name, priority and parent as three columns...I want to get the max(priority) grouped by the parent... I am trying to get name, priority, parent, max(parent) as the four columns in the result...Could some one help me with this

select name, priority, parent from table1
select max(priority) from table1 group by parent

are the two select statements that I wish to combine...is it possible..?

Function In C
I was trying to figure out how would you write a function in MySQL using C. I saw that theres a CREATE FUNCTION syntax but am confused on how to compile and write a C function.
For example if I wanted to write a function that added an extra period after every period and called it ADD_PERIOD() how would I do that.

Example:

x = "joe runs far."
SQLSTRING = "SELECT * from Story where Sentence = '"+ADD_PERIOD(x)+"'";

this would return. joe runs far

SQL IN() Function
so i have a query such as
SELECT first_name FROM users WHERE user_ID IN (5,4,8,19,8,4) and i want the first_name to be retrieved for EACH of the cases (eg. name is repeated twice for 4 and 8)

2 Queries In 1 Function?
Is it possible to query the database twice in the same function?

If so is it possible to use results form the first query IN the second?

Count () Function
Cant anyone tell me why this very basic query won't process.
This is to give a breakdown of Mem.heard 1-12 numbers ("where did you hear about us survey") for ea of the MOrig two user types

EX:
MOrig | heardof | count_heardof

UserA 1 5 (times)
UserA 2 6 (times)
UserB 1 3 (times)
UserB 2 8 (times)

SELECT MOrig, heardof, count ( heardof ) as count_heardof
FROM Mem
GROUP BY MOrig, heardof
ORDER BY MOrig

Search-function
Does anyone happen to know how to select info from the database based on multiple strings like so:

SELECT title FROM food WHERE title LIKE '%New%' AND WHERE title LIKE '%York%' AND WHERE title LIKE '%restaurant%'

SUBSTR() Function
SELECT SUBSTR(message, 0, 20) FROM posts

My goal is to start at the first characters on the far left, and then return 20 chars only. This function for some crazy reason seems to start at the end of the string though, which is stupid, because how do I know where the string starts?

Hidden_field Function
Does mysql 4.0.24 standard have a hidden_field function? I need to maintain state from one form to another and hidden_field is used in a script I have using mysql 3.22. If 4.0.24 doesn't have hidden_field function, what can I use with mysql 4.0.24, which is used on my server?

Date Function
I have a time stamp field in my orders table and I want to form a select statement where it returns all orders with an order time of two weeks ago from the current time or later so I can keep better track of unpaid orders.

Here are the fields for the orders table
orderID
order_time
order_status
etc...

SELECT orderID FROM orders WHERE ???? AND order_status=2 ORDER BY order_time ASC

Thanks for any help. The date functions are a little confusing to me from the manual.

Function ROW_INDEX
I couldn't find a function that numbers the resultset of a query. First row should be 1, second row 2 etc.

I searched for rownum, rowcount, rowindex etc with and without underscore's.

Password() Function
I've created a simple table called users. This table contains four columns. First_Name, Last_Name, User_Name, Password. I've used the password() function when inserting a user. I have read the documentation on using other types of encryption, but it's killing me that I can't get this to work. Robert has a User_Name of "Bob" and a password of password('tomato').

===============================
HOW I ADDED BOB

insert into users values("Robert","Evans","bob",password('tomato'));

1 row added yada yada

HOW I QUERY BOB

select * from users where User_Name="bob" and password=password('tomato');

Empty Set

===============================

That exact same query is in a tutorial for a simple PHP login form, but an empty set is returned even in the command prompt using MySQL by itself, therefore the script cannot move on.

Mysql_query Function
I making a loop of the returned result set from mysql_query, it is very important to know if an update occur during the loop to the table affect the result set of the loop or not ?

I need to loop blindly without see any updates or inserts occur during the loop....

Counting Function
I have a table with a list of names and some are duplicate and I would like to count them?? It's like this:

Drew
Simon
Alli
Tim
Beth
Alli
Simon
Drew
Simon
Simon
Alli

Then to export:
Drew - 2
Simon - 4
Alli - 3
Tim - 1
Beth - 1

How could I go about asking this query?

SELECT COUNT(name) FROM table

??

Random Function?
I would like to know if there is an existing random function I can use to generate a random number so I can concatenate it to a string?

AES_ENCRYPT Function
Essentially I am playing around with the aes_encrypt fucntion to securly store some data in the db. The problem I am having is that I get the following error : Data truncated for column 'foo' at row 1 and the table creating was done with:

CREATE TABLE `foobar` (
`foo` varchar(10000) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

with a varchar(10000) and doing and AES_ENCRYPT('foo','bar') one would think that it would work. But I suspect that I am doing something incorrectly.

Using Unix_timestamp Function
Can anyone see anything wrong with the way I am using the MySQL unix_timestamp function in this snippet from a query?

$importdata="INSERT INTO import
(date, gluc, humilog, regular, lantus, carbs)
VALUES (unix_timestamp('2004-08-01 05:04:00'),152,02.0,00.0,00.0,00.0)

The table column where I am inserting this was created as:

"date timestamp not null"

I am getting an incorrect timestamp value (20001228122240)that converts to "Mon, 18 Jan 2038 20:14:07 -0700" in PHP. As far as I can see, my usage of unix_timestamp is the same as that in the MySQL manual.

RAND Function
Basically, I have a stored function that is designed to deliver a random 3 digit code. But I need it to be of the format #~#~# (ie, 3~0~7, 5~1~5, etc) So I set up the function to generate 3 random digits and then concatenate them together, using the following lines inside the function:

SET v1 = FLOOR(RAND() * 10);
SET v2 = FLOOR(RAND() * 10);
SET v3 = FLOOR(RAND() * 10);
SET vCode = CONCAT_WS('~', v1, v2, v3);

The function works fine, and gives me back my 3 digit code in the exact format that I want.

However, after testing it out, I've discovered that it isn't entirely random. There are certain combinations that I never end up getting returned from the function. There are basically 1000 different possible combinations, but I'm only ever receiving 800 combinations back. I've loop tested the function to generate literally millions and millions of different codes, and there are certain codes that I just never see. Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved