I need to print a mysql (or better mysqli) query that contain math operators.
If I use a query like that
$sql = "SELECT SUM( `Home` + `Away`) AS Tot\n"
. "FROM `teams`\n"
. "WHERE `idteam` = \'Chelsea\'";
when I go to print the %result with mysql_result:
<?php echo mysql_result($result1,0); ?>
or with mysqli function:
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s \n",$row[0]);
it doesn't works.
The same code works if the Query ask to db just a simple information and not math operators like "SELECT SUM(a+b)*3"
Any ideas?
Thank you for support
try
$sql = "SELECT Home + Away AS Tot FROM teams WHERE idteam='Chelsea'";
SUM() is an aggregate function, your just adding 2 numbers
$sql = "SELECT SUM(Home + Away) AS Tot FROM teams WHERE idteam='Chelsea'"; Works for me. I think the problem is in your php try
echo mysql_result[0];
what if you try this?
$sql = "SELECT SUM(Home + Away) AS Tot FROM teams WHERE idteam='Chelsea'";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res))
{
echo $row["Tot"]."<br>";
}
(but this will print only one row)
Related
I'm quite new to working with PHP and databases. So, like all beginners, I'm having problems with the mysqli extension. I was trying to avoid using mysqli_results, as I didn't want to deal with an array and a loop every time I want a simple piece of data. But that might not be possible.
I need to echo $user_count, but nothing seems to be stored there. My code seems to be okay according to the API, but maybe I'm just trying to use the wrong functions altogether. How do I put the result I need into $user_name?
mysqli_query($conn, "SELECT * FROM `wp_users` WHERE 1");
$result_user_count = mysqli_query($conn, "SELECT COUNT(`ID`) FROM `wp_users`");
$user_count = mysqli_fetch_field_direct($result_user_count, 0);
echo $user_count . ' users found on ' . $dbname . ':</br>';
Based on your provided code, you need to change it like below:-
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
echo "Total Users Are:- ". $row["COUNT"];
}
Note:- try to add mysqli error reporting code so that you will what problem actually exist in your query code, and you can easily resolve them. thanks
Try this. comment if not worked.
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users");
if ($u_count = $mysqli->query($query)) {
while ($res = $u_count->fetch_assoc()) {
echo "Total Users: ". $res["COUNT"];
}
}
I have a SQL query in my PHP file that makes use of some variables in it. I want to print the query itself on the localhost to check as to whether the entire query is been executed or not.
My query is like this:
$result = mysql_query("SELECT * FROM sample WHERE col01 LIKE '%$abc%',$db);
I am trying to print the query using echo $result but get Resource id #25 on localhost. I want to print Select * FROM ... as the output. Is there any way?
First of all: You are missing a double quote: $result = mysql_query("SELECT * FROM sample WHERE col01 LIKE '%$abc%'",$db).
That said, what stops you from
$sql="SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
$result = mysql_query($sql,$db);
echo $sql;
If you were using PDO (and you should, the old mysql_ functions are deprecated and insecure) you could just use PDOStatement->queryString property to view the query at a later time.
Store as a variable $sql
Its normal, first you need to fetch that resource obj
And anyway you missing a double quote,
example.
$sql = "SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
$result = mysql_query($sql);
while ($line = mysql_fetch_object($result)) {
echo $line->colname ."\n";
}
echo "\n" . ' query: ' . $sql
And from PHP 5.5.0 and beyond use mysqli
$sql = "SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
if ($result = $mysqliobj->query($sql)) {
while($line= $result->fetch_object()){
echo = $line->colname ."\n";
}
}
echo "\n" . ' query: ' . $sql
or print_r($mysqliobj->info); # store las query
Following the code I wrote. This doesn't return any values, even though the table has the keywords.
<?php
$conn = mysql_connect("localhost", "root", "qwerty");
mysql_select_db("mis", $conn);
$coursename=$_POST['coursename'];
$sql = "SELECT *
FROM course
WHERE coursename='$coursename'".
"ORDER BY coursename";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo $row['coursename'];
};
?>
The problem is case-sensitivity. MySQL identifiers are not case sensitive unless you enclose them in backticks. However, PHP array indexes are.
Therefore if you have a column named CourseName, the following query will work:
SELECT *
FROM course
WHERE cOuRSEnaME = 'foo'
ORDER BY courSEnAmE
But, referencing it in PHP as $row['coursename'], $row['cOURsENamE'] or any other differing combination will not work, as these all refer to different keys. You must use $row['CourseName'].
See also: PHP array, Are array indexes case sensitive?
Try to add error_reporting(E_ALL); immediately after your <?php and see if you get any error messages from your browser.
You should be able to track down the root cause of your problem.
Good luck with your coursework (i guess ;).
$sql = "SELECT *
FROM course
WHERE coursename='$coursename'".
"ORDER BY coursename";
Your code is good but it is much better to use concatenate the string and the variable so it is easily interpreted and also I wanna point out that there is no space before your ORDER BY statement that can cause an error, so make sure there are spaces between them coursename = '" . $coursename . "' ORDER BY. See the full query below
$sql = "SELECT *
FROM course
WHERE
coursename = '" . $coursename . "' ORDER BY coursename";
$sql = "SELECT *
FROM course
WHERE coursename='" . $coursename . "'
ORDER BY coursename";
$result = mysql_query($sql, $conn);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
echo $row['coursename'];
}
} else {
echo "given coursename does not exist";
}
?>
I have the following php code:
mysql_query("SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'");
I also have a string:
$mynumbers = "AND b.question_code IN (1);";
How can I combine this string withing the mysql_query()?
Thanks,
mysql_query("SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."' " . $mynumbers);
But keep in mind that AND GROUP BY all_surveys.question_code IN (1); is incorrect sql and makes no sense.
You can also do like this if you want more simplicity;
$sql="SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
$sql.=$mynumbers;
echo $sql;
Also as zerkms said your sql seems to be incorrect
First is, you can not combine above two statements, the alternatively you can do like this-
//Here i assume that you want to concatenate 2nd condition on particular situation so you need to add if condition or else you can directly contcate it with "." (dot) operator.
$query = "SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
if(//your condition) $query .= "AND GROUP BY b.question_code IN (1);";
mysql_query($query);
Dont use AND before Group By
Try the following code,
$query = "SELECT a FROM b WHERE b.c = '".$_REQUEST['companyName']."'";
$query .= " GROUP BY b.question_code IN (1)"
mysql_query($query)
When someone selects a service number, then it will pop down and only give the total one records. I want it to give to total from two tables..
<?php
$qry = mysql_query ("SELECT SUM(fruit) AS total FROM fruitinventory WHERE `fruitcolor` LIKE '%{$_POST['select']}%'");
$row2 = mysql_fetch_assoc($qry);
echo $row['total'];
?>
<?php
$qry = mysql_query ("SELECT SUM(fruitsmart) AS total FROM fruitrotten WHERE `fruitcolor` LIKE '%{$_POST['select']}%'");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
?>
Then
I want the total of both rows to display then too.
<?php
$total return = $row+$row2
?>
You can consolidate your two database calls into a single query, and use the columns of the result as your variables to echo out:
<?php
$input = mysql_real_escape_string($_POST['select']);
$sql = "
SELECT
(
SELECT SUM(fruit)
FROM fruitinventory
WHERE fruitcolor LIKE '%$input%'
) AS totalfruit,
(
SELECT SUM(fruitsmart)
FROM fruitrotten
WHERE fruitcolor LIKE '%$input%'
) AS totalfruitsmart";
$qry = mysql_query($sql);
$row = mysql_fetch_assoc($qry);
echo $row['totalfruit'] . '<br />';
echo $row['totalfruitsmart'] . '<br />';
echo $row['totalfruit'] + $row['totalfruitsmart'];
?>
Please, at the very minimum, use mysql_real_escape_string() to help prevent against SQL injection attacks which is what your original code was wide open to.
Assuming you want to keep that same sort of structure for you code, you can display a total like so (Note: MySQLi (i = improved) functions should be used as MySQL functions are deprecated):
<?php
$qry = mysqli_query ("SELECT SUM(fruit) AS total FROM fruitinventory WHERE `fruitcolor` LIKE '%{$_POST['select']}%'");
$row2 = mysqli_fetch_assoc($qry);
echo $row2['total'];
$qry = mysqli_query ("SELECT SUM(fruitsmart) AS total FROM fruitrotten WHERE `fruitcolor` LIKE '%{$_POST['select']}%'");
$row = mysqli_fetch_assoc($qry);
echo $row['total'];
$total = $row['total'] + $row2['total'];
echo $total;
?>
Since it will come up, I suggest you look at using PDO instead of the mysql_ (or at least wrap mysqli_real_escape_string around your variables) to protect your database.
You can try to do the sum using only SQL. Something like below:
$qry = mysql_query ("
SELECT (
( SELECT SUM(fruit) AS total FROM fruitinventory WHERE `fruitcolor` LIKE '%{$_POST['select']}%' ) +
( SELECT SUM(fruitsmart) AS total FROM fruitrotten WHERE `fruitcolor` LIKE '%{$_POST['select']}% )
) AS total") ;
$row = mysql_fetch_assoc($qry);
echo $row['total'];