Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
a.php
include 'function.php';
$avg = get_ecomm_avg_rating();
echo "Avg rating :- $avg";
function.php
$conn = mysqli_connect("myserver","abc","red", "live");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
function get_ecomm_avg_rating()
{
$query = "SELECT AVG(ROUND((r.ratings_sum / r.ratings_qty),1)) AS total_rating
FROM abc AS c, xyz AS r
WHERE c.store
IN (994,094)
AND r.reviewid = c.id
AND c.published = '1'";
$avg_rating_result = mysqli_query($conn,$query);
$avg_rating_row = mysqli_fetch_array($avg_rating_result);
$avg_rating_count = $avg_rating_row[0];
$avg_rating_count_final = number_format($avg_rating_count, 1);
return $avg_rating_count_final;
}
Always return 0.0. What should I do? Any batter approach? I'm New to functions and PHP
There is a problem with a variable scope, var $conn doesn't exists in the function.
You need to add this var by parameter:
function get_ecomm_avg_rating($conn, $storeno) {
....
}
// and here when calling the function you pass there a DB connection
$avg = get_ecomm_avg_rating($conn);
// $conn has to be defined elsewhere, like $conn = mysqli_connect(...)
Then, you have there parameter $storeno which is unused.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is my function .Earlier it was working in a very better way but now a day it is not working well .
function table_rows()
{
$sql="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='check_db' AND `TABLE_NAME`='tbl_details'";
$result= mysql_query($sql);
return $result;
}
The function results..Something like this .I want only field names.
COLUMN_NAME
tbl_structure.php?change_column=1&field=id&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=fnme&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=lnme&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=age&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=dob&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=mail&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=ads&token=8a5756ad27bfde74d341bfed684767e5
first, mysql_ is deprecated. Use mysqli_ or PDO
second, you are only executing the sql query, but not fetching any data. Try the code below. Note that $con is the variable to connect to db. You should change it to your own
(Edited)
https://www.w3schools.com/php/func_mysqli_query.asp
https://www.w3schools.com/php/func_mysqli_fetch_array.asp
function db () {
static $con;
if ($con===NULL){
$con = mysqli_connect ("localhost", "root", "", "database");
}
return $con;
}
function table_rows()
{
$con = db();
$sql="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='check_db' AND `TABLE_NAME`='tbl_details'";
$query= mysqli_query($con,$sql);
$result= mysqli_fetch_array($query,MYSQLI_ASSOC);
return $result;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm not sure how I can query a MYSQL database with PHP by incoming URL value. For example my API receives ?name=Radi and I want to check if there's user with name "Radi" in my database.
// How to define isNameExisting. Can I use PDO or something similar?
if (isNameExisting) {
echo"OK"
}
You can use php's PDO and MySQL's count() for that.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
$stmt = $pdo->prepare('
SELECT
Count(*) as c
FROM
tablename
WHERE
name=?
');
$stmt->execute( array($_REQUEST['NAME']) );
$row = $stmt->fetch();
if ( intval($row['c']) > 0 ) {
echo 'Ok';
}
else {
echo 'no such record';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to execute these lines When I click on the button:
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
It will pick another word from the database using that query.
The button in the HTML page:
echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
How can I do this?
To actually run the query you can use MySQLi. Here is a good beginners guide.
//Connect to database.
$db = new mysqli('localhost', 'user', 'pass', 'database');
//The code you provided.
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
//Run the query.
$result = $db->query($sql);
//Get the first (and presumably only) row
$row = $result->fetch_assoc();
//Output the word.
echo $row['word'];
//Free up some memory.
$result->free();
To get the link to work, you need to have the name of the page. You can either hardcode it, or use $_SERVER['PHP_SELF']:
echo 'Play again!'
(I don't know what you use the $n for so I just left it in there.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
hey i just want a query which will be printing only the result of count query . put in the else part
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
}
?>
You would use the function mysql_fetch_row() to return a row from your database query as an array. You can then access the result for the count(*) as the first element in the returned array.
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
$row = mysql_fetch_row($rq);
echo $row[0];
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am not experienced in PHP so I need your help on this. I would like to create on array from two, if I can say like that.
Basically, I would like to make from this:
[{"Naziv":"Nemanja","duzina":"45.475","sirina":"23.423"},{"Naziv":"Aleksandar","duzina":"45.427","sirina":"21.124"}]
to something like this:
[{"Naziv":"Nemanja","duzina":["45.475","23.423"]},{"Naziv":"Aleksandar","duzina":["45.427","21.124"]}]
Here is my PHP:
// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);
// Check connection
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// query the application data
$sql = "SELECT Naziv,duzina,sirina FROM lokacije";
$result = mysqli_query($con, $sql);
// an array to save the application data
$rows = array();
// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
// close the database connection
mysqli_close($con);
// echo the application data in json format
echo json_encode($rows);
Instead of
$rows[] = $row;
create a new array in your desired format:
$rows[] = array(
'Naziv' => $row['Naziv'],
'duzina' => array($row['duzina'], $row['sirina'])
);