Echo single MySQL field - php

I want to echo out one field from my database so I do not want to use a while loop.
The database table is called index and the field that I want to echo is called title.
What is wrong with this code as the output is just blank.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($sql);
echo $row['title'];

You're passing a wrong argument to mysql_fetch_array(). Modify it as follows.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($result);
echo $row['title'];
You need to pass $result and not $sql with the mysql_fetch_array()function.

Try:
$row = mysql_fetch_array($result);
print_r($row); ///see what you get

The fastest solution would be mysql_result
$result = mysql_query('SELECT title FROM index LIMIT 1');
$field = mysql_result($result, 'title');

You may want to add LIMIT or check your database against something
$result = mysql_query("SELECT * FROM index WHERE id='$someid' LIMIT 1");

Related

Select query for getting a single value is not working

$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help

How to I retrieve data from a mysql table cell and put it into a variable in php?

I am creating a function that will retrieve an id number from a table with multiple columns, put it in a variable which i want to use to send as a "claims number" in an email to a claimant and also echo on a confirmation screen. Here's what I have so far.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$claim_id = $result;
$result = mysql_fetch_array(mysql_query($sql));
$claim_id = $result['id'];
Supposing you are sure that you will receive 1 row.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$c = mysql_fetch_assoc($result);
$claim_id = $c['lname'];
NB*: And get ready for a lecture. Someone is going to tell you that mysql is depracated.
Start using MySQLi or PDO (recommended).
Try :
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$claim_id = $row[0];
or
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$claim_id = $row['id'];
}
$row = mysql_fetch_array($result);
$claim_id = $row['id'];
please note that you should also check for errors (e.g. if($result === FALSE)). Also, there are other ways to fetch data - I recommend looking at mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_object
Also, don't rely on the statement always returning exactly one row - make sure that the result is as expected: mysql_num_rows
Give this a try:
$sql = "SELECT id FROM warranty_claim where lname='".$lname."'";
$result = mysql_query($sql);
$objResult = mysql_fetch_array($result);
$claim_id = $objResult['id'];

PHP loop not performing as it should

$query = "SELECT * FROM table WHERE data = '$userinput'";
$row = mysql_query($query);
while ($row = mysql_fetch_array($row))
{
echo $row['data'];
}
Ok So my questions are:
What exactly does mysql_fetch_array return?
AND if my WHERE clause finds multiple matches, shouldn't the loop execute numerous times?
I am running a program and I can only get the first result to print
You're overwriting $row. Instead use a different variable for the query result.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo $row['data'];
}
Try using mysql_fetch_assoc($row)
You should not use the same variable as the index and the loop variable.
$row = mysql_query
while( $row = ...
Replace the first $row with $result

While returning no results

I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.

PHP MySQL single column/row display?

I'm trying to get a single result from my database, just one name.
I tried using;
$row = mysql_fetch_array(mysql_query("SELECT * FROM persons WHERE id = '$id'"));
echo $row['name'];
But that din't work, any other way to simply show only one result?
Thanks in advance!
[EDIT:]
(I'm using PHP 5.3)
<?php
include("connection.php");
$id = $_GET['deletid'];
$result = mysql_query("SELECT * FROM persons WHERE id = '$id' LIMIT 1");
if(!$result){
echo mysql_error();
}
if ($row = mysql_fetch_array($result)){
echo $row['name'];
}
echo "<p>id:$id</p>";
?>
If you need just the name and you need just one result you should rewrite your query as follow:
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1"));
Now to get the result you should just get it with a
$row['name'];
EDIT
Now that you posted your entire code i got what's wrong: You are deleting that result before getting its name. Basically you delete that user and then you attempt to get its name.
EDIT
<?php
include("connection.php");
if (empty($_GET['deleteid'])) {
exit('"deleteid" is empty');
}
$id = mysql_real_escape_string($_GET['deletid']);
$result = mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1");
if(!$result){
echo mysql_error();
}
$row = mysql_fetch_assoc($result); // for just one result you don't need of any loop
echo $row['name'];
echo "<p>id:". htmlspecialchars($id) ."</p>";
?>
try
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = ". (int) $id));
echo $row['name'];

Categories