I'm almost done, this is what I'm doing
my table has 3 fields indexed separately
date, unit, code
===========================
<?php
$flag = toread();
function toread
$sql = "SELECT * FROM table .
WHERE date = " . "'" $var1 . "'" .
" AND unit = " . "'" . $var2 . "'" .
" AND code = " . "'" . $var3 . "'";
$result = mysql_query($sql);
return mysql_num_rows($result);
//retrieve data into variables...
?>
<html>
...
<script javascript>
var var1 = '<?php print "$flag"; ?>'
if (var1 == 0)
alert(msg)
else
getdata()
...
=================================
well the problem is that the same
SELECT sentence, works fine in a
small htmtest, but in the above example
I always receive var1 = 0...
Even when issue an INSERT,
(and the record is generated)
the flag is always 0...
I use javascript because
I need to keep the data in memory
for other tasks, visualization in html
is less important in this case
what do you suggest?
almost done, but...
Moderator: General Moderators
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Just fixing up some syntax ...
Code: Select all
<?php
$flag = toread();
function toread() {
$sql = "SELECT * FROM table
WHERE date = '".$var1."'
AND unit = '".$var2."'
AND code = '".$var3."'";
$result = mysql_query($sql);
return mysql_num_rows($result);
}
//retrieve data into variables...
?>i just made double sure, but i think the problem is how you are calling your function and then you are defining it ( and not doing that correctly ). maybe that's why..
anyways, try this :
hope this helps.
anyways, try this :
Code: Select all
<?php
function toread()
{
$sql = "SELECT * FROM table WHERE
date = '" .$var1. "'
AND unit = '" .$var2. "'
AND code = '" .$var3. "'";
$result = mysql_query($sql);
return mysql_num_rows($result);
}
$flag = toread();
?>Nope the problem is the results pointer is only local to that function and also you would not appear to be passing the vars to the funtion...
solution:
The other alternative would be to declare $result, $var1, $var2 and $var3 as globals:
But that is bad 
solution:
Code: Select all
<?php
function toread($var1, $var2, $var3)
{
$sql = "SELECT * FROM table WHERE
date = '" .$var1. "'
AND unit = '" .$var2. "'
AND code = '" .$var3. "'";
$result = mysql_query($sql);
return $result;
}
$flag = toread();
$numofrows = mysql_num_rows($flag);
?>Code: Select all
<?php
function toread() {
globals $result, $var1, $var2, $var3;actually, i think $var1, $var2, and $var3 have already been defined before the construction of the function ( thus why she can query the database with $varx and actually get a result... ). Unless, of course, this function is in a totally different script, in which she is gonna be entering the variables through the function to query, then the code you gave would be correct.
but if she is naming this function in the same script that already contains $varx, the $varx portion is ok. not really sure about the line you gave : $numofrows = mysql_num_rows($flag);
don't see how this would return anything different from the example i gave since she is returning the number of rows already..
but if she is naming this function in the same script that already contains $varx, the $varx portion is ok. not really sure about the line you gave : $numofrows = mysql_num_rows($flag);
don't see how this would return anything different from the example i gave since she is returning the number of rows already..
basics
thanks guys! I do appreciate your help
do you know where to find basic practical examples?
I just need to write/read one record at a time,
using the same multipart index, can't be easier,
I think I'm not getting the mysql syntax,
since manual is complex and is based on multiple selection criteria,
and not to identify/recovery one particular record
any links?
do you know where to find basic practical examples?
I just need to write/read one record at a time,
using the same multipart index, can't be easier,
I think I'm not getting the mysql syntax,
since manual is complex and is based on multiple selection criteria,
and not to identify/recovery one particular record
any links?
believe it or not, you have one of the easiest practical examples manual already on your pc for mysql ( if you have mysql installed on your computer ). You can find it within the MySQL/Doc directory. It's actually manual.html.
if you skip down to around section 3.1, it will begin by showing you how to create databases first thing, and then how to select only one record, or multiple records, and so on.
it covers the most basic of queries you can call to mysql, leaving the advanced available to you on the online manual when you need it.
also, i might go ahead and buy a book on it if you are serious about it. PHP and MySQL Web Development by Luke Welling and Laura Thomson is highly recommended...
hope this helps.
if you skip down to around section 3.1, it will begin by showing you how to create databases first thing, and then how to select only one record, or multiple records, and so on.
it covers the most basic of queries you can call to mysql, leaving the advanced available to you on the online manual when you need it.
also, i might go ahead and buy a book on it if you are serious about it. PHP and MySQL Web Development by Luke Welling and Laura Thomson is highly recommended...
hope this helps.