Simple Way to Read Single Record from MySQL

how to select a single row in mysql?

By using LIMIT in mysql you can decide how many rows you want to fetch.

Try this:

SELECT * FROM time_table LIMIT 1;

Selecting one row from MySQL using mysql_* API

Try with mysql_fetch_assoc .It will returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. Furthermore, you have to add LIMIT 1 if you really expect single row.

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];

Selecting a single row in MySQL

If I understand your question correctly:

SELECT * FROM tbl WHERE id = 123 OR colname4 = 'x' OR colname8 = 'y' LIMIT 1

The 'LIMIT' keyword makes sure there is only one row returned.

How Select Only One Record in MySQL?

Try using the limit statement, Example:

SELECT * FROM table LIMIT 0, 1

Read one column from one row from a MySQL database

use this:

$result = mysql_query("SELECT value FROM table WHERE row_id='1'");
return mysql_result($result, 0);

How to read one row from mysql?

Your code makes no sense. You already know the surname and the name because you pass them as parameters for the WHERE condition. A part from this, the problem with surname being null is the fact that you don't execute the second command.

You should add

//2. Read surname
string sql = "select surname from profili where name=@name and surname=@surname";
cmd = new MySqlCommand(sql, konekcija);
cmd.Parameters.Add("@name", MySqlType.VarChar).Value = name;
cmd.Parameters.Add("@surname", MySqlType.VarChar).Value = surname;
reader = cmd.ExecuteReader();
person_1.surname = reader["surname"].ToString();

Said that, it makes no sense to execute a command two times to retrieve a single row. Your query text could easily add all the column names that you want to retrieve and execute just one read

//1. Read the row matching the known surname and name 
string sql = @"select name, surname, column1, column2, colx
from profili where name=@name and surname=@surname";

cmd = new MySqlCommand(sql, konekcija);
cmd.Parameters.Add("@name", MySqlType.VarChar).Value = name;
cmd.Parameters.Add("@surname", MySqlType.VarChar).Value = surname;
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read()) {
person_1.name = reader["name"].ToString();
person_1.surname = reader["surname"].ToString();
person_1.Property1 = reader["column1"].ToString();
...and so on for other columns

Printing when there's only a single record in MySQL / PHP

If you're absolutely certain that this query will always retrieve 1 row then this should be enough:

$row = mysql_fetch_assoc(mysql_query($sql));

Then you can manipulate $row (the single row) at your will.



Related Topics



Leave a reply



Submit