mysql query result in php variable
Of course there is. Check out mysql_query
, and mysql_fetch_row
if you use MySQL.
Example from PHP manual:
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Output mysql query result in a php variable
Just try this:
$con = mysqli_connect("localhost","user","pass", "database_name"); //your connection
$sql = "SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(CURRENT_DATE, dob)),'%y Years %m Months %d Days') AS age FROM table_name WHERE id=".$row['id'];
$query = mysqli_query($con, $sql);
$result = mysqli_fetch_assoc($query);
echo $result['age'];
Don't forget to replace table_name.
Store query result as php variable
The mysqli_fetch_row
function returns the array with enumerated keys, so you should use
$locID = $row[0];
If you want, you can use mysqli_fetch_assoc
to get the row with associative keys instead.
how to assign a mysqli query result value to a php variable?
Your query is wrong. Do like below:-
<?php
$con=mysqli_connect("localhost","user","pass","db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT COUNT(*) as total_count FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'CustomersTable'") or die(mysqli_error($con));
$something = mysqli_fetch_assoc($result);
echo $something['total_count'];//or do var_dump($something);
mysqli_close($con);
?>
PHP: How can variables be assigned values from a MySQL Query?
Here an example of a Variable-Variable:
$sql="SELECT * FROM fruits";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$variable = $row["variable"];
$$variable = $row["value"];
echo("Name: " . ${variable} . " Value: " . $$variable . "<br>");
}
}
Best regards
How to use a php variable in a query to SELECT something WHERE thing = $thing?
- first thing i did escaping the string that's going to be passed to the query to prevent SQL Injection but this might help but not giving you full protection consider using prepared statement's.
- you are trying to output the
mysqli_query()
result as a string you cannot do that you have to fetch the data from the result to some kind of array then fetch it usingwhile
loop. to fetch those data you needmysqli_fetch_assoc()
function. - feel free to change the column name to whatever you want from clubID to clubName for example if you have column name named clubName you can also put them side by side if you want to output all column data but in your
select
statement you have to fetch*
data to be able to do that any way copy and paste the following code and let us know what happens. {}this is to be able to use single qoute in the double qoute to fetch the data from the array if you don't do that the variable won't expand.
.
<?php
session_start();
$email = mysqli_real_escape_string($link,$_SESSION["email"]);
echo $email;
$sqlget = "SELECT clubID FROM users WHERE email = '$email'";
$sqldata = mysqli_query($link, $sqlget) or die('error');
while($sqloutput=mysqli_fetch_assoc($sqldata)){
echo "{$sqloutput['clubID']}</br>";
}
?>
PHP query result into variable
First off change mysql_fetch_array to mysqli_fetch_array :)
nevermind the following line, iil just leave it in, I saw you used LIMIT.
And secondly change mysqli_num_rows($result)==1 to mysqli_num_rows($result) > 0
PHP MySQL query result into a variable and then echo ($variable)
You must first know as a developer, that mysql extension in php will be fully deprecated in the future as it is already in the newer php versions. So use instead Mysqli extension and PDO for sanitation and securer code for your database.
As it goes to your question:- Try the following ;
// Make a MySQL Connection
$query = "SELECT cat_id FROM category ORDER BY cat_id DESC LIMIT 1";
//assign result to a variable
$result = mysql_query($query) or die(mysql_error());
//fetch result as an associative array
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['cat_id'];
You can assign it to avariable $row['cat_id'] = $catId;
like that and use it .
Related Topics
Remove Trailing Slash from String PHP
Getting Output and Exit Status from Shell_Exec()
How to Display Special Characters in PHP
While ($Row = MySQL_Fetch_Array($Result)) - How Many Loops Are Being Performed
Php: Strtotime Is Returning False for a Future Date
How to Reverse a Unicode String
How to Debug Why Simplest MySQL Query Returns False
Upgrading PHP on Centos 6.5 (Final)
Twig for Loop for Arrays with Keys
How to Enable --Enable-Soap in PHP on Linux
Strict Standards: Only Variables Should Be Assigned by Reference PHP 5.4
Laravel Model Callbacks After Save, Before Save, etc
PHP Strtotime +1 Month Behaviour
PHP Run Once and Insert Twice in MySQL Database
Process Mathematical Equations in PHP
Store_Result() and Get_Result() in MySQL Returns False
Why a Function Checking If a String Is Empty Always Returns True