Read value from database, echo - php

Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>

Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}

You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);

You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}

Related

how to fetch all user data from database?

i want fetch all users data but am getting only one user details, please help me to solve
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql6="SELECT username FROM users";
if($result = mysqli_query($conn, $sql6)){
while ($row=mysqli_fetch_array($result)){
//Hashrate Data Fetch
$investedusername = $row['username'];
$sql3="SELECT sum(hashrate_amount) as total FROM buyhashrate WHERE invested_username='$investedusername'";
$result = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result);
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
Your re-using the $result field, change your second reference to something like ...
$result1 = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result1);
This will stop it reseting the value your using for your main loop in
while ($row=mysqli_fetch_array($result)){
You use the $result variable for both mysqli_query
$result = mysqli_query($conn, $sql3);
While other answers are right about your mistake, I want to give you a better solution.
Try to use a join like this:
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql="SELECT SUM(hashrate_amount) AS total FROM users AS t1 LEFT JOIN buyhashrate AS t2 ON (t1.username=t2.invested_username) GROUP BY t1.username";
if($result = mysqli_query($conn, $sql)){
while ($row=mysqli_fetch_array($result)){
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
This way you do just one query from database. But using your method you have n+1 queries which n is the number of users. So for one hundred users there is 101 queries.

Simple php + mysql printing a variable from a database

sorry to bother you all but I'm really struggling with this one:
I connect to my database fine and then I try the following mysql statements:
$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);
However, a few lines later when I try :
echo $answer1;
I'm given only nulls :(
Can anyone give me any suggestions please?
edit:
SQL logins:
mysql_connect("correct", "username", "password");
mysql_select_db("dbname") or die(mysql_error());
everything you did is right you have just to fetch the data like this:
$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);
while($data= mysql_fetch_array($answer1)){
echo $data['row1'];
}
And this is a complet answer, i adjust it as you need ;)
<?php
//Connect to your database
$con=mysqli_connect("db_hostname","db_user","db_password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Value of the row to select
$row2 = 'some value';
//Make select query
$result = mysqli_query($con, "SELECT row1 FROM MyTable WHERE row2='$row2'");
//Fetch datas
while($row = mysqli_fetch_array($result))
{
echo $row['row1'];
echo "<br>";
}
//Close database
mysqli_close($con);
?>
Good Luck :)
Try using MySQLi_* instead MySQL_* functions and pass the connection variable to the function calls.
If this doesn't work then you might want to try some further debugging by enabling all error reporting and dumping the global scope.
<?php
error_reporting(E_ALL); // Show all errors & warnings
$conn = mysqli_connect("server", "username", "password");
mysqli_select_db($conn, "dbname") or die(mysql_error());
$sql1 = "SELECT `row1` FROM `mydatabase` WHERE `row2` = '".$Name."';";
$query1 = mysqli_query($conn, $sql1);
$answer1 = mysqli_fetch_assoc($query1);
var_dump($GLOBALS); // Dumps all variables in the global scope
?>
add this after $answer1= mysql_query($query1);
while ($row = mysql_fetch_assoc($answer1)) {
// echo data
echo $row['row1'];
}

SELECT, WHERE MYSQL php table confusion

Link to Mysql Table
I'm trying to have a basic website display info from a table to use as content. I run this
$result = mysqli_query($conn,"SELECT cont_id, cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
Each page_location is a page on the website. I would like to do something like
<?php echo $row['cont_text'] WHERE cont_id = 15; ?>
I know that's not right but then down the page I would echo cont_text from a new cont_id. How would I go about this?
Do I need to reorganize my table? Or multiple mysqli_query's?
You're over-thinking it. Just run one query using cont_id in the where clause of the query:
$result = mysqli_query($conn,"SELECT cont_text FROM content WHERE cont_id = 15") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
echo $row['cont_text'];
edit
Based on your comments try this:
$result = mysqli_query($conn,"SELECT cont_id , cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$content = array();
while($row = mysqli_fetch_array($result)) {
$content[$row['cont_id']] = $row['cont_text'];
}
echo $content[15];

Unable to count database users

I can't count my users in the database with my current configurations.
<?php
require_once 'scripts/app_config.php';
mysqli_connect(DATABASE_HOST,DATABASE_USER,DATABASE_PASS,DATABASE_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "There are: ". $num . " users in the database";
mysqli_close($con);
?>
Your problem is this:
$num = mysql_num_rows($result);
Your query only returns 1 row, so I presume the problem you're having is that it always outputs: There are 1 users in the database.
You should instead use:
$num = mysqli_fetch_array($query)[0];
Either use COUNT(*) OR use mysql_num_rows
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$num = $row[0];
echo "There are: ". $num . " users in the database";
Note : mysql_* function is deprecated, move on mysqli_* function asap.

MySQL - count total number of rows in php

What is the best MySQL command to count the total number of rows in a table without any conditions applied to it? I'm doing this through php, so maybe there is a php function which does this for me? I don't know. Here is an example of my php:
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("some command");
$row = mysql_fetch_array($result);
mysql_close($con);
?>
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysql_close($con);
?>
Either use COUNT in your MySQL query or do a SELECT * FROM table and do:
$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";
mysqli_num_rows is used in php 5 and above.
e.g
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
Use COUNT in a SELECT query.
$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);
you can do it only in one line as below:
$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;
use num_rows to get correct count for queries with conditions
$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";
for PHP 5.3 using PDO
<?php
$staff=$dbh->prepare("SELECT count(*) FROM staff_login");
$staff->execute();
$staffrow = $staff->fetch(PDO::FETCH_NUM);
$staffcount = $staffrow[0];
echo $staffcount;
?>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
echo "number of rows: ",$rowcount;
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
it is best way (I think) to get the number of special row in mysql with php.
<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>
Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php
$sql = "select count(column_name) as count from table";
Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard. I hope it helps.
PHP Code
// create a function 'cnt' which accepts '$tableName' as the parameter.
function cnt($tableName){
global $conection;
$itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
echo'<h6>'.$itemCount.'</h6>';
}
Then when I need to get the count of items in the table, I call the function like following
<?php
cnt($tableName = 'projects');
?>
In my HTML front end, so it renders the count number
It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.

Categories