almost done, but...

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
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

almost done, but...

Post by lisawebs »

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?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Just fixing up some syntax ...

Code: Select all

<?php 
$flag = toread(); 
function toread() &#123;
     $sql = "SELECT * FROM table 
                WHERE date = '".$var1."'
                AND unit = '".$var2."' 
                AND code = '".$var3."'"; 
     $result = mysql_query($sql); 
     return mysql_num_rows($result);
&#125;
//retrieve data into variables... 
?>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

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 :

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(); 
?>
hope this helps.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

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:

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);
?>
The other alternative would be to declare $result, $var1, $var2 and $var3 as globals:

Code: Select all

<?php 

function toread() { 
    globals $result, $var1, $var2, $var3;
But that is bad :P
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

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..
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

basics

Post by lisawebs »

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?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

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.
Post Reply