Newish to mysql. I have a query and it is not showing the value of the cell just the row name:
$sql="SELECT 'first' from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_assoc($res);
echo $row['first'] ;
What am I doing wrong????
Brackets in your query is wrong:
$sql = "SELECT 'first' from `users` WHERE `id` = $userid";
Must be:
$sql = "SELECT `first` from `users` WHERE `id` = $userid";
Note difference in first
First remove quotes from 'first' - it is a column so don't put it in quotes, you can use ` istead.
Next loop through results and that's all.
$sql="SELECT first from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($res))
echo $row['first'] ;
Try:
echo $row[0]['first'];
SELECT 'first'
will simply return the string first.
remove the quotes.
$sql="SELECT 'first' from users WHERE id= $userid ";
you are using normal quotes to select instead of backticks you are not selecting anything from the database.
use
$sql="SELECT first from users WHERE id= $userid ";
instead
and side note:
never "be sure" that your query returns exactly 1 row
use mysql_fetch_assoc() in a loop and check if you really retrieve 1 result.
Related
I'm trying to retrieve the email field from my database using the id associated with it:
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '".$userID."' ") or die(mysql_error());
This query is always returning NULL and I can't work out why.
Have tested using a var_dump that $userID is indeed correct.
But when I use it with the hardcoded value instead of $userID it works fine:
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '85' ") or die(mysql_error());
Why isn't the $userID variable being passed to my query? Is there a way to pass this correctly?
Edit:
Declaration of $userID as requested. var_dump of this variable works OK the line before the query2.
// Fetch ID for matching details
$query = mysql_query("SELECT id FROM `users` WHERE `email` = '".$emailInput."' && `username` = '".$usernameInput."' ") or die(mysql_error());
// Successful query - ID stored
if(mysql_num_rows($query) > 0){
$userID = mysql_fetch_array($query);}
var_dump($userID);
Both var_dumps output the following on the page:
array(2) { [0]=> string(2) "85" ["id"]=> string(2) "85" } NULL
Id say the the fact that $userID is an array is your problem... Do $userID['id'] instead in the query.
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '" . $userID['id'] . "' ") or die(mysql_error());
Its probably a concatenation problem , if the $userid is string its should be fine , but if its an integer or double..etc it will be dealt with as a string
if its an integer try :
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = ".$userID) or die(mysql_error());
or
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = ".$userID['0']) or die(mysql_error());
so, the problem is that your $userID is not 85 or a simple number but it's an array and you are still trying to concatenate it in the query.
The problem is somewhere else in your code, probably where you set $userID
Here goes the solution:
$sql = mysql_query("SELECT email FROM `users` WHERE `id` = " . $userID['id']) or die(mysql_error());
try removing either double quotes or single quotes. because id is usually of data type int you are using twice which makes it a string value..
also PHP is quite smart in recognizing variables and data types so u can use "$userID" or "{$userID}" without concatination..
Try this ....
$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = $userID ") or die(mysql_error());
MYSQL I would like to retrieve a value of a field in columnX where a field value for columnY is specified. There maybe more than one row where this occurs but the first one will do. it would be excellent if you could provide the php. Thanks
Try this SQL:
SELECT `columnX` FROM `table` WHERE `columnY` = $value LIMIT 1
In PHP:
$query = $mysqli->query("SELECT `columnX` FROM `table` WHERE `columnY` = $value LIMIT 1");
$row = $query->fetch_assoc();
echo $row['columnX'];
IF you want to do it in sql you can use the IF condition like :
SELECT IF(X=0,Y,1) As cond FROM table
In PHP:
$query = $mysqli->query('SELECT IF(X=0,Y,1) As cond FROM table');
$row = $query->fetch_assoc();
echo $row['cond'];
thanks to "Daniel Li"
i wrote the following query
SELECT COUNT(userID) From statistics WHERE userID = ""
this query displays the number of unathunticated visit to the website.
the query works in phpmyadmin when i use double quotes however it doesnt when i use single quotes like below it just gives me the number of record stored in the table
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = ''";
$resultB =mysql_query($queryA, $con) or die(mysql_error());
$authB = mysql_result($resultB, "COUNT(userID)");
echo "the number of authenticated visits were $authB<br />\n";
i've no idea why it breaks, any ideas?
you store your query in $queryB but you use $queryA
not sure if it will work...its just first think that came to mind:
how about when u use escaped double quotes?
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = \"\""
Try this:
$queryB = "SELECT COUNT(userID) AS total From statistics WHERE userID = ''";
$resultB =mysql_query($queryB, $con) or die(mysql_error());
$authB = mysql_fetch_assoc($resultB);
echo "the number of authenticated visits were ".$authB['total']."<br />\n";
Does userID have a default value? If the default value is NULL, then change your query to
$queryB = "SELECT COUNT(userID) From statistics WHERE userID IS NULL";
you should change a little to your code
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = ''";
$resultB =mysql_query($queryB, $con) or die(mysql_error());
$authB = mysql_result($resultB, 0, 0);
echo "the number of authenticated visits were $authB<br />\n";
I want to show the maximum value of a particular column of mysql table in php. Here is my code:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$var = $_POST['value'];
$sql = mysql_query(" SELECT MAX(column) FROM tableName WHERE variable='$var' ") or die(mysql_error());
$row = mysql_fetch_array($sql) or die(mysql_error());
echo "Maximum value is :: ".$row['column'];
?>
output:::
Maximum value is ::
Or you could be a bit creative:
$sql = mysql_query("SELECT `column` FROM tableName WHERE variable='$var' ORDER BY `column` DESC LIMIT 1") or die(mysql_error());
This is the same problem that I was searching for a solution. The approach below worked. The solution was using "MYSQLI_NUM". While the code below gives the intended result it still seems way too complex. Is there a way to "short-cut" using the "while" statement to search an array since there is only one returned value?
Selecting a max value only returns one number, yet PHP still seems to require a loop to evaluate an entire array. I was expecting something easy, like: max_value=result(0).
<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error.' Sorry, could not connect to the database server');
$query = "SELECT MAX(IssueIDNUM) FROM tblIssueList";
$result =$conn->query($query);
if (!$result) die($conn->error);
while($row=mysqli_fetch_array($result, MYSQLI_NUM))
{
$maxresult=$row[0];
echo $maxresult;
}
$result->close();
$conn->close();
?>
With further experimentation, I was also able to adapt the code by Jim H. to develop the following solution. Though it works the code still seems too complex.
// Solution #2
$result =$conn->query("SELECT MAX(IssueIDNUM) `max` FROM tblIssueList");
if (!$result) die($conn->error);
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo $row['max'];
}
The real name of column is MAX(column), try:
print_r($row);
You may either do:
$sql = mysql_query(" SELECT MAX(column) AS `column` FROM tableName WHERE variable='$var' ") or die(mysql_error());
Or:
$row = mysql_fetch_row($sql) or die(mysql_error());
echo "Maximum value is :: ".$row[0];
You have two choices. By Index or by Column Name. If you really want to use a column name, alias the column in your query.
$sql = mysql_query(" SELECT MAX(column) `max` FROM tableName WHERE variable='$var' ") or die(mysql_error());
Then you can use
$row['max'];
or
$row[0];
You are probably not returning any rows. You should try WHERE variable LIKE '%$var%';
$sql = mysql_query(" SELECT MAX(column) AS GIVE_A_NAME FROM tableName WHERE variable='$var' ") or die(mysql_error());
and
echo "Maximum value is :: ".$row['GIVE_A_NAME'];
$u_id=$event_assoc['Uniqueid'];
echo $u_id."\n";
$result1 = mysql_query("SELECT * FROM eventdetail WHERE unique_id = '$u_id'", $con1);
while($row = mysql_fetch_array($result1))
{
echo 'in eventdetail'."\n";
$e_id= $row['event_id'];
$destination= $row['destination'];
$uniqueid= $row['unique_id'];
$call_num= $row['channelid'];
}
echo mysql_num_rows($result1);
echo $e_id."\n";
echo $destination."\n";
echo $call_num."\n";
echo $uniqueid."\n";
if(mysql_num_rows($result1)>0)
{
echo 'calculate'."\n";
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
$bil = mysql_fetch_array($result);
$bill= (float) $bil['sum(billsec)'];
echo $bill."\n";
this is my code..
whenever i try to execute sum function query it retruns top row's billsec instead of addition of all row's billsec
You are doing a where on an unique ID in the sum query. Remove the where and it'll work
You are using unique id in your query.. And in a table single row will have that id, thats why you are getting top row's billsec... Remove the where clause in second query, and then check.
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
i guess there is a bug in this area...look again
The PHP statement for the SQL SUM Query should be:-
$result= mysql_query("SELECT sum(`billsec`), `uniqueid` FROM `cdr` GROUP BY `uniqueid` HAVING `uniqueid` = '$uniqueid'", $con2);
You need to use the clause "GROUP BY" whenever you are using any MySQL Aggregate functions (if needed). Also if you are using any aggregate functions (like "SUM", "MAX" etc), then you cannot use "WHERE" clause on the field (in this case, the field is "uniqueid") which is being used in the "GROUP BY" clause.
Hope it helps.