dongzanghua8422 2013-08-14 07:10
浏览 15

如何从数据库信息中创建变量

I want to make a PHP variable equal to an entry in a database. The connection to the database is fine because it is doing other things, but the variable isn't showing up.

Here is my variable declaration:

$companyNameRet = mysqli_query($con,"SELECT company_name FROM x6_quote_data WHERE quote_number = '".$getQuote."'") ;

The variable is to be used in the "Placeholder" attribute of an input field, effectively to pre-populate a form with data from a database.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dpyic24480 2013-08-14 07:13
    关注

    The following should work:

    $row=$companyNameRet->fetch_assoc();
    $myvariable=$row['company_name'];
    

    good luck!

    For multiple variables, $row will be populated $row['column_name'] for as many column names as you have. It can get quite confusing if you are decrypting or doing anything fancy with functions so it may be worth assigning aliases:

    SELECT column1 firstcol,column2 secondcol,column3 thirdcol...
    

    In this case, $row would have the aliases as keys: $row['firstcol'], $row['secondcol'] etc... Try var_dump($row) to see what you get. It is most educational to do so.

    You can fetch multiple rows by using:

    while($row=$companyNameRet->fetch_assoc())
        {
        // $row will contain consecutive rows selected from your db.
        }
    

    W3 Schools provide a decent tutorial on mysqli basics. You would do well to read it.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?