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'])
);
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 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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP and MySQL. I've created a form that uploads a url to my database field and I would like to know how to display the stored url from the database to a webpage.
Like this
$query = "SELECT url FROM table";
$resource = mysql_query($query);
if (mysql_num_rows($resource) > 0) {
$results = array();
while($record = mysql_fetch_array($resource)) {
$results[] = $record;
}
}
foreach ($results as $result) {
echo $result['url'];
}
And if you are among the mysql_* complain group:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT url FROM table";
if ($result = $mysqli->query($query)) {
// Loop through results
$results = array();
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$result->free();
}
foreach ($results as $r) {
echo $r['url'];
}
$mysqli->close();
Edit I would like to add that it would not be a bad idea to first consult a beginners PHP/MySQL book and get yourself up to speed with the programming basics.
Resource
PHP Beginner books
As we don't know your database structure or the used PHP library, I assume that you can select the stored information into an array like this:
$row['URL'] = 'your stored url';
Then you can easily print it out using more formulas:
<?php print('Click here'); ?>
Or if you want to integrate php only for the url printing:
Click here
Basically the two is identical, but if you have a large HTML and minimal PHP, then the second solution would be easier to edit and maintain.
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.
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];
}
?>