Jump to content

Recommended Posts

<?php
function checking_out() {

$conn = db_connect();
$nickname=$_SESSION['valid_user'];
$query="select sum(price) from preorders where name='".$nickname."'";
$result = $conn->query($query);

if ($result) {
	echo '<h1>'.$result.'</h1>';
}
}
?>

 

This is not working, there is no result in the browser, any idea ?  :confused:

Link to comment
https://forums.phpfreaks.com/topic/252481-unable-to-echo-a-sum/
Share on other sites

Did you get an error message and if yes what was it?

 

First steps in trouble shooting code : Check the inputs!

echo out $nickname

 

Did you make a connection to your db? I ask because it's not shown that you did here and you don't tell us if the connection is a global variable, which you would need here.

<?php
function checking_out() {

$conn = db_connect(); // function works fine.
$nickname=$_SESSION['valid_user'];
$query="select sum(price) as price from preorders where name='".$nickname."'";
$result = $conn->query($query);

if ($result) {
	echo '<h1>';
	echo $result['price'];
	echo '</h1>';
} else {
	echo '<h1>No existe conexion</h1>';
}
}
?>

 

No, it's not working. The php stops rendering the HTML page until this part, the table "preorders" is ok with a column "price". I am trying to use a database (table: preorders) to storage all the products that some registered customer wants to save until he decided to pay for them, so the idea of this code is for a header, a place to show the customer always de total value of all the products saved and the quantity (I don't code it yet so any idea of how I can integrate the "count()" in to this single query would be of great help too.

 

Thanks

 

Renzo :'(

 

The ->query() method of your database class probably (depending on what database class you are using) should either return a FALSE value (which won't echo as anything) when your query FAILS due to an error, or it will return a result resource/result object, which you then need to fetch the data from.

 

So, what is your database class you are using and you need to have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will report and display all the errors it detects.

Hi, thanks for responding. How I can program the server to send me the type of error that I need to know? This is a public server, Yahoo Small Business Hosting services. However, I can tell you what the database information says:

 

Server version: 4.1.14

Protocol version: 10

MySQL via TCP/IP

UTF-8 UNICODE

 

back log - 50

basedir /

binlog cache size 32,768

bulk insert buffer size 8,388,608

character set client utf8

(Valor global) latin1

character set connection utf8

(Valor global) latin1

character set database latin1

character set results utf8

(Valor global) latin1

character set server latin1

character set system utf8

character sets dir /share/mysql/charsets/

collation connection utf8_unicode_ci

(Valor global) latin1_swedish_ci

collation database latin1_swedish_ci

collation server latin1_swedish_ci

concurrent insert ON

connect timeout 5

datadir /var/lib/mysql/data/

date format %Y-%m-%d

datetime format %Y-%m-%d %H:%i:%s

default week format 0

delay key write ON

delayed insert limit 100

delayed insert timeout 300

delayed queue size 1,000

expire logs days 0

flush OFF

flush time 0

ft boolean syntax + -><()~*:""&|

ft max word len 84

ft min word len 4

ft query expansion limit 20

ft stopword file (built-in)

group concat max len 1,024

have archive NO

have bdb NO

have blackhole engine NO

have compress YES

have crypt YES

have csv NO

have example engine NO

have geometry YES

have innodb DISABLED

have isam NO

have ndbcluster NO

have openssl NO

have query cache YES

have raid NO

have rtree keys YES

have symlink YES

init connect

init file

init slave

innodb additional mem pool size 1,048,576

innodb autoextend increment 8

innodb buffer pool awe mem mb 0

innodb buffer pool size 8,388,608

innodb data file path

innodb data home dir

innodb fast shutdown ON

innodb file io threads 4

innodb file per table OFF

innodb flush log at trx commit 1

innodb flush method

innodb force recovery 0

innodb lock wait timeout 50

innodb locks unsafe for binlog OFF

innodb log arch dir

innodb log archive OFF

innodb log buffer size 1,048,576

innodb log file size 5,242,880

innodb log files in group 2

innodb log group home dir

innodb max dirty pages pct 90

innodb max purge lag 0

innodb mirrored log groups 1

innodb open files 300

innodb table locks ON

innodb thread concurrency 8

interactive timeout 28,800

join buffer size 131,072

key buffer size 8,388,600

key cache age threshold 300

key cache block size 1,024

key cache division limit 100

language /share/mysql/english/

large files support ON

license GPL

local infile ON

locked in memory OFF

log OFF

log bin OFF

log error /tmp/mysql4.log

log slave updates OFF

log slow queries OFF

log update OFF

log warnings 1

long query time 10

low priority updates OFF

lower case file system OFF

lower case table names 0

max allowed packet 1,048,576

max binlog cache size 18446744073709551615

max binlog size 1,073,741,824

max connect errors 10

max connections 100

max delayed threads 20

max error count 64

max heap table size 16,777,216

max insert delayed threads 20

max join size 18446744073709551615

max length for sort data 1,024

max relay log size 0

max seeks for key 18446744073709551615

max sort length 1,024

max tmp tables 32

max user connections 0

max write lock count 18446744073709551615

myisam data pointer size 4

myisam max extra sort file size 2,147,483,648

myisam max sort file size 9223372036854775807

myisam recover options OFF

myisam repair threads 1

myisam sort buffer size 8,388,608

net buffer length 16,384

net read timeout 30

net retry count 10

net write timeout 60

new OFF

old passwords ON

open files limit 65,535

pid file /tmp/mysql4.pid

port 0

preload buffer size 32,768

protocol version 10

query alloc block size 8,192

query cache limit 1,048,576

query cache min res unit 4,096

query cache size 10,485,760

query cache type ON

query cache wlock invalidate ON

query prealloc size 8,192

range alloc block size 2,048

read buffer size 131,072

read only OFF

read rnd buffer size 262,144

relay log purge ON

relay log space limit 0

rpl recovery rank 0

secure auth OFF

server id 0

skip external locking ON

skip networking ON

skip show database OFF

slave net timeout 3,600

slave transaction retries 0

slow launch time 2

socket /tmp/mysql.12874.sock

sort buffer size 2,097,144

sql mode

storage engine MyISAM

sql notes ON

sql warnings ON

sync binlog 0

sync replication 0

sync replication slave id 0

sync replication timeout 0

sync frm ON

system time zone UTC

table cache 64

table type MyISAM

thread cache size 8

thread stack 196,608

time format %H:%i:%s

time zone SYSTEM

tmp table size 33,554,432

tmpdir /tmp

transaction alloc block size 8,192

transaction prealloc size 4,096

tx isolation REPEATABLE-READ

version 4.1.14

version comment Source distribution

version compile machine x86_64

version compile os unknown-linux-gnu

wait timeout 28,800

Your database class is the OOP class you are making an instance of in your db_connect() function code, nothing to do with the database server information you posted.

 

A) You should be learning php, developing php code, and debugging php code on a local development system, not on a live server.

 

B) You can set php's error_reporting/display_errors settings in your script (which won't show fatal parse errors in your main file, which is why the settings should be in the master php.ini.) You can always add the following two lines of code immediately after your first opening <?php tag for debugging purposes (remember to remove them once you are finished debugging your code so that you won't expose server/account information to a hacker that intentionally triggers errors on your live site) -

 

ini_set("display_errors", "1");
error_reporting(-1);

 

 

 

 

A. I have no other option.

B. Ok, I will use the code you posted from now to always for debugging purposes after my first opening php tag. (Thanks for that).

 

The $nickname=$_SESSION['valid_user'] exist because is part of the session_start from the page. The existance of THIS $nickname was tested. Moreover the $nickname is stored in the database in other function. What I need to obtain the total sum of the values inside the "price" column of the "preorders" table from db.

 

This is the error report:

 

Fatal error - Cannot use object of type mysqli_result as array in /session_test.php on line 34

 

Actual code:

 

<?php
ini_set("display_errors", "1");
error_reporting(-1);

function checking_out() {

$conn = db_connect(); // function works fine.
$nickname=$_SESSION['valid_user'];
$query="select sum(price) as price from preorders where name='".$nickname."'";
$result = $conn->query($query);

if ($result) {
	echo '<h1>';
	echo $result['price'];
	echo '</h1>';
} else {
	echo '<h1>No existe conexion</h1>';
}
}
?>

A. I have no other option.

 

There are always options. You can install a free all in one Apache/Php/Mysql package on just about any modern computer and develop your code locally before putting it onto a live server.

 

As to the error message. That at least identifies the database class as mysqli.

 

$result is a mysqli result object. See the following link for the methods and properties that you can use with a mysqli result object - http://us3.php.net/manual/en/class.mysqli-result.php fetch_assoc would be a good choice to use to fetch the price alias value.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.