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'];
}
Related
I am new into php and mysql and also on this website. So I hope I do everything good.
I am trying to get the SUM of some values in a column. But I don't see any result of this on the website. I am trying for a week now searched a lot of information and topics but so far no result at all. So I would like to ask a favor to you in the hope someone can see what I do wrong.
This is what I have now.
<?php
$con=mysqli_connect("localhost","user","1234","dbname");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$res = mysqli_query($con,"SELECT SUM(Bedrag) FROM `Uitdraai` WHERE Datum BETWEEN '2018-01-01' AND '2018-03-01'");
if (FALSE === $res) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($res);
$sum = $row[0];
mysqli_close($con);
?>
I already tried almost every option also on the following pages but so far no luck. I just get my regular webpage only without the answer.
Get sum of MySQL column in PHP
Single Result From SUM With MySQLi
I will be really thankful!
SELECT SUM(Bedrag) AS value_sum FROM `Uitdraai` WHERE Datum BETWEEN '2018-01-01' AND '2018-03-01;
In the PHP code, try this:
$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();
}
$result = mysqli_query($con,'SELECT SUM(Bedrag) AS value_sum FROM `Uitdraai` WHERE Datum BETWEEN '2018-01-01' AND '2018-03-01');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
echo $sum;
Using PDO (mysql_query is depreciated)
$stmt = $handler->prepare("SELECT SUM(Bedrag) AS value_sum FROM `Uitdraai` WHERE Datum BETWEEN '2018-01-01' AND '2018-03-01");
$stmt->execute();
$row = $handler->fetchAll(PDO::FETCH_OBJ);
$sum = $row->value_sum;
echo $sum;
You should know that mysql_connect is depreciated from php 7 or upper. For this you need to use mysqli or pdo for interacting with the server.
you can try below code for getting sum from the database.
$con = new mysqli("localhost", "my_user", "my_password", "my_db");
$sql = "SELECT SUM(Bedrag) AS value_sum FROM Uitdraai WHERE Datum BETWEEN '2018-01-01' AND '2018-03-01'";
$stmt = $con->query($sql);
if($stmt){
$result = $stmt->fetch_assoc();
echo $result['value_sum'];
}
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'];
}
When I run the below code i get an error message C:\wamp\www\web\polling\includes\resul and Warning: mysqli_query() expects parameter 1 to be mysqli, integer given i
<?php
$pollid = $_POST['foodID'];
$connection = include('connection.php');
$query = "SELECT * FROM polling WHERE foodID='$pollid'";
$q = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($q)) {
$id = $row[0];
$food = $row[1];
$foodRate = $row[2];
$userEmail = $row[3];
echo "<h1>$food</h1>";
echo "<h1>$userEmail</h1>";
}
?>
Try mysqli_affected_rows() and see if $q is getting any data, if not it will never enter the while loop
Besides that it appears there is an issue in your connection, can you display how your connecting in connection.php?
I'm not sure but the two different types of mysql interactions on the same page raises a red flag. Do you have other pages that work with two types of mysql interactions?
EDIT 1: Try this
$connection = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
This should work for your first script
Is there a reason your using two different types of mysql interaction?
You are missing a symbol in the statement
$query = "SELECT * FROM polling WHERE foodID='$pollid'";
s/b
$query = "SELECT * FROM `polling` WHERE foodID='$pollid'";
I have the following PHP code, which is supposed to get the "first" and "last" values of the column where the "id" matches what is in the URL. For instance, when the URL is ../Profile?id=1, it would say First: Bob Last: Doe, because Bob Doe is attached to the id of 1.
I do this with the following code:
session_start();
$id = $_GET['search'];
$dbhandle = mysql_connect("localhost", "SocialAdmin", "******")
or die("Unable to connect to MySQL");
$selected = mysql_select_db("socialdonuttesting",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT * FROM users WHERE id = '$id'");
$first = $result['first'];
$last = $result['last'];
echo "First: $first Last: $last";
But for some reason, this is just displaying First: Last: when I go to ../Profile?id=1. Does anyone know why?
You are not fetching the result.
$row = mysql_fetch_assoc($result);
echo $row['first'];
echo $row['last'];
You need http://php.net/manual/en/function.mysql-fetch-row.php
However, drop the mysql_ usage and go with PDO.
Also, you are vulnerable to sql injection in your example.
you are not fetching you query
try this
session_start();
$id = $_GET['search'];
$dbhandle = mysql_connect("localhost", "SocialAdmin", "******")
or die("Unable to connect to MySQL");
$selected = mysql_select_db("socialdonuttesting",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT * FROM users WHERE id = '$id'");
while($row = mysql_fetch_array($result)){
$first = $row['first'];
$last = $row['last'];
echo "First: ". $first. " Last: ". $last ."</ br>"; }
$result is a resource, you have to use mysql_fetch_* functions. In addittion, I don't recommend you to use * in your SQL query (for example, if you change table structure in the future, the results will be messed up.
I am trying to Build a simple search that first grabbs 'query' from a form passed from a HTML form through the url to this script. Once I run the script I get the output: Resource id #140Resource id #141Resource id #142. Why am I getting this output and what does it mean?
Side note I am just using the "echo" as a way to see the output of each variable.
<?php
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());
mysql_select_db("dataBase") or die(mysql_error());
?>
<?php
$query = $_GET['query'];
// gets value sent over search form
$user_id = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
echo $user_id;
$account_id = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
echo $account_id;
$user_name = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
echo $user_name;
?>
This is not the way to print the results. The method mysql_query returns a resource that you have to use within a loop to actually print the results. For instance, loop at the second example in the official doc page.
P.S. $query = $_GET['query']; using this statement you could have Sql injections problems.
Try something similar to this - after first "SELECT" query :
while($user_id_obj = mysql_fetch_object($user_id))
{
echo $user_id_obj->id;
}
The way you implemented leads to SQL Injection Attacks
SQL Injection Attacks Example
This could be possible in two ways.Which is usefull for you is depends on your requirements.
1.if your query contains a single value as a result then following code with changes in your code will be usefull for you.
<?php
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());
mysql_select_db("dataBase") or die(mysql_error());
?>
<?php
$query = $_GET['query'];
// gets value sent over search form
$result_user = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
if (!$result_user) {
die('Could not query:' . mysql_error());
}
$user_id=mysql_result($result_user,0); // outputs first user's id
echo $user_id;
$result_accountuser = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
if (!$result_accountuser) {
die('Could not query:' . mysql_error());
}
$account_id=mysql_result($result_accountuser,0); // outputs first accounts_users's account_id
echo $account_id;
$result_account = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
if (!$result_account) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result_account,0); // outputs first accounts's account_name
?>
2.Or your query contains more than one result or more than one rows than following changes in your code will help you
<?php
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());
mysql_select_db("dataBase") or die(mysql_error());
?>
<?php
$query = $_GET['query'];
// gets value sent over search form
$result_user = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
while($row=mysql_fetch_array($result_user))
{
$user_id = $row['id'];
echo $user_id;
}
$result_accountuser = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
while($row=mysql_fetch_array($result_accountuser))
{
$account_id = $row['account_id'];
echo $account_id;
}
$result_account = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
while($row=mysql_fetch_array($result_account))
{
echo $row['account_name'];
}
?>