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.
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 7 years ago.
Improve this question
I have for example a var.php, that connect to a database, and read some data from there:
while($row = mysqli_fetch_assoc($result)) {
echo "$".$row["name"]. " = " . $row["value"]. ";<br>";
}
and the result looks like:
$VAR1 = 3.00000;
$VAR2 = 5.00000;
$VAR3 = 8.00000;
$VAR4 = 9.00000;
How can i embed this php (using echo or include or require) in another php, and will it work like variables? ( Sorry for my poor english :( )
You should take a look at variable variables (http://php.net/manual/en/language.variables.variable.php).
For your example:
while($row = mysqli_fetch_assoc($result))
{
$$row["name"] = $row["value"];
}
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'm having trouble getting a mysql transaction to work through PHP. I'm fairly new to PHP and very new to mysql.
If I take the var_dump of $query and try to run it through phpmyadmin it works fine.
$description =
mysql_real_escape_string($_REQUEST['description']);
$query = 'BEGIN;
INSERT INTO indiespark.tasks
(description, owner_user_id)
VALUES ("' . $description . '", '
. $user->user_id . ');
SET #task_id = LAST_INSERT_ID();
INSERT INTO indiespark.projecttasks
(task_id, project_id)
VALUES (#task_id, ' . $project->project_id . ');
COMMIT;';
$result = mysql_query($query);
var_dump($query);
var_dump($result);
if ($result) {
return viewproject();
} else {
throw new Exception('database error');
}
mysql_query doesn't support sending multiple queries in one call. Use separate calls.