dongyongyu0789
2016-03-19 13:49 阅读 89
已采纳

如何用php从数据库中获取单个值

I am trying to get a single value from database and store it in a variable Here is the structure of my Database

mysql_connect("localhost","root","") or die(mysql_error());;
mysql_select_db("test") or die(mysql_error());
$result = mysql_query('SELECT * FROM names WHERE name = noshair');
while($row = mysql_fetch_array($result));
{
    echo $row['course'] . "<p>";    
}

When I use the above code it prints all the courses against my name from data base but I want a specific course name to be selected, like there are 5 courses against my name and i just want all of then separately to be saved in separate variable.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    dongliao1948 dongliao1948 2016-03-19 14:22

    Give this query a try:

    SELECT DISTINCT name, GROUP_CONCAT(DISTINCT course ORDER BY course) AS courses;
    FROM names
    WHERE name = noshair
    

    and change your echo statement to this:

    echo $row['courses'] . "<p>";
    

    This should output a list of your course like this -> 'java, c#, php, maths' which you could then put in a variable.

    点赞 评论 复制链接分享
  • drzrzzkh462254 drzrzzkh462254 2016-03-19 13:51

    Perhaps you should try the query:

    SELECT GROUP_CONCAT(course)
    FROM names
    WHERE name = noshair;
    

    As side notes, you should stop using mysql_ functions (use mysqli_ or some similar interface). And learn to use parameters in your queries.

    点赞 评论 复制链接分享
  • dongqi7631 dongqi7631 2016-03-19 18:50

    Why don't you use php foreach statement like these:

    foreach ($row['course'] as $key => $value)
     {
        echo $value;
    }
    

    better still you can use the PHP Implode or Explode method to display them separately.

    点赞 评论 复制链接分享
  • dtc99987 dtc99987 2016-03-19 19:04

    I think you have to excape the matching value for WHERE:

    Try this:

    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    $result = mysql_query("SELECT * FROM `names` WHERE `name` = 'noshair'");
    while($row = mysql_fetch_array($result));
    {
        $courses[] = $row['course'];    
    }
    
    var_dump( $courses );
    

    The code saves all the contents to an Array.

    点赞 评论 复制链接分享
  • dsf4354353452 dsf4354353452 2016-09-20 16:13

    Use array to save all courses separately.Then the course name will be save in separate indexes of array like $array[0]="math" and $array[1]="english" .Then use the for loop to save each value in separate variables by setting the condition of loop with total number of values in array.Then the courses will be save separately in different variables like $sub1, $sub2 and so on.Try this code

    $con=mysql_connect("localhost","root","") 
    
    if(!$con)
    {
    die('Not connected : ' . mysql_error());
    }
    $db_selected = mysql_select_db('test', $con);
    if (!$db_selected) {
    die ('Can\'t use test : ' . mysql_error());
    }
       $result = mysql_query('SELECT * FROM names WHERE name = noshair');
    
    
       while($row = mysql_fetch_array($result));
      {
      $value=$row['course'] . "<p>"; 
      echo $value;
      $array[]=$value;
      }
    
     $total=count($array);
     for($i=0;$i<$total;$i++)
     {
    $n=$i+1;
    ${'sub'.$n} = $array[$i];
     }
    
    点赞 评论 复制链接分享

相关推荐