RE: [PHP] a little logic...
| From: | Bruce Douglas | Date: | Sun, 18 May 2003 18:22:34 +0000 |
| Subject: | RE: [PHP] a little logic... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148110@lists.php.net to get a copy of this message | ||
The following sampl of code should help with what you're trying to do
regarding the mysql arrays....
To add anything.. you will have to alter the table schema..and then modify
the associated select/insert/update queries...
<?php
//Auction - Items page
$menuvar = 2;
require('../mysql/dbconfig.inc');
require('../mysql/database.php');
require('../includes/items.inc.php');
//initialize the database
$result = db_connect($HOST, $USER, $PASSWORD);
$result = db_select($DBNAME, $result);
?>
<?php
$query = "select * from foo";
$result = db_query($query);
$object = db_fetch_array($result);
$ii=count($object);
for ($i=1;$i<$ii+1;$i++)
{
$auction = $object[$i]["auctionid"];
$descr = $object[$i]["description"];
}
etc......
?>
Now... you need to remember that the retuned array starts with an ndx of 1.
The object returned from the db_fetch_array is simply an array of the
returned rows from the query...
I created a couple of simple functions that i use to encpasulate the mysql
functions...
//connects to the mysql database engine
//
function db_connect($dbhost,$dbuser,$dbpasswd) {
global $conn;
$conn = mysql_connect($dbhost,$dbuser,$dbpasswd)
or die("Could not connect: " . mysql_error());
return $conn;
}
//select the specific database
//
function db_select($dbname, $conn) {
$result = mysql_select_db($dbname, $conn)
or die(mysql_error());
return $result;
}
//query the specific database
//
function db_query($query) {
$result = mysql_query($query)
or die("Query error is: ".mysql_error());
return $result;
}
//return the number of rows for a db_query
//this is used directly after a db_query call
//
function db_numrows($result) {
$numrows = 0;
$numrows = mysql_num_rows($result);
// or die("Could not determine any rows: " . mysql_error());
return $numrows;
}
function db_free_result($qhandle) {
return @mysql_free_result($qhandle);
}
function db_result($qhandle,$row,$field) {
return @mysql_result($qhandle,$row,$field);
}
function db_numfields($lhandle) {
return @mysql_numfields($lhandle);
}
function db_fieldname($lhandle,$fnumber) {
return @mysql_fieldname($lhandle,$fnumber);
}
function db_affected_rows($qhandle) {
return @mysql_affected_rows();
}
function db_fetch_single_array($qhandle) {
$res = 0;
$res = mysql_fetch_array($qhandle);
if ($res){
return $res;
}else{
return false;
}
}
function db_fetch_array($qhandle) {
$num_fields = mysql_num_fields($qhandle);
$j=0;
$x=1;
while($row=mysql_fetch_array($qhandle))
{
for($j=0;$j<$num_fields;$j++){
$name = mysql_field_name($qhandle, $j);
$object[$x][$name]=$row[$name];
}
$x++;
}
return $object;
}
Have fun....
bruce
-----Original Message-----
From: Jason Wong [mailto:php-general@gremlins.biz]
Sent: Sunday, May 18, 2003 11:04 AM
To: php-general@lists.php.net
Subject: Re: [PHP] a little logic...
On Monday 19 May 2003 01:43, Ryan A wrote:
> Hi,
> Having a little logic problem here...This is my (original)code:
> --------------------------------------------------------------------------
-
>- ---
> $q = "SELECT cno FROM main_co where user='".$user."' and
pass='".$pass."'";
> $r = mysql_result(mysql_query($q),0) or die("Error: " . mysql_error());
>
> $_SESSION['cno'] = $r;
> --------------------------------------------------------------------------
-
>- ---
>
> what logic should I use to get/add $company and $site?
> (This is what i want to make of it)
> eg:
> --------------------------------------------------------------------------
-
>- ---
> $q = "SELECT cno,company,site FROM main_co where user='".$user."' and
> pass='".$pass."'";
> $r = mysql_result(mysql_query($q),0) or die("Error: " . mysql_error());
>
> $_SESSION['cno'] =$r //???
> $_SESSION['company'] = $r // ???
> $_SESSION['site'] = $r; // ???
> --------------------------------------------------------------------------
-
>- ---
>
> After reading up in the manual I think $r becomes an array/ or I should
> make it an array or somehow use an array...right? I HATE arrays so dont
> really know how to use them or even if I need one here...the only arrays I
> use are the ones that return a resultset after querying the database.
You're using the wrong function. You should be using one of these:
mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object(),
mysql_fetch_row().
In this particular you probably want to use mysql_fetch_assoc(), look it up
in
the manual and follow the example.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
Can you MAIL a BEAN CAKE?
*/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php