Php execute lines [closed] - php

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.)

Related

How to find name all rows from a table in MySQL? [closed]

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;
}

PhP database function [closed]

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';
}

query for fetching only one integer value using php [closed]

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];
}
?>

Two fields in one field [closed]

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'])
);

Store Facebook user data in database [closed]

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 9 years ago.
Improve this question
I'm trying to store this in the database but it's not working. Is there a better way to do this? It's picking up the user session but it won't insert the data into the database.
if($user)
{
$user_interest = $facebook->api('/me/interests');
for($i = 0; $i < sizeof($user_interest[data]); $i++)
{
$name = $user_interest[data][$i]['name'];
$category = $user_interest[data][$i]['category'];
$categoryId = $user_interest[data][$i]['id'];
$created_time = $user_interest[data][$i]['created_time'];
$strsql = "INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES(\"$fbId\",\"$categoryId\",\"$category\",\"$name\",\"$created_time\")";
mysql_query($strsql, $connect) or die(mysql_error());
$chkrow1 = mysql_affected_rows($connect);
}
"INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES(\"$fbId\",\"$categoryId\",\"$category\",\"$name\",\"$created_time\")";
Strings in MySQL (and other SQL) use single quotes.
"INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES('$fbId', '$categoryId', '$category', '$name', '$created_time')";
Also, escape all of those values using mysql_real_escape_string before interpolating them, then stop using the deprecated mysql_ extensions and use parametrized queries with PDO.

Categories