Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to output the sum of turples from the a table. The SQL statement is ok and gives me results. My problem is printing the answer on a form. Below is my code:
<?php
$r=mysql_query("select sum(total_amount) from lbc_production where link_id='C741_Link01' and execution_date BETWEEN '2014-12-01' AND '2014-12-31'");
$rows=mysql_fetch_assoc($r);
echo $r;
echo $rows['(total_amount)'];
?>
It outputs Resource id #9 on a form but I want a figure.
You have two issues:
$r contains the resource ID for your MySQL connection. You echo it out. That's why you see that. Stop doing that and it goes away.
You are not using the right array key to access your sum value you seek so nothing is output. If you had error reporting turned out PHP would have told you this.
Here's improved code to resolve these issues:
<?php
$r=mysql_query("select sum(total_amount) as total from lbc_production where link_id='C741_Link01' and execution_date BETWEEN '2014-12-01' AND '2014-12-31'");
$rows=mysql_fetch_assoc($r);
echo $rows['total'];
?>
I removed the line where you echo out the MySQL resource and added an alias to sum(total_amount) which makes it easier to access via PHP. The alias is called total which I use as the key to access that value from the $row array.
FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$insert_query = "Asset Addition (server_name, phy_ip, nat_ip, server_model) values ('$server_name','$phy_ip','$nat_ip','$server_model')";
if(mysqli_query($con, $insert_query))
Do you think there is an issue with the syntax here?
"Do you think there is an issue with the syntax here?"
Yes, as I said in comments; syntax is English-based.
You need to use: INSERT INTO your_table and replacing with your table's actual name.
Your code also is open to an sql injection, use a prepared statement.
https://en.wikipedia.org/wiki/Prepared_statement
and mysqli_error($con) on the query.
Edit: Now seeing more comments, make sure you did successfully connect to your database and using the same MySQL API, and that all variables contain value and running off a webserver with PHP/MySQL installed and running.
Reference(s):
http://php.net/manual/en/function.mysqli-connect.php
http://php.net/manual/en/function.error-reporting.php
Example:
if(mysqli_query($con, $insert_query)) {
echo "Success";
} else {
echo "Error: " . mysqli_error($con);
}
Should this also fail, then you may also be inserting characters that MySQL is complaining about, such as an apostrophe and is seeing it as an sql injection.
Therefore you will need to escape that data, to which you should be doing in any case.
Again; use a prepared statement.
You need to make sure that the column types/lengths can also accomodate the incoming data. That could fail silently.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ex: If I have code :
$data = array('a','b','c');
foreach($data as $val){
mysql_query("INSERT INTO db (`title`)VALUES('$val')");
}
I want to insert all data from variable $val how can I coding it ?
please help !!!
thank !!
There are two things wrong which i can see at a glance.
mysql is deprecated, use mysqli instead. Reference
Since you will have to switch over to mysqli you will need to reference your database connection every time you want to do a mysql database related operation, unless you do some PHP magic (classes or methods).
Other than these two i guess there is no problem in your code, the for loop should work perfectly fine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new here, so please bear with me.
Am trying to run a project built using the codeigniter.
The project is an online registration system,but am getting the undefined variable error in some parts of my page.
Please if you got the time to go over the project and help me out I will be very grateful.
Here is the link to the whole system.
NB:It contains the exported database already( the database is called 'orsdb' )
Cheers!!!
https://drive.google.com/open?id=0ByYO-aax6KH3cnJNRnBsNEgwd1k
You should check every variable in your CI view part before printing it, you can use isset() to check you variable is either define or not. Example -
<?php echo isset($var) ? $var:""; ?>
You can also use empty() function to check variables.
Another thing is if your variable is always undefined then you must check your array which contains data fetching from database in CI controller and print the array to check your all required value does exist or not. If not exists then check your SQL query whether it fetches your required data or not.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am a beginner at PHP and MYSQL. Here is my simple code to add data to a data base. it is not working
the connection.php(sets up the mysql connection variables) files have already been created and are working fine with other files and functions. Am receiving no errors while this code here does not add the data to the database
could someone please tell me where the problem could be?
<?php
if (isset($_POST['bookt']) & isset($_POST['type']) & isset($_POST['publisher']) & isset($_POST['year']) & isset($_POST['class']) & isset($_POST['subject'])) {
//set the values
$bookt= $_POST['bookt'];
$type= $_POST['type'];
$publ=$_POST['publisher'];
$year=$_POST['year'];
$class= $_POST['class'];
$subj= $_POST['subject'];
//INSERTING A ROW
$add_query= "INSERT INTO books ('Book Title','Type','Publisher','Yearp', 'Class','Subject')
VALUES ('$bookt','$type','$publ','&year','$class','$subj')";
//query
$result=mysql_query($add_query);
if (!$result) {die("couldn't perform query".mysql_error());}
if ($result) {echo " </ br> <p><script type='text/javascript'>alert('INSERT SUCCESSFUL!!!');</script></p><br /><br /> insert id was ".mysql_insert_id();}
};
?>
You have a lot of major problems with this code.
First, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. They were removed entirely from PHP 7. Use MySQLi or PDO instead.
Second, the Boolean "and" operator is &&, not & (the bitwise "and" operator).
Third, it's $year, not &year.
Fourth, put column names in backticks, not single quotes ('...'):
$add_query= "INSERT INTO books (`Book Title`,`Type`,`Publisher`,`Yearp`, `Class`,`Subject'`)
VALUES ('$bookt','$type','$publ','$year','$class','$subj')";
Single quotes will cause your query to fail. This is why your query isn't working at all.
Fifth, you aren't doing any error checking or data validation.
Sixth, you are wide open to SQL injection. You need to use prepared statements and never put user input directly into SQL.
There may be even more issues, but these are the big ones.
If you want multiple conditions in your if-statement use a logical operator "&". Also mysql_ has been gone from PHP7 for a long time.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a program I am currently trying to change from MySQL to MySQLi. The following database configuration is in one file:
<?php
define ("DBHOST","localhost");
define ("DBUSER","user");
define ("DBPASS","pass");
define ("DBNAME","name");
/*define ("DBUSER","root");
define ("DBPASS","");
define ("DBNAME","date_demo");*/
define ("DBPREFIX","");
define ("CHAR_SET","utf8");
define ("CACHEDIR","");
?>
The other section is on another page:
$conn = new mysqli(DBHOST,DBUSER,DBPASS) or die(mysqli_connect_errno());
$row = mysqli_fetch_array(mysqli_query("select * from settings where id = '1' "));
How can I get them all onto one page? Any ideas would be much appreciated so I can start converting to MySQLi.
by doing this, you are borderline mixing procedural and OOP php which is highly ill advised. If you want to keep the connection seperate, you can
include 'conn.php';
however i would highly reccomend looking into building a PDO object and running any queries via that. a useful tutorial can be found at https://www.youtube.com/watch?v=c_hNNAdyfQk&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc which introduces the concepts of using global variables and shows how to instatiate a DB object which creates a better structure for your code and abstracts functions from data.