doucu9677 2015-07-29 19:10
浏览 62
已采纳

如何将单个MySQL结果保存到PHP变量?

I have this database that I've been working on, and I'm at the point where I'm trying to tie everything together and start integrating the information from my database tables into actual web pages. What I'm trying to do is to query the database for specific answers which I want to save to PHP variables so I can perform a simple <?php echo $variable; ?> throughout my webpage. However I can't seem to figure out how to do that.

Here's a sample of what I've been trying.

SQL

$Name = "John Smith";

$Nickname = $db->query('SELECT `Nickname` FROM `Profiles` WHERE `Name` =' . $Name);
$country = $db->query('Select `Country` from `Profiles` Where `Name` =' . $Name);

PHP/HTML

<div>
<table>
<tr>
<th colspan ="2"><?php echo $Name; ?></th>
</tr>
<tr>
<td>Country</td><td><?php echo $country; ?></td>
</tr>
<tr>
<td>Nickname</td><td><?php echo $Nickname; ?></td>
</tr>
</table>
</div>

This isn't EXACTLY what I've been doing, the table(s) I'm trying to plug this information into is/are far more complicated, which is why if at all possible, I'd like to avoid using the normal method of returning MySQL queries:

Code I'd like to avoid if possible

<?php
while ($rows = $country->fetch()){
echo ['Country'];
}
?>

In the event that the above code is still by far the simplest way to return any/all query results, I just have one question: Is it possible to nest different while ($rows = $variable->fetch()) statements within each other? Because my table has information from different database tables somewhat scattered throughout it and there are a couple of places where I would NEED to nest the WHILE statement. Would it work?

Edit:

So I've tried a couple of suggestions. First, I changed my SQL syntax for the $Name so that it will work in the SQL query.

`WHERE `Name` ="' . $Name . '"');`

So now that's working. But I've tried the other 2 suggestions and both are breaking my code (the page won't load).

Also, no one's answered my question about being able to nest While() Statements.

  • 写回答

2条回答 默认 最新

  • douhai5835 2015-08-01 13:28
    关注

    I figured it out! I was able to assign the results from my database query into variables that I could then recall through a simple <?php echo $variable; ?> at any time, as many times as needed. Here is what I did.

    SQL Example

    $result = $db->query('Select `Name`, `Age`, `Color` from `People` ');
    while ($rows = $result->fetch()){
    $Name = $rows['Name'];
    $Age = $rows['Age'];
    $Color = $rows['Color'];
    }
    

    In Webpage

    <p>
    Hello, my name is <?php echo $Name; ?>. 
    I am <?php echo $Age; ?> years old and my favorite color is <?php echo $Color; ?>.
    </p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵