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"];
}
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 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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = mysqli_fetch_array($result))
{
$libid=$row['libid'];
$regno= $row['regno'] ;
$name= $row['stuname'] ;
$branch= $row['branch'] ;
$semester= $row['semester'] ;
$section= $row['section'] ;
$yearofadm= $row['yearofadm'];
}
}
Dont post anything directly in database as its a threat to data security (SQL Injection)
$libid = $_POST['libid'];
$libid = mysqli_real_escape_string($connection, $libid);
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".$libid."'");
Make sure that your mysql field is really libsutdent. Seems like it should be libstudent.
Then Place {} around your Post variable. ie {$_POST[\"libid\"]}.
Conversely you can place another step in your code like:
$libid = $_POST["libid"];
I think you can do without the quotes around libid, but I always think it reads better to add them.
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
Should be
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".mysql_real_escape_string($_POST['libid'])."'");
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.
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 trying to query the db in php and get if there is a match / success but it's not resulting in success although it should so I've thinking there's a syntax error?
Here is the code:
if ($db_found) {
$SQL = "SELECT * FROM wp_users where user_login='.$user.'";
$result = mysql_query($SQL);
//Check if theres a match
if $result > 0 {
echo 'There was a match';
}
...
}
if (mysql_num_rows($result)) {
echo 'match';
}
if (mysql_num_rows($result)==1) {
echo 'There was a match';
}