简体   繁体   English

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

[英]How to get single value from database with 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个课程,我只想将所有这些分别保存在单独的变量中。

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). 作为旁注,您应该停止使用mysql_函数(使用mysqli_或某些类似的接口)。 And learn to use parameters in your queries. 并学习在查询中使用参数。

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语句更改为:

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. 这应该输出课程列表,例如->'java,c#,php,maths',然后可以将其放入变量中。

Why don't you use php foreach statement like these: 为什么不使用php foreach语句,如下所示:

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

better still you can use the PHP Implode or Explode method to display them separately. 更好的是,您可以使用PHP Implode或Explode方法单独显示它们。

I think you have to excape the matching value for WHERE: 我认为你必须超越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. 该代码将所有内容保存到数组。

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 使用数组分别保存所有课程。然后,课程名称将保存在数组的单独索引中,例如$ array [0] =“ math”和$ array [1] =“ english”。然后使用for循环保存每个值通过使用数组中的值总数设置循环条件来设置单独的变量,然后将课程分别保存在不同的变量中,例如$ sub1,$ sub2等。

$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];
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM