将 MySQL 数据库值分配给 PHP 变量 [英] Assign MySQL database value to PHP variable

查看:42
本文介绍了将 MySQL 数据库值分配给 PHP 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含产品和价格的 MySQL 数据库表.虽然是 html 表单,但我在某个 php 文件中获得了产品名称.对于这个文件中我想做的操作,我也需要相应的价格.

I have a MySQL Database Table containing products and prices. Though an html form I got the product name in a certain php file. For the operation in this file I want to do I also need the corresponding price.

对我来说,以下内容看起来足够清楚:

To me, the following looks clear enough to do it:

$price = mysql_query("SELECT price FROM products WHERE product = '$product'");

然而,它的回声返回:

Resource id #5 

取而代之的是像这样的值:

instead a value like like:

59.95

似乎还有其他选择,例如mysqli_fetch_assocmysqli_fetch_array但是我无法让他们输出任何有意义的东西,我不知道该使用哪个.

There seem to be other options like mysqli_fetch_assoc mysqli_fetch_array But I can't get them to output anything meaningful and I don't know which one to use.

提前致谢.

推荐答案

您需要从数据库中获取数据

You will need to fetch data from your database

$price = mysql_query("SELECT price FROM products WHERE product = '$product'");
$result = mysql_fetch_array($price);

现在你可以用

echo $result['price'];

作为旁注,我建议您切换到 PDO<代码>mysqlimysql_* api 已弃用,很快将不再维护

As side note I would advise you to switch to either PDO or mysqli since mysql_* api are deprecated and soon will be no longer mantained

这篇关于将 MySQL 数据库值分配给 PHP 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆