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

时间:2022-11-27 18:27:43

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个课程,我只想将所有的课程单独保存在单独的变量中。

5 个解决方案

#1


0  

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'然后你可以把它放在一个变量中。

#2


0  

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.

作为附注,你应该停止使用mysql_函数(使用mysqli_或一些类似的接口)。并学习在查询中使用参数。

#3


0  

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方法单独显示它们。

#4


0  

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.

代码将所有内容保存到数组中。

#5


0  

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

#1


0  

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'然后你可以把它放在一个变量中。

#2


0  

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.

作为附注,你应该停止使用mysql_函数(使用mysqli_或一些类似的接口)。并学习在查询中使用参数。

#3


0  

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方法单独显示它们。

#4


0  

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.

代码将所有内容保存到数组中。

#5


0  

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