Load mysql result into php variable - php

Hello I have this code:
<?php $servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT varible FROM test";
$result = $conn->query($sql);
echo $result;
?>
All I want is to load a number from mysql table and load it into php variable and work with that variable as number.
Thanks for any reply.

try this:
$sql = "SELECT varible FROM test";
$result = $conn->query($sql);
$res = mysqli_fetch_assoc($result);
echo $res['varible'];
if you have a list then you can use:
while($res = mysqli_fetch_assoc($result)) {
echo $res['varible'];
}

in the last line do it like this:
$sql = "SELECT varible FROM test";
$result = $conn->query($sql);
$row=$conn->fetch_array($result);
echo $row;

MySQLi::query() returns a MySQLi_Result object for SELECT queries. You should read the documentation.
The MySQLi_Result object has a variety of method that you can use to get at the rows.
If we use e.g. MySQLi_Result::fetch_object() then we can do it like this:
while ($row = $result->fetch_object()) {
var_dump($row);
}

You got it all wrong...think about it one second: if the query returns multiple results how can you store it in one variable?..you need or to loop through the results and assign each time a different value or store the results in an array.

Related

I can't print the query results from my database through php, nothing happens

I'm trying to echo the query made to my database. So far the code runs but nothing happens. I'm very new at coding so help or an explanation about what I'm doing wrong would be awesome! Thanks a lot!!!
<?php
$servername = "servername";
// REPLACE with your Database name
$dbname = "dbsname";
// REPLACE with Database user
$username = "username";
// REPLACE with Database user password
$password = "password";
$ID = 1;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Error de conexion.";
}
$sql = "SELECT * FROM registro WHERE ID = '1' ";
$result = $conn->query($sql);
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
}
echo $data;
$conn->close();
Thanks everyone who replied, #AbraCadaver was right, I can't echo an array so I used print_r() instead and printed the variable $data. Now the php file prints the array with the query results. Thank you all!
$sql = "SELECT * FROM registro WHERE ID = $ID";
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
print_r($data);
}

PHP mysqli query doesn't return any results

I'm using this code here
<?php
error_reporting(1);
$servername = '127.0.0.1';
$username = '';
$password = '';
$dbname = 'splafpoo_users';
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
exit;
}
$key = '';
if(isset($_POST['key'])){
$key = $_POST['key'];
}
$query = "SELECT * FROM users WHERE serial='$key'";
echo $query;
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row;
?>
Running the query SELECT * FROM users WHERE serial='test' in phpMyAdmin returns the desired result however when trying to display the result using the code above nothing is displayed and I cannot figure out how. How do I display the result?
You're gonna need a good old fashion while loop
while($row = $result->fetch_assoc()) {
echo $row['WHATEVERCOLUMNITISYOUWANT'];
}
also this is most definitely a duplicate.
Use var_dump($row) instead of echo $row or you use echo with a key:e.g. echo $row["user"]

phpMyAdmin: one table in database work, another doesn't

I've created an app were you can register as a user. You can sign up and then you're in the database "myAppDataBase" in "firsttable". A second table contains a list of lets say other important users that I manually created in the PHPmyAdmin-Website/"App". This table is called "secondtable".
My code to get the data is as follows:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydatabas";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
//Print ("successfully connected");
}
$query = "SELECT * FROM firsttable";
$result = mysqli_query($conn, $query) or die("Error: " . mysqli_error($query));
$num = mysqli_num_rows($result);
$rows = array();
while ($r = mysqli_fetch_assoc($result))
{
$rows[] = $r;
Print ("sf");
}
Print json_encode($rows);
mysqli_close($conn);
?>
The only thing i changed was this line: THIS WORKS
$query = "SELECT * FROM firsttable";
But when I change it to this it won't work anymore.
$query = "SELECT * FROM secondtable";
Any help?
Change this:
mysqli_error($query)
With this:
mysqli_error($conn) // with your connection
Explanation:
mysqli_error() function needs connection link identifier not your query as param.
Mysqli_error PHP Manual
I SOLVED IT! Somehow, my second wasn't encoded the right way. I simply added this coder and it worked:
mysqli_set_charset($conn, 'utf8mb4');
Thanks for all you help though. ;)

Adding different values together from same table

I have a table. In that table, there is are two fields called to and amount. I need to display the sum of amount of all rows where to value is equal to a particular value. (Say 34). How to achieve this?
Code I have done so far
$result = mysql_query("SELECT * FROM transactions WHERE to = '34'")or die(mysql_error());
while($row = mysql_fetch_array( $result )) { echo $row['amount']; } ?>
The above code gives the individual amount value of each row. But what I want is the sum of these values.
The Database
Use SUM to add all the amounts and echo it
$result = mysql_query("SELECT SUM(amount) as amounts FROM transactions WHERE to = '34'")or die(mysql_error());
while($row = mysql_fetch_array( $result )) { echo $row['amounts']; } ?>
I suggest you to use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.
Here is the complete example of your code by using MYSQLi Object Oriented:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT SUM(amount) as amounts FROM transactions WHERE to = 34";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['amounts']. "<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
?>

PHP MySQLi echo data in array without doing a while loop

When using MySQLi, do I have to perform a kind of while loop where the actual data from the query is put into a variable array?
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
// Check if able to connect to database
if ($conn->connect_error) {
trigger_error("Database connection failed: " . $conn->connect_error, E_USER_ERROR);
}
$sql = "SELECT name FROM users WHERE email = '$email'";
$rs = $conn->query($sql);
$numRows = $rs->num_rows();
I always do the following:
$rs->data_seek(0);
while($row = $rs->fetch_assoc()) {
$name = $row['name'];
}
echo $name;
Isn't there a much more convenient way to echo the data form the query when there is only one row?
If there is only one row, you don't need the loop. Just do:
$row = $rs->fetch_assoc();
$name = $row['name'];

Categories