I try to create a function that will get all values (int) in specific column in my database (MySQL), and create the total of all values. I have an error: Resource id #12
This is my function:
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) FROM articles");
return $result;
}
And this my declaration:
<h4><?php echo vuesCours(); ?></h4>
Thank you in advance for your response.
This is not error just you have to do one thing more after query, so try to replace following code and try again.
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) as total FROM articles");
$row = mysql_fetch_array($result);
return $row['total'];
}
It may help you.
you need to return not results but the value of row. something like this:
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) as total FROM articles");
$row = mysql_fetch_array($result)
return $row['total'];
}
hope this helps.
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT views FROM articles");
$sum=0;
while($row=(mysql_fetch_row($result)))
{
$sum=$sum+$row['views'];
}
echo $sum;
1) i have just changed the select query
2)we have result set of query in $result
3)mysql_fetch_row will return result row as an enumerated array
from which you can access
particular column by it's name or by giving id into subscript brackets in a line $sum=$sum+$row[index/name of column];
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
i would like to get the number of rows of a mysql database table with one single statement or a function
include "opendatabase.php"; //opens database
while (NUMBEROFROWS > 0){
//do something
}
the NUMBEROFROWS should be replaced with the statement that returns the number of rows
i already tried to create a function
function getRowNumber(){
$query = "SELECT COUNT(*) FROM `votes`";
$result = mysql_query($query, $connect);
list($length) = mysql_fetch_row($result);
return $length;
}
but it does not work if i dont put the include "opendatabase.php"; in it.
what am i doing wrong
$result = mysql_query("SELECT * FROM tablename");
if (mysql_num_rows($result) > 0) {
// rows found..
}
the problem is that include "opendatabase.php"; runs in another scope like described here
http://www.php.net/manual/en/language.variables.scope.php
there is a global $connect missing within the function
Here you go:
function num(){
$data = mysql_query("SELECT * FROM table");
if(mysql_num_rows($data) > 0){
while($row = mysql_fetch_assoc($data)){
// do something with your data..
}
}
}
would this work for you ?
function getRowNumber()
{
$query = "SELECT COUNT(*) as counts FROM `votes`";
$result = mysql_query($query, $connect);
$row = mysql_fetch_array($result);
$counts = $row['counts'];
return $counts;
}
In my database I have a field called spaj_per as an auto increment primary key.
How can I display the lastest inserted value in the field spaj_per. I tried this.
<?php
$con = mysql_connect("localhost", "SuperAdmin", "***");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("sistem_pengurusan_fail",$con);
$q = "SELECT MAX(id) AS spaj_per FROM unit_pengambilan";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
?>
<?php echo $row['spaj_per']; ?>
But it won't work.
You may use
SELECT id FROM mytable ORDER BY id DESC LIMIT 1;
or like
$last_id = mysql_insert_id();
Try this,
$q = "SELECT MAX(spaj_per) AS spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_row($result);
echo $row[0];
Use PHP's mysql_insert_id() function
Try the following
<?
$q = "SELECT MAX(spaj_per) AS last_spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_array($result);
echo $row['last_spaj_per'];
?>
$row = mysql_fetch_array($result); Change to $row = mysql_fetch_assoc($result); and try
Try with mysql_insert_id
For more reference use following link
http://php.net/manual/en/function.mysql-insert-id.php
For ex. adress page test.php?prid=4477535
Code page test.php
function query($query) {
$database = 'test';
$host = 'test';
$username = 'test';
$password = 'test';
$link = mysql_connect($host,$username,$password);
if (!$link) {
die(mysql_error());
}
$db_selected = mysql_select_db($database);
if (!$db_selected) {
die(mysql_error());
}
$result = mysql_query($query);
mysql_close($link);
return $result;
}
$product_idn=$_GET['prid'];
$select_image = query("SELECT * FROM products_images WHERE `product_idn`='$product_idn'") or die(mysql_error());
foreach ($select_image as $row)
{
$select_image_array[]=$row->image;
}
print_r ($select_image_array);
receives a request
SELECT *
FROM products_images
WHERE `product_idn` = '4477535'
If make select from phpmyadmin i have 10 rows.
But if i use test.php?prid=4477535 i see empty page.
print_r ($select_image_array) not show array.
Tell me please why i see rows with phpmyadmin and not see rows with script?
Like the other said, you are prone to SQL injection since you don't serialize your input, but to fix your code, use this:
$select_image = query("SELECT * FROM products_images WHERE `product_idn`='$product_idn'") or die(mysql_error());
while($data = mysql_fetch_assoc($select_image))
{
echo $data['image'];
}
You are doing it wrong.
You have to fetch the resource (mysql_query returns a resource) into an array, and the keys of the array will be the names of the rows returned from your query.
$product_idn=$_GET['prid'];
$select_image = query("SELECT * FROM products_images WHERE `product_idn`='$product_idn'") or die(mysql_error());
while($fetch=mysql_fetch_assoc($select_image))
{
echo $fetch['image'];
}
print_r ($select_image_array);
BTW, You have a security hole here - SQL Injection.
Test the following
$result = query("SELECT * FROM products_images WHERE `product_idn`='$product_idn'")
$select_image = mysql_fetch_assoc($result);
var_dump($select_image);
for more information look at http://se2.php.net/mysql_query
You just
echo $row->image;
Never initialize $select_image_array
print_r ($select_image_array); won't show anything because there is no $select_image_array defined. Did you mean print_r ($select_image);?
Is query() a function you've defined? If not and you don't have errors on you are likely to see nothing.
You also need to sanitize your SQL. Simplest method for now since it's an integer:
$product_idn=(int)$_GET['prid'];
How do I fetch only one value from a database using PHP?
I tried searching almost everywhere but don't seem to find solution for these
e.g., for what I am trying to do is
"SELECT name FROM TABLE
WHERE UNIQUE_ID=Some unique ID"
how about following php code:
$strSQL = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = mysql_query($strSQL) or die('SQL Error :: '.mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['name'];
I hope it give ur desired name.
Steps:
1.) Prepare SQL Statement.
2.) Query db and store the resultset in a variable
3.) fetch the first row of resultset in next variable
4.) print the desire column
Here's the basic idea from start to finish:
<?php
$db = mysql_connect("mysql.mysite.com", "username", "password");
mysql_select_db("database", $db);
$result = mysql_query("SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID");
$data = mysql_fetch_row($result);
echo $data["name"];
?>
You can fetch one value from the table using this query :
"SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID limit 1"
Notice the use of limit 1 in the query. I hope it helps!!
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["name"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();